我决定在暑假期间学习 x86 汇编会很有趣。所以我从一个非常简单的 hello world 程序开始,借用免费的例子gcc -S
可以给我。我最终得到了这个:
HELLO:
.ascii "Hello, world!\12\0"
.text
.globl _main
_main:
pushl %ebp # 1. puts the base stack address on the stack
movl %esp, %ebp # 2. puts the base stack address in the stack address register
subl $20, %esp # 3. ???
pushl $HELLO # 4. push HELLO's address on the stack
call _puts # 5. call puts
xorl %eax, %eax # 6. zero %eax, probably not necessary since we didn't do anything with it
leave # 7. clean up
ret # 8. return
# PROFIT!
它编译甚至工作!我想我理解了大部分。
不过,魔法发生在第 3 步。如果我删除这一行,我的程序将在调用puts
和xor
未对齐堆栈错误之间终止。我会更改$20
为另一个值,它也会崩溃。所以我得出结论,这个值很very
重要。
问题是,我不知道它的作用以及为什么需要它。
谁能解释我?(我在 Mac OS 上,这有什么关系。)