0

Currently testing stuff in emu8086, and I was wondering, if we had this code:

MOV [0002H], 0005H

In the machine instruction, what would the REG bits become? Does the assembler use a registry to make this possible? If so, which register?

I am not an expert and really just experimenting because of my CS classes.

  • 1
    No register will be involved in executing the above instruction. – 500 - Internal Server Error Apr 13 '22 at 11:22
  • 2
    Are you talking about the REG field of the ModRM byte in the machine code? It's `0`, as shown by the `/0` in the `C6 /0 ib` opcode for `mov r/m8, imm8`, or `C7 /0 iw` for 16-bit (you left the operand-size ambiguous). – Peter Cordes Apr 13 '22 at 11:24
  • @PeterCordes yes, exactly – guacamoleku Apr 13 '22 at 11:25
  • So, no registers are envolved during the execution of this, and this is completely valid. Got it – guacamoleku Apr 13 '22 at 11:27
  • 1
    That's a totally different question from "Does the compiler use a registry to make this possible? If so, which register?" - in that part it seems you're asking if the assembler (not compiler) expands this source line to multiple instructions so it actually writes data to one of the 8 registers. (Which you could have answered by looking at disassembly of the machine code, if emu8086 will do that the way real tools do.) – Peter Cordes Apr 13 '22 at 11:27
  • I mean, yes. If the assembler actually expands the source code and uses a register, the REG bits become 000 and we kind of implicitly use AX (assuming the size to be 16bits) ? – guacamoleku Apr 13 '22 at 11:29
  • 1
    No! That instruction doesn't affect any registers. The `REG` field is used as extra opcode bits by that opcode byte, `0` does *not* mean AX. The destination is encoded by the Mod and R/M bits. A CPU might use some internal temporary to hold the immediate while feeding it to the memory bus, but that's unrelated to the architectural register state (what software sees) or even the physical register file. (A real CPU has temporary storage for lots of reasons other than the named registers.) – Peter Cordes Apr 13 '22 at 11:32
  • Thank you! I have asked this question to my academician who gives this lecture, I was left with no direct answer as to what happens to those bits and whether if we use a register. So this actually helps! – guacamoleku Apr 13 '22 at 11:37
  • @guacamoleku The REG bits are just a field in the modr/m byte. They are not always used to encode a general purpose register. – fuz Apr 13 '22 at 11:46
  • You could have tested in a debugger and single-step to verify that none of the actual register change value after this instruction. Anyway, the actual question about what goes in the `REG` field is basically a duplicate of [How to read the Intel Opcode notation](https://stackoverflow.com/a/53976236) along with the manual: https://www.felixcloutier.com/x86/mov. Except for the part wondering if maybe one of the named registers are used (internally, or in a software-visible way, so the instruction would have two destinations?) I think that idea came up in an old question once, didn't find it. – Peter Cordes Apr 13 '22 at 11:49
  • 1
    Oh, I found [Is there an x86 opcode for moving an immediate byte to a direct memory location (without using registers)?](https://stackoverflow.com/q/7408983) which seems an exact duplicate. – Peter Cordes Apr 13 '22 at 11:53
  • @PeterCordes Yeah, this is similar to mine. However they didn't mention if he should use a register or not even though if it was in the question. But I know that it really doesn't, and it just becomes `imm8` or `imm16` in this case. I really didn't know what words I should've used to search for my question. Thanks for the answer again! – guacamoleku Apr 13 '22 at 12:02
  • 1
    Yeah, I figured it covered the non-use of a register in a different way. That question asks if there is a machine instruction that doesn't use a register, the answers are that yes, there is, and `mov byte ptr [address], value` is it, and show the machine code. If it did have to involve a register, the answers would be downvoted if they didn't say so. – Peter Cordes Apr 13 '22 at 12:07

0 Answers0