我正在尝试使用 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
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
main.s:6: 错误:“push”的操作数类型不匹配
这不起作用的原因是什么?
来自Intel® 64 and IA-32 Architectures Software Developer's Manual , 7.3.1.5 Stack Manipulation Instructions in 64-Bit Mode:
在 64 位模式下,堆栈指针大小为 64 位,不能被指令前缀覆盖。在隐式堆栈引用中,地址大小覆盖被忽略。在 64 位模式下无法在堆栈上压入和弹出 32 位值。
(强调我的。)
push ebp
尝试压入 32 位寄存器,这在 64 位模式下是不允许的。
这是 32 位代码(即使可编码也会在 64 位模式下push ebp
崩溃),因此您需要将其组装成 32 位可执行文件。使用 gcc 或 clang,使用
gcc -m32 -no-pie -fno-pie main.s -o my_prog
Run Code Online (Sandbox Code Playgroud)
(no-pie 选项不是必需的,但您可能希望它们为 32 位代码获得更简单的位置相关可执行文件。)