Narek Margaryan
Narek Margaryan

Reputation: 334

x86 Assembly , stack push instruction

I just found out that the push instruction can have an immediate byte , word , dword arguments and each of these have different opcodes. There were no examples in the book I was reading so I don't understand how the assembler differentiates between these three types. For example , if I write push 12h how will it be interpreted by the assembler and what will actually happen on the stack?

Upvotes: 4

Views: 7113

Answers (3)

F McSorley
F McSorley

Reputation: 1

In DOS or Windows Debug.com (not Debug.exe at least not in my version), you would say Push byte 12 or push word 1234. Push dword 12345678 might also work, for a double word (4 bytes, 32 bits), but I'm not at my PC at this moment so I can't try it

Upvotes: -2

Deleted User
Deleted User

Reputation: 2541

The assembler generates a different opcode for each variant. It will examine the argument before deciding which opcode to assemble to. Because 12h from your example is not a name of a register, but meets the characteristics of hex representation of a number, it concludes that pushing an immediate value is wanted, and generates the corresponding opcode along with the binary value as instruction. It will also examine whether the argument is enclosed by square brackets, for indirection. For the CPU, when executing that code, those different variants are de facto different instructions - albeit sharing some commonalities when executed.

Examining the argument(s), to determine their nature, is what the assembler does with many instructions, other than push, too, for the same purpose: deciding which opcode needs to be chosen for the instruction.

Upvotes: 2

Michael
Michael

Reputation: 58507

That's up to the assembler. It may pick the opcode with the smallest operand field large enough to hold the immediate value. It may also require you to tell it which variant you want to use.

For example, NASM will assemble push 12h into 6A 12 (push byte 12h).

If you wanted e.g. to get the push imm16 variant you'd say push strict word 12h (strict is necessary if you don't want NASM to optimize the instruction into a byte push).

Note that an immediate byte push doesn't actually push a byte onto the stack. The value will be sign-extended to at least 16 bits before being pushed (this happens during execution, not during compilation).

Upvotes: 5

Related Questions