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
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With