堆栈参数正在使用C调用约定在汇编中消失
我目前正在编写一些 x86 汇编代码,并使用as和将其与 glibc 链接ld。
.section .data
str1:
.asciz "n"
str3:
.asciz "Hello"
str4:
.asciz "Goodbye"
str5:
.asciz "Guten Tag!"
str6:
.asciz "Guten Morgen!"
.section .bss
.section .text
.globl _start
_start:
call main
movl %eax, %ebx
movl $1, %eax
int $0x80
# void writeLine(char*);
.type writeLine, @function
writeLine:
pushl %ebp
movl %esp, %ebp
movl $str1, %ecx
pushl %ecx
movl 8(%ebp), %ecx
pushl %ecx
call strcat
movl %eax, %ecx
pushl %ecx
call puts
popl %ebx
.leaver1:
movl %ebp, %esp
popl %ebp
ret
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
movl $str3, %ecx
pushl %ecx
call writeLine
popl %ebx
movl $str4, %ecx
pushl %ecx
call writeLine
popl %ebx
movl $str5, %ecx
pushl %ecx
call writeLine
popl %ebx
movl $str6, %ecx
pushl %ecx
call writeLine
popl %ebx
.leaver3:
movl %ebp, %esp
popl %ebp
ret
问题是,对于预期的输出:
Hello Goodbye Guten Tag! Guten Morgen!
我得到:
Hello Guten Tag!
通过一些更深入的调试,当我这样做时,参数似乎正在消失printf:
pushl 8(%ebp) # Argument for writeLine
pushl $str7 # "Argument for writeLine: %sn"
call printf # printf();
它将“消失”的参数显示为无,例如:
# should be "Goodbye"
Argument for writeLine:
还有,当我打印出来的格式参数%d我最终得到一个常数,只有什么字符串(改变str3,str4等等),我传递。
例子: 134525414
我还尝试将它与 gcc ( gcc -m32 -S main.c) 通过编译一个应该做完全相同的程序生成的程序集进行比较,除了 gcc 生成的东西,我无法通过快速查看代码找到任何东西。
看起来,我的论点正在消失。
回答
strcat(str3, str1)通过将 的字节复制到内存中,从 的终止空字节开始,连接str1到 的末尾。因此,字符的终止空值被覆盖,并且一个新的终止空值被写入接下来的字节。但是你猜怎么着:紧跟在终止 null 之后的字节是 的第一个字节。所以你刚刚用空字节覆盖了第一个字节。因此,当您稍后打印它时,它的行为就像一个空字符串。str3str1str3str3'n'str3str4str4
您可以在每个字符串之间添加一个额外的字节空间以避免这种情况。但更广泛地说,一个writeLine函数修改它传递的字符串并不是真正明智的,所以更好的计划是重新设计它,这样它就不需要这样做了。例如,您可以通过一次调用来编写传递的字符串puts(它已经附加了一个换行符),如果您想要一个额外的换行符,请单独调用putc.