【发布时间】:2010-06-06 02:44:16
【问题描述】:
我认为在暑假期间学习 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 上,这有什么关系。)
【问题讨论】:
标签: assembly x86 gnu-assembler