I decided it would be interesting to study the x86 build during the summer break. So, I started with a very simple world program, borrowing for free gcc -S examples could give me. I ended up with this:
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 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!
It compiles and even works! And I think that I understand most of this.
Although, the magic happens in step 3. If I delete this line, my program will die between calling puts and xor from an error stack error. And I would change $20 to another value, that would work too. Therefore, I came to the conclusion that this value is very important.
The problem is that I do not know what she is doing and why she is needed.
Can someone explain to me? (I'm on Mac OS, that will make a difference.)
assembly x86 gas
zneak
source share