Assembler – “hello, art”

Ich will herausfinden, was “meine” Kunst ist. Ich habe vor mehr als 30 Jahren mal Assembler gelernt und sogar ein paar Programme geschrieben (Was ist Assembler?).
Also los … stellen wir doch mal die Uhr ein wenig zurück.

Ich nutze heutzutage Mac OS X auf einem MacBookAir und möchte ein Assembler Programm erstellen.
Installation des Compilers
Erstmal benötige ich Homebrew als Paket Manager um mir den Assembler Compiler nasm zu installieren. Ich öffne das Terminalfenster und los:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install nasm
Danach erzeugt der Befehl nasm -v die folgende
Ausgabe
$ nasm -v
NASM version 2.11.08 compiled on Sep 22 2015
Der Compiler ist installiert!
Programm schreiben
Ich erzeuge mir eine Datei mit dem Editor nano und füge dort den Code für das hello, art Programm ein. Keine Bange, den Code habe ich auch nur kopiert – Link siehe unten.:
nano hello_art.asm

; hello_art.asm - a "hello, art" program using NASM

section .text

global mystart                ; make the main function externally visible

mystart:

; 1 print "hello, art"

    ; 1a prepare the arguments for the system call to write
    push dword mylen          ; message length                           
    push dword mymsg          ; message to write
    push dword 1              ; file descriptor value

    ; 1b make the system call to write
    mov eax, 0x4              ; system call number for write
    sub esp, 4                ; OS X (and BSD) system calls needs "extra space" on stack
    int 0x80                  ; make the actual system call

    ; 1c clean up the stack
    add esp, 16               ; 3 args * 4 bytes/arg + 4 bytes extra space = 16 bytes
    
; 2 exit the program

    ; 2a prepare the argument for the sys call to exit
    push dword 0              ; exit status returned to the operating system

    ; 2b make the call to sys call to exit
    mov eax, 0x1              ; system call number for exit
    sub esp, 4                ; OS X (and BSD) system calls needs "extra space" on stack
    int 0x80                  ; make the system call

    ; 2c no need to clean up the stack because no code here would executed: already exited
    
section .data

  mymsg db "hello, art", 0xa  ; string with a carriage-return
  mylen equ $-mymsg             ; string length in bytes

Nun muss die Datei compiliert, gelinkt und ausgeführt werden
Kompilierung
nasm -f macho -o hello_art.asm
Linken
ld -o hello_art -e mystart hello_art.o
Ausführen
$ ./hello_art
hello, art

tadaa :)

Und den Rest der Nacht werde ich damit verbringen zu verstehen, was der Code wirklich tut :)

Link:
http://peter.michaux.ca/articles/assembly-hello-world-for-os-x


Posted

in

, , ,

by

Tags:

Comments

One response to “Assembler – “hello, art””

  1. Sofasophia Avatar

    Das fasziniert mich grad sehr. Es ist etwas wie Übersetzen in eine andere Dimension und zurück. So philosophisch habe ich mir Programmieren noch nie gedacht. :-)

Leave a Reply

Leave a Reply