gcc - 推送 ebp : operand type mismatch for `push'

标签 gcc assembly x86

我正在尝试使用 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/

相关文章:

c++ - 使用函数定义作为友元声明时,clang 和 gcc 之间的行为差​​异

C++ GCC 为什么这段 sfinae 代码可以用 GCC 4.7 编译,但不能用 4.8 编译?

windows - 如何在 Windows 的 x86 程序集中编写系统调用?

linux - 如何通过查看堆栈值生成回溯?

组装在模式 x 中读取平面是否需要与写入不同的输出到 VGA 端口?

c++ - 神秘的 For 循环错误

对 gcc -static 选项或其在虚拟机中的行为感到困惑

linux - 使用 stdin 读取文件后需要从 stdin 获取箭头键代码

assembly - 如何开始为 ARM 处理器编写自己的移动操作系统?