Reputation: 21
EDIT: Based on the answers and comments, it appears both the book and I are correct. BUT IMHO the book did a poor job of explaining the example, I mean instead of something like "For this instruction we wish to store the data in the AX register. Therefore, the D bit will be a logical O." it could have just said something like "There are two ways to store data in the AX register - setting D=0 and directly specifying it in the REG field, or setting D=1 and MOD=11 then specifying the AX REG bits in the R/M field". Please let me know if you agree!
I'm currently in a computer architecture course and one of the texts being used is "Programming the 8086/8088 First Edition" by James W. Coffron (an Amazon review about it says it's "riddled with both grammatical and technical mistakes").
I'm not sure if I'm just stupid but there seems to be a contradiction in the excerpt below:
FIGURE 3.10:
##########
REG bits | 16-BIT REGISTER | 8-BIT REGISTER
000 | AX | AL
001 | CX | CL
010 | OX | OL
011 | BX | BL
100 | SP | AH
101 | BP | CH
110 | SI | OH
111 | 01 | BH
In this example, we will move data from memory, or move a register to or from a register. We will use an instruction like MOV AX,BX. This instruction is 2 bytes because there is no memory address to add on. The bytes will appear as shown in Figure 3.11 (100010DW MOD REG R/M).
We can see in Figure 3.11 that the first byte has the lower two bits as DW, The W is for word or byte (as shown previously). The D is to indicate if the data is to be stored in the operand specified by the MOD and R/M field, D=0, or if it is to be stored in the register specified by the REG field, D=1.
Figure 3.12 shows the MOD and R/M assignments. Notice in the MOD description that if the value is 11, then the R/M field is encoded with a register format. This format was shown in Figure 3.10.
For this instruction we wish to store the data in the AX register. Therefore, the D bit will be a logical O.
^ Isn't AX in the REG list, and didn't they just say that D=1 when storing in a REG field?
This means that the data must be stored in the location specified by the MOD and R/M fields. Therefore, the MOD will equal 11. The R/M field will equal 000, indicating that the AX register is the destination for the data. The REG field for the second byte of data will equal all, thus indicating that the BX register is the source register to be used (this value comes from Figure 3.10). The complete second byte of data for the instruction will be 11 011 000 or D8. Thus, the object code for the instruction MOV AX,BX is 89D8.
^ Using emu8086 I've in fact confirmed that the object code for MOV AX,BX is 8BC3, and converting this to binary the D-bit is indeed set to 1. Based on this I do think there's an error in the textbook, which made me very angry as it wasted a lot of my time.
FIGURE 3.12:
BASE AND INDEX REGISTER SPECIFIED BY R/M FOR OPERANDS IN MEMORY (MOD ≠ 11)
##########
R/M FIELD | BASE REGISTER | INDEX REGISTER
----------
000 | BX | SI
001 | BX | DI
010 | BP | SI
011 | BP | 01
100 | NONE | SI
101 | NONE | 01
110 | BP | NONE
111 | BX | NONE
##########
MOD | DISPLACEMENT | COMMENT
----------
00 | ZERO
01 | 8·BIT CONTENTS OF NEXT BYTE OF INSTRUCTION SIGN EXTENDED TO 16 BITS | INSTRUCTION CONTAINS AN ADDITIONAL BYTE
10 | 16·BIT CONTENTS OF NEXT TWO BYTES OF INSTRUCTION | INSTRUCTION CONTAINS TWO ADDITIONAL BYTES
11 | R/M REGISTER
##########
IF MOD=00 AND R/M=110, THEN:
1. THE ABOVE DOES NOT APPLY
2. THE INSTRUCTION CONTAINS TWO ADDITIONAL BYTES
3. THE OFFSET ADDRESS IS CONTAINED IN THOSE BYTES
Upvotes: 2
Views: 408
Reputation: 58802
Note that mov ax, bx
has two encodings. Both are correct.
The book is using the mov r/m16, r16
, opcode 89
. In this case the destination is the r/m16
hence D=0
, r/m=ax
and reg=bx
. Machine code is 89 D8
.
What you found using emu8086 is the instruction mov r16, r/m16
, opcode 8B
. Here the destination is the r16
hence D=1
, r/m=bx
and reg=ax
Machine code is 8B C3
.
Upvotes: 5