I want to call two simple function(fun1 and fun2) consecutively, but it seems that only fun1 is executed. Do I need to save/load register before/after the call? (I'm working on windows 10 64bit)
result:
hello 1
hello 2
test 1
code:
segment .data
hello1 db "hello 1", 0xd, 0xa, 0
hello2 db "hello 2", 0xd, 0xa, 0
test1 db "test 1", 0xd, 0xa, 0
test2 db "test 2", 0xd, 0xa, 0
segment .text
global main
extern ExitProcess
extern printf
main:
push rbp
mov rbp, rsp
sub rsp, 32
; Call printf to print the main message
lea rcx, [hello1]
call printf
lea rcx, [hello2]
call printf
call fun1
call fun2
; Exit the program
xor rax, rax
call ExitProcess
; Define additional functions
section .text
fun1:
lea rcx, [test1]
call printf
ret
fun2:
lea rcx, [test2]
call printf
ret
command:
nasm -f win64 -g -o main.o main.s
link /LARGEADDRESSAWARE:NO main.o kernel32.lib kernel32legacylib.lib legacy_stdio_definitions.lib legacy_stdio_wide_specifiers.lib ucrt.lib /subsystem:console /entry:main /out:main.exe /DEBUG
Thanks so much for the help from the comments, now I'm learning to reserve space in the function/procedure and it works.
updated code:
segment .data
hello1 db "hello 1", 0xd, 0xa, 0
hello2 db "hello 2", 0xd, 0xa, 0
test1 db "test 1", 0xd, 0xa, 0
test2 db "test 2", 0xd, 0xa, 0
segment .text
global main
extern ExitProcess
extern printf
main:
push rbp
mov rbp, rsp
sub rsp, 32
; Call printf to print the main message
lea rcx, [hello1]
call printf
lea rcx, [hello2]
call printf
call fun1
call fun2
; Exit the program
xor rax, rax
call ExitProcess
; Define additional functions
section .text
fun1:
sub rsp, 32 ; Allocate
lea rcx, [test1]
call printf
add rsp, 32 ; Deallocate
ret
fun2:
sub rsp, 32 ; Allocate
lea rcx, [test2]
call printf
add rsp, 32 ; Deallocate
ret
result now:
hello 1
hello 2
test 1
test 2