Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segfault with x86 assembly on mov 0, %eax

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
like image 421
dschatz Avatar asked Apr 29 '11 15:04

dschatz


1 Answers

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.)

like image 57
Bo Persson Avatar answered Oct 07 '22 13:10

Bo Persson