MP02 - Insruction Set 1
MP02 - Insruction Set 1
Outline
References
1
1 8086 Addressing Modes
ä Addressing modes are the various methods used to specify the operand. It
define how the processor calculates the effective address of an operand.
1. Immediate.
2. Register.
3. Memory.
4. Implied
2
ã Example
MOV DH, [21F3]
MOV [0100], AX
ã it is not flexible.
ã The address of memory operand is stored in one of the index or base regis-
ters (SI, DI, BX).
MOV CX, [BX]
ADD [SI], AL
ã This mode is not efficient for variables having a single element.
ã This mode generates the address by taking the sum of Base register + Index
register + offset.
ä When memory addressing mode is used, the execution time of the instruction
is longer because one/more memory access is required. If memory operand is
destination, it requires two memory access; one to read operand and another to
save the result.
ADD AL, [100] ; requires one memory access
ADD [100], AL ; requires two memory access
3
ä The default segment register is DS for direct,SI,DI,BX
ä Instruction set of 8086 can be divided into a number of groups depending on the
type of operation as shown in the table below.
Group Instructions
4
ä The effect of these instructions on the flag register is not the same. The instruc-
tion may affect some or all flags, while others do not affect flags at all.
R ⇐ data
M ⇐ data
R ⇐⇒ M
Example 1 - write a program to copy the content of memory locations 0200h into
0201h.
5
Or
Instruction AX DX
1111 FFFF
XCHG AX, DX FFFF 1111
Example 3 - two words are stored consecutively in memory locations 250h - 253h.
Write a sequence of instructions to exchange their contents with each other.
XLAT [BX+AL] ⇒ AL
ä XLAT instruction performs the direct table lookup technique often used to convert
one code to another.
ä The primary purpose of the XLAT instruction is to perform a one-byte table lookup
using the AL register as an index.
2000:500 00H, 01H, 04H, 09H, 10H, 19H, 24H, 31H, 40H, 51H
6
Stack memory
ä The stack is viewed as 32 K words (16 bit data can be read or written).
ä The bottom of stack is the last memory location in the stack (SS:FFFE)
PUSH S S ⇒ [SP]
1. SP = SP - 2 ; SP = F008 - 2 = F006
POP D [SP] ⇒ D
2. SP = SP + 2 ; SP = F008
4 Arithmetic Instructions
7
4.1 Addition and Subtraction Instructions
Instruction O S Z A P C
ADD, ADC, SUB, SBB, NEG, CMP x x x x x x
INC, DEC x x x x x
MUL, IMUL x ? ? ? ? x
DIV, IDIV ? ? ? ? ? ?
ADD D, S D = D + S
ADC D, S D = D + S + CF
INC D D=D+1
SUB D, S D=D-S
SBB D, S D = D - S - CF
CMP D, S performs D - S without saving the result in D. Only update flags
NEG D D = 0 - D = -D
DEC D D=D-1
MOV [SI+5], AX
HLT
Example 8 - write a program to compute the result of adding 8 bit with 16 bits
numbers. The numbers are stored in memory locations 0100h - 0102h respectively.
Store the result in memory locations starting at address 0103h.
8
MOV BL, 00
ADD AL, [SI]
ADC AH, 00
ADC BL, 00
MOV [SI+1], AX
MOV [SI+3], BL
MOV AH, 00
SBB AH, 00
MOV [SI+2], AX
(8 bit) MUL S AX = Al ∗ S
ä MUL and IMUL affect only CF and OF flags. Other flags are undefined.
CF = OF = 0 if high order of the result is zero.
ä There are two type of division operations: unsigned (DIV) and signed (IDIV).
ä In the case of memory operand, memory pointer (byte ptr and word ptr) should
be used to specify the size of operand.
9
MUL byte ptr[SI] 8 bit AL * M8
MUL word ptr[SI] 16 bit AX * M16
DIV byte ptr[SI] 8 bit AX/M8
MUL word ptr[SI] 16 bit DX AX / M16
MUL BL
AX = 000F
CF = OF = 0 because AH = 0
Example 11 -
unsigned signed
AL = FF 255 -1
BL = FE 254 -2
AX CF = OF
Unsigned MUL BL FD02h = 64770 1
Signed IMUL BL 0002h 0
MOV [SI+100], AX
Y = X 2 − 3X + 5
Store the result in memory locations starting at address 201h. Assume X is stored in
memory location 200h
10
MOV AL, 03
MUL BL ; AX = 3X
XCHG AX, BX ; AH = ?, AL = X, BX = 3X
MUL AL ; AX = X 2
SUB AX, BX ; AX = X 2 - 3X
MOV [SI+1], AX
Number extension
ä Converting 8 bit operand to 16 bit is performed differently for signed and un-
signed numbers.
ä Examples
16 bit-unsigned 16 bit-signed
AL = FF AX = 00FF AX = FFFF
AX = FFFF DX AX = 0000 FFFF DXAX = FFFFFFFF
CBW AL = 80h ⇒ AH = FF
AL = 0F ⇒ AH = 00
16
Example 14 - write a program to compute
−3
11
MOV AL, 10 ; AL = 16
CBW ; AX = 0016h
MOV BL, 03
NEG BL BL = -3 = FDh
IDIV BL AL = FB = -5 , AH = 1
3X 2 + X
X +2
Assume X is stored in memory location 200h. Store the result in memory locations
starting at address 201h.
MOV BL, AL ; BL = X
MOV BH, 00
MUL AL ; AX = X 2
MUL DX ; DX AX = 3 X 2
ADD AX, BX
3X 2 + X
DIV BX ; AX =
X +2
MOV [201], AX
Homework 3 - the speed and time of a car is shown below. Write a program to
compute the average speed of the car.
12
5 60
2 100
5 Logic instructions
Instruction O S Z A P C
AND, OR, XOR, TEST 0 x x ? x 0
NOT
y = A + B⊕C
XOR AL,[SI+2] ; AL = B ⊕ C
OR AL, [SI]
NOT AL
13
6 Shift and Rotate Instructions
ä Each instruction can shift/rotate the content of operand by one or by the count
specified in CL register
SHL AL, 1
ROR BX, CL
14
Example 18 -
1. Intrasegment the transfer is within the same segment (only change IP)
2. Intersegment transfer to another segment (change IP and CS)
1. JUMP instructions
2. CALL instruction
ä Jump instructions allow the programmer to skip a section of the program and
jump to any part of the memory to execute next instruction.
15
1. Uncondition jump execute next instruction indexed by the addr
JMP addr
JMP 1000 intrasegment jump
JMP 2000:0500 intersegment jump
2. Conditional jump execute next instruction indexed by the addr if some
condition is verified
Jcc addr
ä The control will be transferred to a new address if a particular flag satisfies the
condition.
16
MOV CX, 21
XOR AX, AX ; AX = 0000
MOV DI, 500
NEXT: ADD AL, [DI] ; AL = AL + X
ADC AH, 00 ; AH = AH + CF
INC DI
DEC CX
JNZ NEXT
MOV [600], AX
HLT
LOOP instruction
ä LOOP decrements CX
MOV CX, 21
MOV BL, 00
MOV SI, 500
NEXT1: MOV AL, [SI]
TEST AL, 80
JS NEXT
INC BL
NEXT2: INC SI
LOOP NEXT1
HLT
MOV CX, 51
17
MOV DL, 00
MOV SI, 400
NEXT1: MOV AL, [SI]
AND AL, AL
JNZ NEXT2
INC DL
NEXT2: INC SI
LOOP NEXT1
HLT
Example 25 - write a program to shift the content of a table of 10 bytes one byte
down.
18
MOV SI, 0108
MOV CX, 0009
NEXT: MOV AL, [SI] AL = x
MOV [SI+1], AL ; load first location in the table with 00
INC SI
LOOP NEXT
MOV byte ptr [SI] , 00
HLT
if (x < y) {
x=x+1;
y=y-1;
}
...
19
Homework 5 - write a program to count positive numbers in memory range
0500h - 0520h.
Homework 7 - (optional)
2. Write a program to compute the sum of numbers greater than 31h in a table of
30 bytes.
5. Write a program to compute the sum of odd numbers in the range 00 - FF.
ä It is called from main program and return back after the processor executes last
instruction in the procedure.
20
2. The starting address of the procedure (addr) is loaded into IP.
3. The processor starts the execution of the procedure until it reaches RET
instruction.
4. The processor executes POP IP return back to the main program
ä Far CALL usually used for global procedures, while local procedure uses near call.
y = x2 − 5x + 3
21
8 String Instructions
ä 8086 processor supports 5 different string instructions which are divided into two
groups: move and compare.
22
Unconditional repeat REP string-instruction repeat while CX ̸= 0
Conditional repeat REPE / REPZ string-instruction repeat while CX ̸= 0 and ZF =
REPNE / REPNZ string-instruction repeat while CX ̸= 0 and ZF =
ä REP STOSB is the fastest way to initialize a large block of memory. Also you can
use REP STOSW which is much faster.
Example 28 - write a program to move a block of data (100 bytes) from memory
address 2000:0100 to 3000: 0500.
23
MOV AX, 3000h
MOV ES, AX
MOV DI, 0500h
CLD
MOV CX, 0064
REP MOVSB
HLT
Modify this program to load the segment with the following sequence 00, 01, ... , FF,
00, ....
24
MOV AX, 1000h
MOV DS, AX
MOV SI, 0
MOV AX, 2000h
MOV ES, AX
MOV DI, 0
CLD
MOV CX, 0
MOV DX, 0
NEXT1: CMPSB
JNZ NEXT2
INC DX
NEXT2: LOOP NEXT1
HLT
Example 31 - write a program to copy the content of segment 2000 into segment
3000 in reverse order.
Homework 8 -
1. Write a program to move the first 100 bytes from segment 1000h to segment
2000h after squaring each element. Use string instructions.
2. Write a program to count the number of memory locations that contain 0Fh in
segment 1000 using string instructions.
25
9 BCD Arithmetic Instructions
ä Each digit is represented by a binary code of four bits (0 - 9). The letters A - F
are not available.
1. Packed BCD stores each digit in four bits. Thus, two decimal digits can be
packed into a byte. This reduces the memory requirement by half compared
to the unpacked BCD.
1234 ⇒ 12 34
2. Unpacked BCD stores one decimal digit in a byte. The most significant 4
bits of each byte is 0.
1234 ⇒ 01 02 03 04
DAA - decimal adjust after addition. DAS - decimal adjust after subtraction.
MOV AL, 71 MOV AL, 71
ADD AL, 43 ; AL = B4h SUB AL, 43 ; AL = 2Eh
DAA ; AL = 14H and CF = 1 DAS ; AL = 28h
Four instructions are used to correct the result of arithmetic operations applied on
unpacked BCD numbers:
26
AAA ASCII adjust after addition AAM ASCII adjust after multiplication
MOV AX, 0009 MOV AL, 5
ADD AL, 01; AL = 0A MOV CL, 3
AAA ; AX = 0100 MUL CL ; AX = 000F
AAM ; AX = 0105
AAS ASCII adjust after subtraction AAD ASCII adjust before division
MOV AX, 0009 ; AL = 09 MOV BL, 9
SUB AL, 05 MOV AX, 0702
AAS ; AX = 0004 AAD ; AX = 0048h
DIV BL ; AX = 0008
Example 32 - assume BX = 3099 and DX = 1234. Add the numbers and store
the result in BCD format.
MOV BX, 3099
MOV DX, 1234
MOV AL, BL ; AL = 99
ADD AL, DL ; AL = 99 + 34 = CDh
DAA ; AL = 33 , CF = 1
MOV CL, AL
MOV AL, BH ; AL = 30
ADC AL, DH ; AL = 30 + 12 + CF = 43h
DAA ; AL = 43 , CF = 0
MOV CH, AL ; CX = 4333
27
MOV AX, 00FFh
MOV DL, 64h ; DL = 100
DIV DL ; AX = 3702h ===> remainder = 37h = 55d, Quote = 02
MOV DL, AH ; DL = 37h
AAM ; AX = 0002
XCHG AX, DX ; AL = 37h , DX = 0002
AAM ; AX = 0505 ==> DX AX = 00 02 05 05
RET
ä The first 128 items in ASCII table called standard ASCII and the represent
different English letters, number, and keyboard commands like ESC, CTRL, etc.
ä The first 32 codes in standard ASCII are non graphic commands (never printed)
and used for control purpose such as Ins, Esc, Enter, del, shift, etc.
ä The second half of the table is called extended ASCII (80h - FF). They are used
to store the code of non English symbols and letters.
28
ä Each hexadecimal digit is represented by a unique ASCII code as shown in table
below:
Number 0 1 2 3 4 5 ... 9
ASCII code 30h 31h 32h 33h 34h 35h ... 39h
ä When a program read a number from the keyboard, each digit is represented by
a unique ASCII code (byte). If you press keys 1 and 2 the program receives two
bytes 31h and 32h from the keyboard.
ä The result should be converted back into ASCII code before we can display them
on the screen or send them to the printer.
29
Small letter 41h - 46h code - 07 - 30h
Numeric 30h - 39h code - 30h
CMP AL, 61
JB NEXT1
SUB AL, 57h
JMP, NEXT3
NEXT1: CMP AL, 41
JB NEXT2
SUB AL, 37h
JMP, NEXT3
NEXT2: SUB AL, 30
NEXT3: ...
CMP AL, 61
JAE NEXT1
CMP AL, 41
JAE, NEXT2
JMP, NEXT3
NEXT1: SUB AL, 20
NEXT2: SUB AL, 7
NEXT3: SUB AL, 30
2000:500 30h, 31h, 32h, 33h, 34h, 35h, 36h, 37h, 38h, 39h, 41h, 42h, 43h, 44h,
45h, 46h
30
STC Set carry flag
CLC Clear the carry flag
CMC Complement the carry flag
CLD Clear the direction flag
STD Set direction flag
LAHF Load flags into AH register
SAHF Store AH register into flags
PUSHF Push FLAGS onto stack
POPF Pop FLAGS from stack
STI Set interrupt flag
CLI Clear the interrupt flag
31