Question

I am reading a textbook entitled Introduction to 80x86 Assembly Language and Computer Architecture by Richard C. Detmer

I have a question about immediate to memory mov opcode. Here the portion of text I am referring too:

Continuing down figure 4.1, the next row is for immediate-to-memory moves. Each of these instructions has opcode C6, a ModR/M byte, additional address bytes (if needed), and finally a byte containing the immediate operand. The address is encoded as described above for memory-to-register moves. If, for example, smallCounter references a byte in memory and the instruction mov smallCounter, 100 is assembled, the assembler will generate 7 (3+4) bytes of object code, C6 05 xx xx xx xx 64, where xx xx xx xx represents the address in memory, and 64 is the byte-size hex version of 100. The ModR/M byte 05 is 00 000 101, Mod=00 and R/M=101 for direct memory addressing with the Reg field not needed and set to 000.

As another example, consider mov BYTE PTR [edx], -1 with the memory destination using register indirect mode. The opcode is still C6 and the immediate byte (which always comes last) is now FF for -1. The second byte is the ModR/M byte with Mod=00 for register indirect, Reg=000 (unused), and R/M=010 for EDX, making 00 000 010 or 02. The object code is there for C6 02 FF.

page 92, chapter 4, section 1 - Copying Data

Figure 4.1 - entitled mov instructions with byte destination- is a chart with four columns:

  • the first listing the destination
  • the second listing the source
  • the third listing the Opcode
  • and the fourth listing the Bytes of Object code

The line in the chart the above portion is referring too is:

Destination: memory byte Source: immediate byte Opcode: C6 Bytes of Object Code: 3+

Forgive me for putting all that, but I wish for you and I to be on the same page for what my book is saying. I understand the part with the smallCounter but what perplexes me, is that the object code for the mov BYTE PTR [edx], -1 is without an address in memory. It is in indirect mode, so edx is taking the place as a pointer, so why doesn't the object code contain the address in memory it is pointing too? Is that only for variables like with smallCounter's opcode having an address? Why is overall the opcode the way it is for smallCounter in comparison with the other statement?

Was it helpful?

Solution

The object code doesn't contain the address in memory because that address can't be known at assembly/link time.

The memory address to be modified isn't known until the time the instruction is executed. The opcodes say, "get the address to be modified from the EDX register, rather than from the opcode bytes."

Let's look at the opcode bytes.

C6 05 xx xx xx xx FF  <-- store the value at address xx xx xx xx
C6 02 FF              <-- store FF at the address held in the EDX register

So instead of getting the address from the opcodes, the CPU gets the destination address from the EDX register.

Another thing to think about. This code:

mov edx, offset smallCounter
mov byte ptr [edx], 100

does the same thing as

mov byte ptr [smallCounter], 100

Well, except that the former modifies the EDX register. But both store the value 100 in memory at smallCounter.

That help clarify things?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top