Unit II
Instruction set And Addressing Modes
Data Transfer Instructions - XCHG
Mnemonic Meaning Format Operation Flags
Affected
XCHG Exchange XCHG D,S (D) (S) None
Destination Source
Accumulator Reg16
Memory Register
Register Register
Register Memory
Example. XCHG [1234h], BX
2
Data Transfer Instructions – LEA, LDS,
LES
An important type of data transfer operation is loading a segment
and a general purpose register with an address directly from memory
Mnemonic Meaning Format Operation Flags
Affected
LEA Load Effective LEA Reg16,EA EA (Reg16) None
Address
LDS Load Register and LDS Reg16, (Mem32) None
DS MEM32 (Reg16)
(Mem32 + 2)
(DS)
LES Load Register and LES Reg16, (Mem32) None
ES MEM32 (Reg16)
(Mem32 + 2)
(ES)
Example.
3 LEA SI, DATA or MOV SI, OFFSET DATA
Arithmetic
Mnemonic
Instructions
Meaning
– ADD,Operation
Format
ADC, INC, AAA, DAA
Flags
Affected
ADD Addition ADD D, S (S) + (D) All
(D)
Carry
(CF)
ADC Add with ADC D, S (S) + (D) + All
carry (CF) (D)
Carry
(CF)
INC Increment INC D (D) + 1 All but the
by one (D) carry flag
AAA ASCII AAA
adjust for
addition
DAA Decimal DAA
adjust for
addition
4
Examples
Ex. 1 ADD AX, 2
ADC AX, 2
Ex. 2 AL contains 32 (ASCII code for number 2)
BL contains 34 (ASCII code for number 4)
MOV AH,0
ADD AL, BL
AAA
; has to be adjusted via AL and only an addition instruction
Ex. 3 AL contains 25 (packed BCD)
BL contains 56 (packed BCD)
ADD AL, BL
5
DAA
Arithmetic
Mnemonic
Instructions
Meaning
–Format
SUB, SBB,Operation
DEC, AAS, DAS, NEG
Flags Affected
SUB Subtract SUB D, S (D) - (S) All
(D)
Borrow
(CF)
SBB Subtract with SBB D, S (D) - (S) - All
borrow (CF) (D)
DEC Decrement DEC D (D) - 1 (D) All but the
by one carry flag
NEG Negate NEG D
DAS Decimal DAS
adjust for
subtraction
AAS ASCII adjust AAS
for
subtraction
6
Example
32-bit subtraction of two 32 bit numbers X and Y that are
stored in the memory as
X = (DS:203h)(DS:202h)(DS:201h)(DS:200h)
Y = (DS:103h)(DS:102h)(DS:101h)(DS:100h)
The result X - Y to be stored where X is saved in the
memory
MOV SI, 200h
MOV DI, 100h
MOV AX, [SI]
SUB AX, [DI]
MOV [SI], AX ;save the LS word of result
MOV AX, [SI] +2
SBB AX, [DI]+2
MOV [SI] +2, AX
Ex. 12 34 56 78 – 23 45 67 89 = EF EE EE EE
7
Multiplication and Division
Multiplication Multiplicant Operand Result
(MUL or IMUL) (Multiplier)
Byte * Byte AL Register or AX
memory
Word * Word AX Register or DX :AX
memory
Dword * Dword EAX Register or EDX :EAX
Memory
Division Dividend Operand Quotient : Remainder
(DIV or IDIV) (Divisor)
Word / Byte AX Register or memory AL : AH
Dword / Word DX:AX Register or memory AX : DX
Qword / Dword EDX: EAX Register or Memory EAX : EDX
8
AAM, AAD, CBW, CWD
AAM: Adjust AX for multiply
AAD: Adjust AX for divide
MOV AL,07 ; first unpacked number
MUL AL, 09 ; second unpacked number
AAM ;AX should have 06 03
Division instructions can also be used to divide an 8 bit
dividend in AL by an 8 bit divisor.
In order to do so, the sign of the dividend must be extended to fill the AX
register
AH is filled with zeros if AL is positive
AH is filled with ones if the number in AL is negative
Automatically done by executing the CBW (convert byte
to word) instruction
CWD (convert word to double word)
Ex. MOV AL, 0A1h
CBW
CWD
9
Example
• Write a program that calculates the average of five temperatures and writes the
result in AX
DATA DB +13,-10,+19,+14,-18 ;0d,f6,13,0e,ee
MOV CX,5 ;LOAD COUNTER
SUB BX, BX ;CLEAR BX,
MOV SI, OFFSET DATA ;SET UP POINTER
BACK: MOV AL,[SI] ;MOVE BYTE INTO AL
CBW ;SIGN EXTEND INTO AX
ADD BX, AX ;ADD TO BX
INC SI ;INCREMENT POINTER
DEC CX ;DECREMENT COUNTER
JNZ BACK ;LOOP IF NOT FINISHED
MOV AL,5 ;MOVE COUNT TO AL
CBW ;SIGN EXTEND INTO AX
MOV CX,AX ;SAVE DENOMINATOR IN CX
MOV AX,BX ;MOVE SUM TO AX
CWD ;SIGN EXTEND THE SUM
IDIV CX ;FIND THE AVERAGE
10
Compare (CMP)
For ex: CMP CL,BL ; CL-BL; no modification on neither operands
Write a program to find the highest among 5 grades and write it in DL
DATA DB 51, 44, 99, 88, 80 ;13h,2ch,63h,58h,50h
MOV CX,5 ;set up loop counter
MOV BX, OFFSET DATA ;BX points to GRADE data
SUB AL,AL ;AL holds highest grade found so
; far
AGAIN: CMP AL,[BX] ;compare next grade to highest
JA NEXT ;jump if AL still highest
MOV AL,[BX] ;else AL holds new highest
NEXT: INC BX ;point to next grade
LOOP AGAIN ;continue search
MOV DL, AL
11
Logical Instructions
AND
Uses any addressing mode except memory-to-memory and segment registers
Especially used in clearing certain bits (masking)
xxxx xxxx AND 0000 1111 = 0000 xxxx (clear the first four bits)
Examples: AND BL, 0FH; AND AL, [345H]
OR
Used in setting certain bits
xxxx xxxx OR 0000 1111 = xxxx 1111
XOR
Used in inverting bits
xxxx xxxx XOR 0000 1111 = xxxx yyyy
Ex. Clear b’ts 0 and 1, set bits 6 and 7, invert bit 5
AND CX, OFCH 1111 1100
OR CX, 0C0H 1100 0000
XOR CX, 020H 0010 0000
12
Shift Target register or memory
C
0
SHL
C
SAL 0
SHR 0
C
SAR
Sign Bit
13
Examples
Examples SHL AX,1
SAL DATA1, CL ; shift count is a modulo-32 count
Ex. ; Multiply AX by 10
SHL AX, 1
MOV BX, AX
MOV CL,2
SHL AX,CL
ADD AX, BX
Ex. What are the results of SAR CL, 1 if CL initially contains B6H?
Ex. What are the results of SHL AL, CL if AL contains 75H
and CL contains 3?
14
Rotate
C Target register or memory
RCL
C
ROL
C
RCR
ROR
Ex. What is the result of ROL byte ptr [SI], 1 if memory location 3C020
contains 41H?
15
Example
Write a program that counts the number of 1’s in a
byte and writes it into BL
DATA1 DB 97 ; 61h
SUB BL,BL ;clear BL to keep the number of 1s
MOV DL,8 ;rotate total of 8 times
MOV AL,DATA1
AGAIN: ROL AL,1 ;rotate it once
JNC NEXT ;check for 1
INC BL ;if CF=1 then add one to count
NEXT: DEC DL ;go through this 8 times
JNZ AGAIN ;if not finished go back
NOP
16
Addressing Modes
When the 8088 executes an instruction, it performs the specified function on data
These data, called operands,
May be a part of the instruction
May reside in one of the internal registers
May be stored in memory
To access these operands, 8088 is provided with various addressing modes
An addressing mode is a method of specifying an operand
Register Operand Addressing
MOV AX, BX
Immediate Operand Addressing Mode
MOV AL,15h
Memory Addressing Modes
One must calculate the PA (physical address)
Register Operand Addressing
MOV AX, BX
C38B16
Before execution
After execution
Immediate Addressing Mode
In immediate addressing, source operand is a constant
Used to load information to some registers
– MOV AX,2550h
– MOV CX, 625 ; decimal 625
– MOV BL,40h
MOV AL, 15H
15B016
Direct Addressing Mode
MOV CX, [1234h]
12340E8B16
Register Indirect Addressing Mode
MOV AX, [SI]
048B16
Based Addressing Mode
• MOV CX,[BX]+10 PA = (DS)0 + (BX) + 10
• MOV AL,[BP]+5 PA = (SS)0 + (BP) + 5
MOV [BX]+1234H,AL
1234878816
Indexed Addressing Mode
• MOV DX,[SI]+5 PA= (DS) 0 + (SI) + 5
• MOV CL,[DI]+20 PA= (DS) 0 + DI + 20
MOV AL, [SI] +1234H
1234848A16
Based Indexed Addressing Mode
MOV CX,[BX][DI]+8h PA= (DS)0 + (BX) + (DI) + 8h
MOV AH,[BP][SI]+29 PA: (SS)0 + (BP) + (SI) + 2
Other notations possible
– MOV AH,[BP+SI+29]
or
– MOV AH, [SI+BP+29]
However
MOV AX, [SI][DI] + displacement is illegal
Example: Based-Indexed Addressing Modes
MOV AH, [BX][SI] +1234H
1234A08A16
Flag Control Instructions
SF ZF AF PF CF
LAHF Load AH from flags (AH) (Flags)
SAHF Store AH into flags (Flags) (AH)
Flags affected: SF, ZF, AF, PF, CF
CLC Clear Carry Flag (CF) 0
STC Set Carry Flag (CF) 1
CLI Clear Interrupt Flag (IF) 0
STI Set interrupt flag (IF) 1
Example (try with debug)
LAHF
MOV [MEM1], AH
MOV ah, [MEM2]
SAHF
; MEM1 = 150 (FF) MEM2 = 151 (01)
38
Jump Instructions
Unconditional vs
conditional jump
Example. JMP [BX]
Assume BX = 1000h
39
Ds:1000 = 200h
Compare
Unsigned Comparison Signed Comparison
Comp CF ZF Comp ZF SF,OF
Operands Operands
Dest > 0 0 Dest > 0 SF=OF
source source
Dest = 0 1 Dest = 1 ?
source source
Dest < 1 0 Dest < ? SF<>OF
source source
40
Compare Example
DATA1 DW 235Fh
…
MOV AX, CCCCH
CMP AX, DATA1
JNC OVER
SUB AX,AX
OVER: INC DATA1
CCCC – 235F = A96D => Z=0, CF=0 =>
CCCC > DATA1
41
Subroutines and Subroutine Handling Functions
A subroutine is a special segment of a program that can be
called for execution from any point in the program
A RET instruction must be included at the end of the
subroutine to initiate the return sequence to the main program
environment
Examples. Call 1234h
Call BX
Call [BX]
Two calls
•intrasegment
•intersegment
42
43
Calling a NEAR proc
The CALL instruction and the subroutine it calls are in
the same segment.
Save the current value of the IP on the stack.
Then load the subroutine’s offset into IP.
Calling Program Subroutine Stack
Main proc sub1 proc
001A: call sub1 0080: mov ax,1
001D: inc ax … 1ffd 1D
. ret 1ffe 00
Main endp sub1 endp 1fff (not used)
44
Calling a FAR proc
The CALL instruction and the subroutine it calls are in
the “Different” segments.
Save the current value of the CS and IP on the stack.
Then load the subroutine’s CS and offset into IP.
Calling Program Subroutine Stack
Main proc sub1 proc 1ffb CB
1FCB:001A: call sub1 4EFA:0080: 1ffc 1F
mov ax,1 1ffd 1D
1FCB:001D: inc ax …. 1ffe 00
… …
1fff N/A
45
… ret
Main endp sub1 endp
Push and Pop Instructions
Push S (16/32 bit or Mem)
(SP) (SP) -2
((SP)) (S)
Pop D (16/32 bit or Mem)
(D) ((SP))
(SP) (SP) + 2
46
Loop and Loop Handling Instructions
47
Loop
48
Nested Loops
Nested Loops
MOV CX,A
MOV CX,A OUTER: PUSH
NOP CX
BACK: … MOV CX, 99
… INNER: NOP
… …
… …
LOOP BACK …
LOOP INNER
NOP CX
POP
LOOP OUTER
49
String Instructions
80x86 is equipped with special instructions to handle string
operations
String: A series of data words (or bytes) that reside in
consecutive memory locations
Operations: move, scan, compare
String Instruction:
Byte transfer, SI or DI increment or decrement by 1
Word transfer, SI or DI increment or decrement by 2
DWord transfer, SI or DI increment or decrement by 4
50
String Instructions - D Flag
The Direction Flag: Selects the auto increment D=0 or
the auto decrement D=1 operation for the DI and SI registers
during string operations. D is used only with strings
CLD Clears the D flag / STD Sets the D flag
51
String Instructions
52
Repeat String REP
Basic string operations must be repeated in order
to process arrays of data; this is done by inserting a
repeat prefix.
53