Reputation: 11
I have a simple questions but couldn't find the answer to it. I am working on a programming language which translates to NASM and of course I need to have compatibility with the x64 calling convention Microsoft uses. My test codes just calls puts()
with the string "Hello", sets RAX to 0 and returns. I know that xor rax, rax
would be smaller but optimization is a job for later on.
extern puts
global main
section .data
section .rdata
constp_main:
.c0: db 72,97,108,108,111,0
section .text
main:
push rbp
mov rbp, rsp
lea rcx, [constp_main.c0]
call puts
mov dword eax, dword 0
.return:
pop rbp
ret
This code does not work and produces a crash. If I reserve 24 bytes or more on stack, code like this works:
extern puts
global main
section .data
section .rdata
constp_main:
.c0: db 72,97,108,108,111,0
section .text
main:
push rbp
mov rbp, rsp
sub rsp, 24
lea rcx, [constp_main.c0]
call puts
mov dword eax, dword 0
.return:
mov rsp, rbp
pop rbp
ret
Can anybody tell me why? I thought the callee has to do all that stuff?
(Editor's note: these examples aren't actually safe because not enough stack space is reserved, even if they happen to work in some cases. See comments.)
Upvotes: 1
Views: 571