问题
I'm trying to assemble a small piece of x86 code. I'm on a 32 bit machine and I have written the following code. It should just add values into eax and then return. I realize there will not be any output. When I compile this using
gcc main.S -o main
It compiles with no errors. But when I run it seg faults (gdb claims that it segfaults on the first movl instruction). main.S has the following code in it. What am I doing wrong?
.text
.globl main
main:
pushl %ebp
movl %esp, %ebp
movl 0, %eax
addl $3, %eax
addl $3, %eax
leave
ret
回答1:
Not your first, but your second movl
movl 0,%eax
That's a load from a memory source operand with absolute address 0
which of course segfaults.
Use mov $0, %eax
for mov-immediate into a register. (Or for zero specifically, xor %eax, %eax
to more efficiently zero a register.)
来源:https://stackoverflow.com/questions/5834222/segfault-with-x86-assembly-on-mov-0-eax