为什么在调用puts时我必须在堆栈上保留空间?

时间:2016-07-12 08:19:25

标签: assembly x86 64-bit nasm win64

我有一个简单的问题,但找不到答案。我正在研究一种转换为 NASM 的编程语言,当然我需要兼容微软使用的x64调用约定。我的测试代码只用字符串“Hello”调用puts(),将 RAX 设置为0并返回。我知道xor rax, rax会更小,但优化是以后的工作。

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

此代码不起作用并导致崩溃。如果我在堆栈上保留24个字节或更多字节,这样的代码可以工作:

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

有人可以告诉我为什么吗?我以为被叫者必须做所有这些事情?

0 个答案:

没有答案