我正在尝试使用 gcc -c main.s 编译以下内容
.intel_syntax noprefix
.global main
main:
push ebp
mov ebp,esp
sub esp,0x10
mov DWORD PTR [ebp-0xc],0x0
mov eax,DWORD PTR [ebp+0xc]
mov eax,DWORD PTR [eax+0x4]
mov DWORD PTR [ebp-0x4],eax
leave
ret
我得到一个错误:
main.s:6: Error: operand type mismatch for `push'
这不起作用的原因是什么?
最佳答案
来自Intel® 64 and IA-32 Architectures Software Developer’s Manual , 7.3.1.5 64 位模式下的堆栈操作指令:
In 64-bit mode, the stack pointer size is 64 bits and cannot be overridden by an instruction prefix. In implicit stack references, address-size overrides are ignored. Pushes and pops of 32-bit values on the stack are not possible in 64-bit mode.
(强调我的。)
push ebp
尝试推送 32 位寄存器,这在 64 位模式下是不允许的。
这是 32 位代码(即使 push ebp
是可编码的,也会在 64 位模式下崩溃),因此您需要将其组装成 32 位可执行文件。使用 gcc 或 clang,使用
gcc -m32 -no-pie -fno-pie main.s -o my_prog
(no-pie 选项不是必需的,但您可能希望它们为 32 位代码获得更简单的位置相关可执行文件。)
关于gcc - 推送 ebp : operand type mismatch for `push' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52691517/