Segfault with x86 assembly on mov 0, %eax

一笑奈何 提交于 2020-03-16 06:00:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!