19

使用 GNU 汇编器汇编文件时,出现以下错误:

hello.s:6:错误:“push”的指令后缀无效

这是我要组装的文件:

        .text
LC0:
        .ascii "Hello, world!\12\0"
.globl _main
_main:
        pushl   %ebp
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        movl    $0, %eax
        movl    %eax, -4(%ebp)
        movl    -4(%ebp), %eax
        call    __alloca
        call    ___main
        movl    $LC0, (%esp)
        call    _printf
        movl    $0, %eax
        leave
        ret

这里有什么问题,我该如何解决?

尽管问题中的错误和说明有所不同,但该问题与此问题有些相关。

4

4 回答 4

23

预先.code32作为您的第一行。

--32选项会将目标更改为 32 位平台。

于 2012-11-30T01:22:52.547 回答
20

64位指令

默认情况下,大多数操作保持 32 位,而 64 位对应项由 REX 前缀中的第四位调用。这意味着每条 32 位指令都有其自然的 64 位扩展,并且扩展寄存器在 64 位指令中是免费的

movl $1,  %eax     # 32-bit instruction
movq $1,  %rax     # 64-bit instruction

pushl %eax         # Illegal instruction
pushq %rax         # 1 byte instruction encoded as pushl %eax in 32 bits
pushq %r10         # 2 byte instruction encoded as pushl preceeded by REX
于 2011-06-23T13:45:10.290 回答
13

你是用 64 位汇编器组装的吗?您的代码看起来像是 32 位的。使用 64 位汇编程序时,您的代码出现此错误:

example.s:6:suffix or operands invalid for `push'

但它适用于 32 位汇编器。

于 2011-06-07T16:55:40.487 回答
9

您必须使用“64 位语法”,或者您可以使用“--32”选项:通过这种方式,汇编器将其目标更改为 i386 平台。

于 2011-07-20T11:18:07.903 回答