Académique Documents
Professionnel Documents
Culture Documents
• Some instructions can be encoded with just 1 byte, others in 2 bytes, and
many require more. The maximum number of bytes an instruction might
take is 6
Single byte instructions generally specify a simpler operation with a
register or a flag bit.
000 AL AX
REGISTER 001 CL CX
FIELD
010 DL DX
ENCODING
011 BL BX
100 AH SP
101 CH BP
110 DH SI
111 BH DI
MOD = 11 EA CALCULATION
R/M W=0 W=1 R/M MOD = 00 MOD = 01 MOD = 10
Example:
The instruction MOV BL, AL
Stands for “move the byte contents from source register AL to destination
register BL using the general format, show how to encode the instruction in
machine code. Assume that the 6 bit Opcode for the move operation is
100010
Solution: In byte 1 the first 6 bits specify the move operation and must be
100010
Opcode = 100010
The next bit, which is D, indicates whether the register that is specified by
the REG part of byte 2 is a source or destination operand. Let us say that we
In byte2, the source operand, which is specified by the REG field, is AL.
The corresponding code is
REG = 000
Since the second operand is also a register, the MOD field is mode 11
The R/M field specifies that the destination register is BL and the code is
011
MOD = 11 R/M = 011
Therefore
BYTE 2 = 11000011B = C3H
2. inc/dec/neg/mul/div register
Opcode mod r/m
mov/add/sub register, register
3. inc/dec/neg/mul/div memory
mov/add/sub memory, register Opcode mod r/m address
mov/add/sub register, memory
BYTE 1 = 00000011b = 03 h
The REG field bits in byte 2 are 000 to select AX as the destination
register. The other operand is in memory and its address is specified by the
contents of SI with no displacement. For indirect addressing using SI with
no displacement. MOD equals 00 and R/M equals 100
MOD = 00
R/M = 100
Example :
What is the machine code for the instruction, XOR CL, [1234h].
This instruction stand for exclusive-OR the byte of data at memory address
1234h with the byte contents of CL. The Opcode for EX-OR is 001100b
Solution: Using the XOR Opcode 001100b, 1 for destination operand, and 0
for byte data. We get
To specify the address 1234h we must use bytes 3 and 4. The least
significant byte of the address is encoded first, followed by the most
significant byte.
This gives,
BYTE 3 = 34h
BYTE 4 = 12h
Example:
The instruction ADD [BX][DI]+DISP, AX
Means “ add the word contents of AX to the contents of the memory
location specified by based-indexed addressing mode”. The opcode for the
add operation is 000000b and assume that DISP equals 1234h
Solution. The add Opcode, which is 000000b , a 0 for source operand, and a
1 for word data, gives
The REG field in byte 2 is 000 to specify AX as the source register, since
there is a displacement and it needs 16 bits for encoding, the MOD field
obtained is 10. The R/M field, which is set to 001 for an effective address
generated from DI and BX.
BYTE 2 – 10000001b = 81 h
The general format cannot be used to encode all the instructions that can be
executed by 8086. Minor modifications must be made to this general format
to encode a few instructions. In some instructions, one or more single-bit
fields are needed. These 1-bit fields and their functions are shown below.
S 0 No sign extension
1 Sign extend 8-bit immediate data to 16 bit if W =1
REP = 111001z
Register SR
ES 00
CS 01
SS 10
DS 11
Example:
The instruction
Mov WORD PTR [BP][DI]+1234h, 0ABCDh
Stands for “ move the immediate data word ABCDh into the memory location
specified by based-indexed addressing mode”.
Express the instruction in machine code.
Solution:
Since this instruction does not involve one of the registers as an operand, it
does not follow the general format we have been using. The format of byte 1
is an immediate data-to-memory move is
1100011W
In our case, we are moving word size data:
Therefore W = 1
BYTE1 = 11000111b = C7h
BYTE2 = (MOD)000(R/M)
For a memory operand using a 16 bit displacement, MOD = 10, and for based
indexed addressing using BP and DI with a 16 bit displacement R/M = 011
BYTE2 = 10000011b = 83h
Byte 3 and 4 encode the displacement with its low byte first. Thus, for a
displacement 1234h,
BYTE3 = 34h and
BYTE4 = 12h
Last bytes 5 & 6 encode the immediate data, also with LSByte first,
For data word ABCDh, we get
BYTE5 = CDh
BYTE6 = Abh
Therefore
Mov WORD PTR [BP][DI]+1234h,DS
= C7833412CDABh
Solution:
Command Syntax
Quit Q
End use of the DEBUG Program
Dump D [address]
Dump the contents of memory to the display
Enter E [address][list]
Examine or modify the contents of the memory
Trace T [address][number]
Trace the execution of the specified number of instructions.
OF overflow OV NV
DF direction DN UP
IF interrupt EI DI
SF sign NG PL
AF auxiliary carry AC NA
ZF zero ZR NZ
PF parity PE PO
CF carry CY NC
Example:
Issue commands to the debugger on the PC that will cause the value in BX to
be modified to FF00h and the verify that this new value exists in BX.
C:> DEBUG
-R BX
BX 0000
:FF00
-R BX
BX : FF00
:
MOV Instruction
Destination Source
Memory Accumulator
Accumulator Memory
Register Register
Register Memory
Memory Register
Register Immediate
Memory Immediate
Seg-Reg Reg 16
Seg-Reg Mem 16
Reg 16 Seg-Reg
Memory Seg-Reg
In all memory reference Mov instruction, the machine code for the
instruction includes an offset address relative to the contents of the data
segment register
Mov [sum],AX
Let DS contain 0200h
SUM correspond to 1212h
Physical address
PA = 02000 + 1212 = 03212h
Example:
What is the effect of executing the instruction
Mov CX, [SOURCE-MEM]
Where SOURCE-MEM is the memory location offset 20h relative to the
data segment starting at address 1A000h ?
Solution:
[[DS]0 + 20h] [CL]
[[DS]0 + 20h + 1h ] [CH]
OR
CL is loaded with contents held at memory address
1A000h + 20h = 1A020h
CH is loaded with 1A000h + 20h + 1h = 1A021h
XCHG instruction
XCHG AX,DX
[AX original] [DX]
[DX original] [AX]
[AX] [DX]
Destination Source
Accumulator Reg 16
Memory Register
Register Register
If [DS] = 1200h
SUM = 1234h
C:> debug
-R
- ----------
- ----------
-A 1100:101
1100:0101 XCHG [1234],BX
1100:0105
-R BX
BX 0000
: 11AA
-R DS
DS 1076
:1200
-R CS
CS 1076
:1100
-R IP
IP 0100
Assuming that this memory location contains 52h ( EBCDIC carriage return)
This is replaced in AL
[AL] = 52h
Example:
LEA SI, [INPUT]
It loads the SI register with an offset address value. The value of this
offset is represented by the value of INPUT. INPUT is encoded following
the instruction Opcode in the code segment memory.
12000 XX
12001 XX
0020
12200 20
12201 00
12202 00
xxxx 12203 13
ADDITION
SUBTRACTION
MULTIPLICATION
ADDITION
Example:
Assume that the AX and BX register contain 1100h and 0ABCh, respectively.
What are the results of executing the instruction
ADD AX,BX
Solution:
[BX] + [AX] = 0ABCh + 1100h = 1BBCh
Example:
The original contents of AX, BL, memory location SUM, and carry flag [CF]
are 1234h, Abh, 00CDh and 0h respectively. Describe the results of
executing the following sequence of instructions.
Solution:
[AX] [AX] + [SUM] = 1234h + 00CDh = 1301h
The carry flag remains reset.
The second instruction adds to the lower byte of the base register [BL], the
immediate operand 5h and carry flag, which is 0h.
This gives
Example:
What is the result of executing the following instruction sequence?
ADD AL, BL
AAA
Assume that AL contains 32h which is the ASCII code for number 2, BL
contains 34h which is the ASCII code for number 4, and AH has been
cleared.
Solution:
[AL] [AL] + [BL] = 32h + 34h = 66h
Next the result is adjusted to give its equivalent decimal number. This is
done by execution of the AAA instruction. The equivalent of adding 2 and 4
is decimal 6 with no carry.
Therefore the result of after the AAA instruction
[AL] = 06h [AH] = 00h
Example:
Solution:
C:> DEBUG
-R
AX=0000 BX=0000 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=1076 ES=1076 SS=1076 CS=1076 IP=0100 NV UP EI PL NZ NA PO NC
1076:0100 4D DEC BP
-R BX
BX 0000
:1234
-R CX
CS 0000
:1234
-R CX
:0123
-R F
NV UP EI PL NZ NA PO NC –
-A
1076:0100 SBB BX,CX
1076:0102
-R
AX=0000 BX=1234 CS=0123 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=1076 ES=1076 SS=1076 CS=1076 IP=0100 NV UP EI PL NZ NA PO NC
1076:0100 19CB SBB BX,CX
-U 100 101
1076:0100 19CB SBB BX,CX
-T
AX=0000 BX=1111 CX=0123 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=1076 ES=1076 SS=1076 CS=1076 IP=0102 NV UP EI PL NZ NA PE NC
1076:0102 0003 ADD [BP+DI],AL
Example:
Assuming that register BX contains 003Ah. What is the result of executing
the following instruction?
NEG BX
Solution:
Executing the NEG instruction causes the 2’s complement Subtraction, that
follows 0000h – [BX] = 0000h + 2’s complement of 003Ah
= 0000h + FFC6h
= FFC6h
This value is placed in BX
[BX] = FFC6h
C:> DEBUG
-R BX
BX 0000
:3A
-A
1076:0100 NEG BX
1076:0102
-R BX
BX 003A
:
-U 100 101
1076:0100 F7DB NEG BX
-T
AX=0000 BX=FFC6 CX=0000 DX=0000 SP=FFEE BP=0000 SI=0000 DI=0000
DS=1076 ES=1076 SS=1076 CS=1076 IP=0102 NV UP EI NG NZ AC PE CY
1076:0102 0003 ADD [BP+DI]. AL
-Q
The second instruction multiply the same two numbers as signed numbers
[Ax] = -1h x –2h = 2 h
C:> DEBIUG
-R AX
AX 0000
:FF
-R CX
CS 0000
:FE
-A
1076:0100 MUL CL
1076:0102
-R AX
AX 00FF
:
-R CX
CS 00FE
:
-U 100 101
1076:0100 F6E1 MUL CL
Solution:
The first instruction leads AL with A1h . This gives
[AL] = A1h = 10100001b
Executing the second instruction extends the MSB of AL which is 1, into all
bits of AH.
The result is
[AH] = 11111111b = FFh
therefore [AX] = FFA1h
This completes conversion of the byte in AL to a word in AX
The last instruction loads each bit of DX with the MSB of AX.
This bit is also one
[DX] = FFFFh