Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate NASM "push byte" to GAS syntax?

I'm "porting" a NASM source to GAS and I found the following lines of code:

push byte 0
push byte 37

GAS doesn't allow "push byte" or "pushb".

How should I translate the above code to GAS syntax?

Thanks

like image 440
Joao Vilaca Avatar asked Apr 05 '09 00:04

Joao Vilaca


2 Answers

pushb was removed from GAS. You should be able to use the push command to get the same effect. A little more information is here.

like image 52
Ben Hoffstein Avatar answered Sep 28 '22 03:09

Ben Hoffstein


1) push byte in NASM 2.11 64 bit compiles to the same as just push, except that it refuses to compile if the thing pushed is larger than a byte:

push 0x0000
push 0x01
push 0x0001
push 0x10

Is the same as:

push byte 0x0000
push byte 0x01
push byte 0x0001
push byte 0x10

But the following fail:

push byte 0x0100
push byte 0x1000
push byte 0x01000000
push byte 0x10000000

All of those compile to the 6a XX form of the instruction.

2) NASM and GAS automatically decide what form to use based on the operand size:

The GAS 2.25:

push $0x0000
push $0x01
push $0x0001
push $0x10
push $0x0100
push $0x1000
push $0x01000000
push $0x10000000

Compiles to the same as the NASM:

push 0x0000
push 0x01
push 0x0001
push 0x10
push 0x0100
push 0x1000
push 0x01000000
push 0x10000000

Objdump:

   0:   6a 00                   pushq  $0x0
   2:   6a 01                   pushq  $0x1
   4:   6a 01                   pushq  $0x1
   6:   6a 10                   pushq  $0x10
   8:   68 00 01 00 00          pushq  $0x100
   d:   68 00 10 00 00          pushq  $0x1000
  12:   68 00 00 00 01          pushq  $0x1000000
  17:   68 00 00 00 10          pushq  $0x10000000

So just push in GAS is the same as push byte in NASM, but without the error checking.

3) The modifier that does exist in GAS is w as in:

pushw $0

which compiles to:

0:   66 6a 00                pushw  $0x0

i.e., adds the 0x66 prefix to toggle 16 bit operation.

NASM's equivalent is:

push word 0

4) The difference from mov is that we cannot control arbitrary push sizes: they are all push fixed amounts to the stack.

The only parameter we can control on the instruction encoding is to include the 0x66 prefix or not.

The rest is determined by the segment descriptor. See the Intel 64 and IA-32 Architectures Software Developer’s Manual - Volume 2 Instruction Set Reference - 325383-056US September 2015.