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
它编译没有错误.但是当我运行它时会出现段错误(gdb 声称它在第一条 movl 指令上出现段错误).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
推荐答案
不是你的第一个,而是你的第二个 movl
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
将 mov-immediate 移入寄存器.(或者对于零,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屋!