Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid instruction suffix for push when assembling with gas

When assembling a file with GNU assembler I get the following error:

hello.s:6: Error: invalid instruction suffix for `push'

Here's the file that I'm trying to assemble:

        .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

What is wrong here and how do I fix it?

The problem is somewhat related to this question although errors and instructions in questions are different.

like image 719
vitaut Avatar asked Jun 07 '11 16:06

vitaut


3 Answers

Prepend .code32 as your first line.

--32 option will change the target to 32 bit platform.

like image 147
Xiao Jia Avatar answered Oct 12 '22 00:10

Xiao Jia


64bit instructions

By default most operations remain 32-bit and the 64-bit counterparts are invoked by the fourth bit in the REX prefix. This means that each 32-bit instruction has it's natural 64-bit extension and that extended registers are for free in 64-bit instructions

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
like image 25
Anu Avatar answered Oct 12 '22 00:10

Anu


Are you assembling with a 64-bit assembler? Your code looks like it's 32-bit. I get this error with your code when using a 64-bit assembler:

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

But it works fine with a 32-bit assembler.

like image 25
Carl Norum Avatar answered Oct 12 '22 00:10

Carl Norum