added macro and function def/call to x32
[nasm.git] / hello32.asm
1 %macro print 2           ; Declare a macro to print a string, with two arguments
2     mov eax,4            ; Put magic # for system call sys_write into rax
3     mov edi,1            ; Prepare arg 1 of sys_write, 1 - file descriptor of stdo
4     mov esi,%1           ; Prepare arg 2, a string reference
5     mov edx,%2           ; Prepare arg 3, string length, no deref. b/c constant 
6     int 80h              ; Call the kernel
7 %endmacro
8
9 section .data
10     hello:     db  'Hello, world.',10   ; declare initialized string with linefeed char
11     helloLen:  equ $-hello              ; declare and init. constant
12     done:      db  0
13
14 section .text
15     global _start        ; Declare program entry point
16
17 _print:
18     push ebp             ; Prologue, save stack pointer
19     mov ebp,esp          ; Get base pointer (start of arguments)
20     mov ebx,[ebp+8]      ; Get first argument (offset from base by the return address)
21     print ebx,helloLen   ; Macro, replaced inline
22     leave                ; Epilogue, equivalent to mov esp,ebp; pop ebp
23     ret                  ; Return control to caller
24
25 _start:
26     cmp byte [done],1    ; Compare byte padded value of done with 1
27     jg exit              ; Jump if greater than to exit
28
29     mov byte [done], 1   ; Put 1 in done
30
31     push hello           ; Push the last argument of _print onto the stack
32     call _print          ; Call the function _print
33
34 exit:
35     mov eax,1            ; The system call for exit (sys_exit)
36     mov edi,0            ; Exit with return code of 0 (no error)
37     int 80h
38
39