-1

I'm trying to encode the following few instructions in 8086.

I've learnt how the whole mod, reg, d, w and everything works. So, I can encode almost all the instructions like MOV [BX + 1234H], DS but I just cant seem to wrap my head around the PUSH and POP instructions?

POP DS Answer: 1FH

PUSH AX Answer: 50H

How do I encode these to binary?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
dydx
  • 157
  • 15
  • 2
    Consult the instruction set reference. `POP DS` is explicitly listed as `1Fh`, it doesn't get easier than that. – Jester Nov 23 '19 at 17:15
  • But, I want to convert it myself. I know I've to look up the MOD table and the 8086 instruction format and I've done that but I can't convert these by myself. – dydx Nov 23 '19 at 17:17
  • 4
    You can't "convert it yourself". You need to look up the opcode. It's `1Fh`. End of story. `PUSH AX` is more interesting, as `PUSH r16` is `50+rw`. Consulting _Table 3-1. Register Codes Associated With +rb, +rw, +rd, +ro_ you can see `AX` is `0` so the machine code is `50h` and **not** `58h` that you claimed (which is `POP AX`). – Jester Nov 23 '19 at 17:19

1 Answers1

3

There are three different encodings for pop depending on what you want to pop. If you want to pop a segment register, the encoding is 07 plus the number of the segment register shifted left by four places, so 07 for pop es, 17 for pop ss and 1f for pop ds. The instruction pop cs used to have opcode 0f, but this was removed in the 80186 processor. It is now the prefix for two-byte instructions. You cannot pop cs anymore. To pop fs or gs, use opcodes 0f a1 for pop fs and 0f a9 for pop gs.

If you want to pop a general purpose register, the encoding is 58 plus the number of the register. The size of the register is 16 bits in a 16 bit operation mode, 32 bit in a 32 bit operation mode or 64 bit in a 64 bit operation mode. It can be changed with a 66. In 64 bit mode, the high bit of the register number also goes into an optional REX prefix.

If you want to pop into a memory location, the encoding is 8f /0, i.e. opcode 8f with a modr/m byte where the register field holds value 0.

Refer to the manual for details. (e.g. https://www.felixcloutier.com/x86/pop, or the full Intel PDF it was extracted from)

fuz
  • 88,405
  • 25
  • 200
  • 352