0

I'm learning assembly code on Linux x64. I wrote something like:

pushq 0x1234567890123456 #It didn't work. 
pushq 0x12345678         #It worked.

I used gcc -c to compile the code and the first line was error with Error: operand type mismatch for 'push'. But pushq means push something 64 bits into stack and the hexadecimal number is exact 64 bits.

So, why would this error occurred ?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    push doesn't have a form that pushes a 64 bit immediate value on the stack. You can push a 32-bit value on the stack and have it sign extended. If you push 0xffffffff on the stack then the 64-bit value 0xffffffffffffffff is placed on the stack if you push 0x1 on the stack then a 64-bit value of 0x1 is placed on the stack. If you want to push a 64-bit value move the value into a 64-bit register and push the 64-bit register on the stack. I also believe you intended to use `$0x1234567890123456` & `$0x12345678` with a `$`since you likely intended to push an immediate val and not a memory operand. – Michael Petch Oct 07 '20 at 03:29
  • I'm assuming that you are using AT&T syntax rather than Intel given the options you claimed to pass to `gcc` – Michael Petch Oct 07 '20 at 03:33
  • Well, actually I'm tring to push a pointer of a string to the stack and I'm pretty sure that my machine is Intel and 64bit. So what should I do if I want to push a 64 bits address to stack? – Desmond121 Oct 08 '20 at 07:51
  • Intel is the processor but the assembler has to two different formats. One is called AT&T syntax and one is Intel syntax. GNU Assembler (which is the default assembler back end for GCC) uses AT&T syntax as a default. In AT&T syntax `pushq 0x12345678` would attempt to read the 8 bytes at memory address 0x12345678 and push those 8 bytes to the stack. `pushq $0x12345678` on the other hand would push the 32-bit (sign extended to 64-bit) immediate value 0x12345678 onto the stack which you could use as a pointer. – Michael Petch Oct 08 '20 at 10:24
  • To push the pointer (64 bit immediate) to the stack you'd have to use something like `movq $0x1234567890123456, %rax` and `push %rax` – Michael Petch Oct 08 '20 at 10:24
  • Got it. Thank you so much! – Desmond121 Oct 08 '20 at 14:44

0 Answers0