8088/8086 MICROPROCESSOR Programming - Integer Instructions and Computations
8088/8086 MICROPROCESSOR Programming - Integer Instructions and Computations
1 Data-Transfer Instructions
8088/8086 MICROPROCESSOR
PROGRAMMING INTEGER
INSTRUCTIONS AND
COMPUTATIONS
Meaning
Format
Operation
Flags affected
MOV
Move
MOV D, S
(S) (D)
None
e.g.
MOV DX, CS
MOV [SUM], AX
8088/8086 MICROPROCESSOR
PROGRAMMING INTEGER
INSTRUCTIONS AND COMPUTATIONS
Destination
Source
Memory
Accumulator
Accumulator
Memory
Register
Register
Register
Memory
Memory
Register
Register
Immediate
Memory
Immediate
Seg-reg
Reg16
Seg-reg
Mem16
Reg16
Seg-reg
Memory
Seg-reg
MOV DX, CS
0100
0100
0200
XXXX
IP
CS
DS
SS
ES
AX
BX
CX
DX
Address Memory
Content
01100
01101
01102
8C
CA
XX
02000
02001
XX
XX
Instruction
MOV DX, CS
Next instruction
SP
BP
SI
DI
8088/8086 MPU
Before execution
611 37100 Lecture 05-6
MOV DX, CS
0102
0100
0200
0100
IP
CS
DS
SS
ES
AX
BX
CX
DX
Address Memory
Content
01100
01101
01102
02000
02001
8C
CA
XX
Instruction
MOV DX, CS
Next instruction
XX
XX
Mnemonic
Meaning
Format
Operation
Flags affected
XCHG
Exchange
XCHG D, S
(D) (S)
None
e.g.
XCHG AX, DX
Destination
SP
BP
SI
DI
8088/8086 MPU
After execution
611 37100 Lecture 05-7
Source
Accumulator
Reg16
Memory
Register
Register
Register
Register
Memory
EXAMPLE
EXAMPLE
Solution:
((DS)0+2016) (CL)
((DS)0+2016+116) (CH)
Therefore CL is loaded with the contents held at memory address
1A00016 + 2016 = 1A02016
and CH is loaded with the contents of memory address
1A00016 + 2016 +116 = 1A02116
Solution:
((DS)0+SUM) (BX)
PA = 1200016 + 123416=1323416
Execution of the instruction performs the following 16-bit swap:
(1323416) (BL)
(1323516) (BH)
So we get (BX) = 00FF16
(SUM) = 11AA16
EXAMPLE
XCHG [SUM], BX
Solution:
0101
1100
1200
11
AA
IP
CS
DS
SS
ES
AX
BX
CX
DX
SP
BP
SI
DI
8088/8086 MPU
Before execution
611 37100 Lecture 05-12
Address Memory
Content
11101
11102
11103
11104
11105
87
1E
34
12
XX
12000
12001
.
.
.
.
13234
13235
XX
XX
FF
00
Instruction
XCHG [SUM],BX
Next instruction
Variable SUM
XCHG [SUM], BX
0105
1100
1200
00
FF
IP
CS
DS
SS
ES
AX
BX
CX
DX
SP
BP
SI
DI
8088/8086 MPU
After execution
611 37100 Lecture 05-13
Address Memory
Content
11101
11102
11103
11104
11105
12000
12001
.
.
.
.
13234
13235
87
1E
34
12
XX
Instruction
XCHG [SUM],BX
Next instruction
Mnemonic
Meaning
Format
Operation
Flags affected
XLAT
Translate
XLAT
((AL)+(BX)+(DS)0) (AL)
None
e.g.
XX
XX
AA
11
Variable SUM
EXAMPLE
Solution:
Meaning
LEA
LEA Reg16, EA
EA (Reg16)
None
LDS
(Mem32) (Reg16)
(Mem32+2) (DS)
None
LES
LES Reg16,Mem32
(Mem32) (Reg16)
(Mem32+2) (ES)
None
e.g.
Format
Operation
Flags affected
EXAMPLE
Solution:
0100
1100
1200
IP
CS
DS
SS
ES
AX
BX
CX
DX
XXXX
SP
BP
SI
DI
Address Memory
Content
11100
11101
11102
11103
11104
C5
36
00
02
XX
12000
12001
.
.
12200
12201
12202
12203
XX
XX
Instruction
Next instruction
20
00
00
13
8088/8086 MPU
Before execution
611 37100 Lecture 05-18
IP
1100
1300
CS
DS
SS
ES
AX
BX
CX
DX
0020
SP
BP
SI
DI
8088/8086 MPU
Address Memory
Content
11100
11101
11102
11103
11104
C5
36
00
02
XX
12000
12001
.
.
12200
12201
12202
12203
.
.
13000
13001
XX
XX
Instruction
Next instruction
20
00
00
13
XX
XX
After execution
611 37100 Lecture 05-19
New data
segment
Addition
Subtraction
Multiplication
Division
Data formats
Unsigned binary bytes
Signed binary bytes
Unsigned binary words
Signed binary words
Unpacked decimal bytes
Packed decimal bytes
ASCII numbers
EXAMPLE
Verify the following instruction using DEBUG program.
LDS SI, [200H]
ADD
ADC
INC
AAA
DAA
SUB
SBB
SUBTRACTION
DEC
NEG
AAS
DAS
MUL
IMUL
AAM
DIV
IDIV
MULTIPLICATION
DIVISION
AAD
CBW
CWD
EXAMPLE
Initializing the internal registers of the 8088 from a table in
memory.
Solution:
MOV AX, [INIT_TABLE]
MOV SS, AX
LDS SI, [INIT_TABLE+02H]
LES DI, [INIT_TABLE+06H]
MOV AX, [INIT_TABLE+0AH]
MOV BX, [INIT_TABLE+0CH]
MOV CX, [INIT_TABLE+0EH]
MOV DX, [INIT_TABLE+10H]
Mnemonic
Meaning
Format
Operation
Flags affected
ADD
Addition
ADD D, S
(S) +(D)(D)
Carry (CF)
ADC
ADC D, S
(S) +(D)+(CF)(D)
Carry (CF)
INC
Increment by 1
INC D
(D) +1(D)
AAA
ASCII adjust
for addition
AAA
AF, CF
OF, SF, ZF, PF undefined
DAA
Decimal adjust
for addition
DAA
Destination
Source
0102
Register
Register
Register
Memory
Memory
Register
Register
Immediate
Reg16
Memory
Immediate
Reg8
1100
1200
CS
DS
SS
ES
1BBC
0ABC
AX
BX
CX
DX
Destination
Memory
Accumulator Immediate
Allowed operands for ADD and
ADC instructions
IP
Address Memory
Content
11100
11101
11102
03
C3
XX
12000
12001
XX
XX
Instruction
ADD AX, BX
Next instruction
SP
BP
SI
DI
Allowed operands
for INC instruction
8088/8086 MPU
After execution
611 37100 Lecture 05-28
EXAMPLE
EXAMPLE
Solution:
Solution:
(BX)+(AX)= 0ABC16 + 110016=1BBC16
The sum ends up in destination register AX. That is
(AX) = 1BBC16
1100
1200
CS
DS
SS
ES
1100
0ABC
AX
BX
CX
DX
Address Memory
Content
11100
11101
11102
03
C3
XX
12000
12001
XX
XX
Instruction
ADD AX, BX
Next instruction
EXAMPLE
The original contents of AX, BL, word-size memory location
SUM, and carry flag (CF) are 123416, AB16, 00CD16, and 016,
respectively. Describe the results of executing the following
sequence of instruction?
ADD AX, [SUM]
ADC BL, 05H
INC WORD PTR [SUM]
Solution:
(AX)(AX)+(SUM) = 123416 + 00CD16 =130116
SP
BP
SI
DI
8088/8086 MPU
Before execution
611 37100 Lecture 05-27
EXAMPLE
Subtraction Instructions:
SUB, SBB, DEC, AAS, DAS, and NEG
Solution:
(AL)(AL)+(BL)= 3216 + 3416=6616
The result after the AAA instruction is
(AL) = 0616
(AH) = 0016
with both AF and CF remain cleared
Source
Register
Register
Memory
Memory
Register
Register
Immediate
Reg16
Memory
Immediate
Reg8
Accumulator Immediate
Allowed operands for SUB and SBB
instructions
Destination
Register
Destination
Memory
Allowed operands
for DEC instruction
Destination
Register
Memory
Allowed operands
for NEG instruction
EXAMPLE
EXAMPLE
Solution:
(DX,CX) (DX,CX)+(BX,AX)
(DX,CX) = FEDCBA9816
(BX,AX) = 0123456716
MOV DX, 0FEDCH
MOV CX, 0BA98H
MOV BX, 01234H
MOV AX, 04567H
ADD CX, AX
ADC DX, BX
Solution:
(BX)-(CX)-(CF) (BX)
We get
(BX) = 123416 012316 016
= 111116
the carry flag remains cleared.
Subtraction Instructions:
SUB, SBB, DEC, AAS, DAS, and NEG
EXAMPLE
Mnemonic
Meaning
Format
Operation
SUB
Subtract
SUB D, S
(D)-(S)(D)
Borrow (CF)
SBB
Subtract with
borrow
SBB D, S
(D)-(S)-(CF)(D)
DEC
Decrement by
1
DEC D
(D)-1(D)
NEG
Negate
NEG D
0-(D) (D)
1 (CF)
DAS
Decimal adjust
for subtraction
DAS
AAS
ASCII adjust
for subtraction
AAS
AF,CF
OF,SF, ZF,PF undefined
Solution:
EXAMPLE
Assuming that the register BX contains 003A16, what is the
result of executing the following instruction?
NEG BX
Mnemonic
Meaning
Format
MUL S
(AL)(S8)(AX)
(AX)(S16)(DX)(AX)
OF, CF
SF,ZF, AF, PF undefined
DIV
Division
(unsigned)
DIV S
(1)Q((AX)/(S8))(AL)
R((AX)/(S8))(AH)
(2)Q((DX,AX)/(S16))(AX)
R((DX,AX)/(S16))(DX)
If Q is FF16 in case (1) or
FFFF16 in case (2), then type 0
Integer multiply
(signed)
IMUL S
(AL)(S8)(AX)
(AX)(S16)(DX)(AX)
OF, CF
SF,ZF, AF, PF undefined
IDIV S
(1)Q((AX)/(S8))(AL)
R((AX)/(S8))(AH)
(2)Q((DX,AX)/(S16))(AX)
R((DX,AX)/(S16))(DX)
If Q is positive and exceeds
7FFF16 or if Q is negative and
become less than 800116, then
type 0 interrupt occurs
interrupt occurs
IMUL
IDIV
Integer divide
(signed)
EXAMPLE
Multiplication Instructions:
Solution:
Flags affected
Multiply
(unsigned)
Solution:
(BX) = 000016-(BX)=000016+2complement of 003A16
= 000016+FFC616
= FFC616
Operation
MUL
Meaning
Format
Operation
Flags affected
AAM
Adjust AL for
multiplication
AAM
Q((AL)/10)(AH)
R((AL)/10)(AL)
SF,ZF,PF
OF,AF,CF undefined
AAD
Adjust AX for
division
AAD
(AH)10+(AL)(AL)
00(AH)
SF,ZF,PF
OF,AF,CF undefined
CBW
Convert byte to
word
CBW
CWD
Convert word
to double word
CWD
None
Destination
Reg8
Reg16
Mem8
Mem16
Allowed operands
EXAMPLE
EXAMPLE
Solution:
MOV SI, 200H
MOV DI, 100H
MOV AX, [SI]
SUB AX, [DI]
MOV [SI],AX
MOV AX, [SI]+2
SBB AX, [DI]+2
MOV [SI]+2, AX
Solution:
(AL) = -1 (as 2s complement) = 111111112 = FF16
(CL) = -2 (as 2s complement) = 111111102 = FE16
Executing the MUL instruction gives
(AX) = 111111112x111111102=11111101000000102=FD0216
Executing the IMUL instruction gives
(AX) = -116 x -216 = 216 = 000216
EXAMPLE
Verify the previous example using DEBUG program.
Destination
Solution:
Source
Register
Register
Register
Memory
Memory
Register
Register
Immediate
Memory
Immediate
Register
Accumulator Immediate
Memory
EXAMPLE
EXAMPLE
Allowed operands
for NOT instruction
AL, 01010101B
AL, 00011111B
AL, 11000000B
AL, 00001111B
AL
Solution:
Solution:
(AL) = A116 = 101000012
Executing the CBW instruction extends the MSB of AL
(AH) = 111111112 = FF16 or (AX) = 11111111101000012
Executing the CWD instruction, we get
(DX) = 11111111111111112 = FFFF16
That is,
(AX) = FFA116 (DX) = FFFF16
EXAMPLE
AND
OR
XOR (Exclusive-OR)
NOT
Mnemonic
Destination
Meaning
Solution:
Mask off the upper 12 bits of the word of data in AX
AND AX, 000F16
Format
Operation
Flags affected
AND
Logical AND
AND D, S
(S) (D)(D)
OR
Logical
Inclusive-OR
OR D, S
(S) (D)(D)
XOR
Logical
exclusive-OR
XOR D, S
(S)(D)(D)
NOT
Logical NOT
NOT D
(NOT D)(D)
None
Meaning
Format
Operation
Flags
affected
SAL/SHL
Shift
arithmetic left /
Shift logical
left
SAL D, Count
SHL D, Count
SHR
Shift logical
right
SHR D, Count
SAR
Shift
arithmetic
right
SAR D, Count
EXAMPLE
Assume that CL contains 0216 and AX contains 091A16.
Determine the new contents of AX and the carry flag after the
instruction SAR AX, CL is executed.
Solution:
(AX)=00000010010001102=024616
and the carry flag is (CF)=12
EXAMPLE
Verify the previous example using DEBUG program.
Destination
Solution:
Count
Register
Register
CL
Memory
Memory
CL
EXAMPLE
Isolate the bit B3 of the byte at the offset address
CONTROL_FLAGS.
Solution:
SHR AX, CL
SAR AX, CL
Meaning
Rotate left
Format
Operation
Flags
affected
ROL D, Count
CF
OF undefined
if count 1
ROR
Rotate right
ROR D, Count
CF
OF undefined
if count 1
RCL
Rotate left
through
carry
RCL D, Count
CF
OF undefined
if count 1
RCR
Rotate right
through
carry
RCR D, Count
CF
OF undefined
if count 1
Register
CL
Memory
Memory
CL
EXAMPLE
What is the result in BX and CF after execution of the following
instructions?
RCR BX, CL
Assume that, prior to execution of the instruction, (CL)=0416,
(BX)=123416, and (CF)=0
Count
Register
Solution:
The original contents of BX are
(BX) = 00010010001101002 = 123416
Execution of the RCR command causes a 4-bit rotate right
through carry to take place on the data in BX, the results are
(BX) = 10000001001000112 = 812316
(CF) = 02
For RCL, RCR, the bits are rotate through the carry flag
Destination
EXAMPLE
Verify the previous example using DEBUG program.
ROL AX, 1
Solution:
ROR AX, CL
(CL)=4
10
Solution:
MOV
MOV
MOV
ROR
AND
AND
ADD
AL, [HEX_DIGITS]
BL, AL
CL, 04H
BL, CL
AL, 0FH
BL, 0FH
AL, BL
11