mov 0,%eax上具有x86组件的Segfault [英] Segfault with x86 assembly on mov 0, %eax
问题描述
我正在尝试汇编一小段x86代码.我在32位计算机上,并且编写了以下代码.它应该只是将值添加到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
它编译没有错误.但是,当我运行它时,seg会出错(gdb声称它在第一个movl指令上存在segfaults). main.S中包含以下代码.我在做什么错了?
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
这是来自具有绝对地址0
的内存源操作数的负载,该地址当然是段错误.
That's a load from a memory source operand with absolute address 0
which of course segfaults.
使用mov $0, %eax
直接移动到寄存器中. (或者具体来说是零,xor %eax, %eax
可以更有效地将寄存器归零.)
Use mov $0, %eax
for mov-immediate into a register. (Or for zero specifically, xor %eax, %eax
to more efficiently zero a register.)
这篇关于mov 0,%eax上具有x86组件的Segfault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!