Module - 1 - 8086 - Instruction - Set
Module - 1 - 8086 - Instruction - Set
1
Instruction sets of 8086
VIT University
Jasmin T Jose Asst. professor
3. Logical Instructions
4. Flags controlling Instructions
5. Branching instructions
6. I/O instructions
7. Stack instructions
8. String Instructions
SCSE,
2
Data transfer Instructions
• Transferring 8/16 bit data from source to destination.
• Size of the source and destination should be same.
MOV D, S- Move source data to destination.
VIT University
Jasmin T Jose Asst. professor
Possible combination of source and destination is,
Source Destination
Register Register
Register Memory
Memory Register
Data Reg/mem
Seg. Reg Reg/Mem
Reg/Mem Seg reg except CS
SCSE,
LAHF - Load AH with 8 LSBs of Flag register.
SAHF - Store AH into 8 LSBs of Flag register.
3
Data transfer Instructions
XCHG D, S- Exchange the content of source and destination.
Possible combination of source and destination is,
Source Destination
VIT University
Jasmin T Jose Asst. professor
Register Register
Register Memory
Eg. XCHG AL,BL XCHG CX, [7800h]
XCHG BX, 8000h not possible.
LDS RX, Mem – Load DS and the given Register with memory
content.
Eg. LDS SI, ES:[BX]
SCSE,
LES RX, Mem – Load ES and given register with memory content.
LEA RX, EA source - Load EA of memory into given register.
4
Arithmetic Instructions- Addition
ADD D, S –
Possible combinations are,
Destination Source
Reg Reg
VIT University
Jasmin T Jose Asst. professor
Reg Mem
Mem Reg
Reg/Mem data
ADC D, S- Add D and S with carry
SCSE,
5
Arithmetic Instructions
? Write a program to add two 32 bit numbers present in registers
AX-BX and SI-DI. Store the result and carry in memory from
ES: 2000h.
VIT University
Jasmin T Jose Asst. professor
ADD BX, DI
MOV ES:[2000H],
BX
ADC AX, SI
MOV ES:[2002H],
AX
SCSE,
MOV AL, 00H
ADC AL,AL
MOV ES:[2004], AL
6
HLT
Arithmetic Instructions
VIT University
Jasmin T Jose Asst. professor
MOV AX, [BX]
ADD [SI], AX
MOV AX, [BX+2]
ADC [SI+2], AX
MOV AX, [BX+4]
ADC [SI+4], AX
SCSE,
MOV AL, 00H
ADC AL,AL
MOV [SI+6], AL
HLT
7
Arithmetic Instructions
DAA – Decimal adjust after addition.
Only 8bit addition is possible.
One data should be in AL register.
VIT University
Jasmin T Jose Asst. professor
? Write a program to add two BCD numbers 45 and 57, store the
result and carry in register DL and DH respectively.
MOV AL, 45H
ADD AL, 57H
DAA
MOV AH, 00H
ADC AH, AH
SCSE,
MOV DX, AX
HLT
8
Arithmetic Instructions
AAA – ASCII adjust after addition.
Only 8bit addition is possible.
One data should be in AL register.
Make AH as 00.
VIT University
Jasmin T Jose Asst. professor
After addition,
If LSB nibble of AL > 9 or AC flag is 1 then
Add 6 to LSB nibble.
MSB nibble of AL = 0.
If AC=1 then AH = 01
SCSE,
9
Arithmetic Instructions -Subtraction
SUB D, S – Subtract source data from destination data.
Result will be in Destination .
SBB D, S- Subtract source data with borrow from destination data.
D = D - S – CF
VIT University
Jasmin T Jose Asst. professor
? Write a program to subtract 32 bit number present in register AX-
BX from 32 bit number present in register CX-DX. Store the result
in register SI-DI.
SCSE,
10
Arithmetic Instructions
DAS – Decimal adjust after subtraction.
Only 8bit addition is possible.
One data should be in AL register.
After subtraction,
VIT University
Jasmin T Jose Asst. professor
If AC = 1, subtract 6 from LSB
If CF = 1, subtract 6 from MSB
? Write a program to perform the subtraction of two decimal
numbers 73D and 28D, store the result in BCD format in
register AH.
AAS – ASCII adjust after subtraction.
SCSE,
Only 8bit subtraction is possible.
One data should be in AL register.
11
Arithmetic Instructions
CMP D, S – Compare source data and destination data.
Flags will be affected.
If D > S, then CF=0 and ZF=0
If D < S, then CF=1 and ZF=0
VIT University
Jasmin T Jose Asst. professor
If D = S, then CF=0 and ZF=1
INC D – Increment the content of destination. D R/M, not data
DEC D – Decrement the content of destination. D R/M, not data
NEG D - Negate the content of destination. D R/M, not data
2’s complement of the destination data.
Only 8bit is possible.
SCSE,
CBW – Convert the Sign Byte into Sign Word. Result will be in AX. Sign
bit will be added to the AH register.
CWD – Convert sign word into sign Double Word. Result will be in DX-
12
AX. Sign bit will be added to the DX register.
Arithmetic Instructions- Multiplication
Unsigned number
Multiplication of source data with Accumulator in equal length.
Source can be register or memory, not data.
MUL S – 8 bit multiplication. Result will be 16 bit.
VIT University
Jasmin T Jose Asst. professor
AX = AL * S
If AH = 00h, then OF=CF=0
Else OF=CF=1
MUL S – 16 bit multiplication. Result will be 32bit.
DX AX = AX * S(16bit).
If DX = 0000h, then OF=CF=0
SCSE,
Else OF=CF=1
? Write a program to perform multiplication of 1Dh and 156Eh. Store the
result in memory DS:SI.
13
Arithmetic Instructions- Multiplication
Signed number
IMUL S – 8 bit multiplication. Result will be 16 bit.
AX = AL * S(8bit).
If signed bit of AL is 0/1 and the number in AH is 00/FF
VIT University
Jasmin T Jose Asst. professor
respectively, then the AH of the result is neglected.
In such case, OF=CF=0.
Else OF=CF=1, total AX is the result.
IMUL S – 16 bit multiplication. Result will be 32bit.
If signed bit of AX is 0/1 and the number in DX is 0000/FFFF
respectively, then the DX of the result is neglected.
In such case, OF=CF=0.
SCSE,
Else OF=CF=1, total DX AX is the result.
? Write a program to perform multiplication of 15h and -76h. Store the
result in BX.
14
:Use NEG instruction
AAM - ASCII adjust after multiplication.
Arithmetic Instructions- Division
Unsigned number
Default source( Dividend) is Accumulator. Divisor will be in any
register or memory.
Source can be register or memory, not data.
VIT University
Jasmin T Jose Asst. professor
DIV S – 16/8 bit division.
AX/S Quotient is obtained in AL and remainder in AH.
DIV S – 32/16 bit Division.
DX AX/S Quotient is obtained in AX and remainder in DX.
SCSE,
memory DS:DI.
15
Arithmetic Instructions- Division
Signed number
Default source( Dividend) is Accumulator. Divisor will be in any
register or memory.
Both Dividend and divisor should be signed numbers, and in 2’s
VIT University
Jasmin T Jose Asst. professor
complement form.
Result also signed number in 2’s complement form.
SCSE,
DX AX/S Quotient is obtained in AX and remainder in DX.
AAD - ASCII adjust before division.
16
Logical Instructions
VIT University
Jasmin T Jose Asst. professor
? Test the value of D3 bit of register CH without affecting other bits.
OR D, S – Result will be stored in destination.
? Set two MSBs and two LSBs of register AL without affecting other
bits.
XOR D, S – Result will be stored in destination.
? What is the result of XOR AH,E0h .
SCSE,
NOT D - D cannot be an immediate data.
17
Logical Instructions
ROTATING INSTRUCTIONS
ROL D, count – Rotate left destination without carry.
MSB will be copied to CF and LSB. If the count is greater than 1,
then it should be stored in CL. Then use CL as count.
VIT University
Jasmin T Jose Asst. professor
MOV CL, 03h
ROL BP, CL
RCL D, count - Rotate left destination with carry.
MSB will be copied to CF, CF will be moved to LSB.
ROR D, count – Rotate right destination without carry.
LSB will be copied to CF and MSB. If the count is greater than 1,
SCSE,
then it should be stored in CL. Then use CL as count.
MOV CL, 03h
ROR BX, CL – Result will be stored in destination.
RCR D, count – Rotate right destination with carry. 18
SHIFTING INSTRUCTIONS
SAL/SHL D, count – Shift Arithmetic Left/ Shift Logical Left
VIT University
Jasmin T Jose Asst. professor
destination. MSB copied to CF. 0 copied to LSB.
Eg. Shift 03h by 2times.
SHR D, count - Shift Logical right destination.
LSB will be copied to CF. 0 copied to MSB.
Division of unsigned number, X/2n
Eg. Shift 10h by 3 times.
SCSE,
SAR D, count – Shift Arithmetic right destination.
LSB will be copied to CF and sign bit will be copied to MSB.
Division of signed number, +-X/2n
19
Flags controlling Instructions
VIT University
Jasmin T Jose Asst. professor
STD – Set Direction flag.
CLD- Clear Direction flag.
STI – Set Interrupt flag.
CLI – Clear Interrupt flag.
CMC- Complement the Carry Flag.
? Hot to change the values of Trap flag
SCSE,
20
Flags controlling Instructions
• No instruction for changing the data of Trap flag
VIT University
Jasmin T Jose Asst. professor
SCSE,
21
Flags controlling Instructions
• No instruction for changing the data of Trap flag
VIT University
Jasmin T Jose Asst. professor
PUSHF
POP BX
OR BH,01H
PUSH BX
POPF
SCSE,
22
Flags controlling Instructions
? Hot to change the values of Trap flag to 0
PUSHF
POP BX
VIT University
Jasmin T Jose Asst. professor
AND BH,0FEH
PUSH BX
POPF
SCSE,
23
Branching Instructions
A new 16bit address is transferred to IP, instead of reading the next
instruction code in sequence.
Intrasegment branching
Intersegment branching
VIT University
Jasmin T Jose Asst. professor
Short jump, Near jump, Far jump (2 operand will be there).
JMP operand – Unconditional jump.
JC and JNC - Conditional Jump instructions
JZ/ JE
JNZ/ JNE
JP/ JPE
SCSE,
JNP/ JPO
JS and JNS
JO and JNO
24
JCXZ
? Write a program to find smallest number among n numbers.
Branching Instructions
Using after comparison instruction,
VIT University
Jasmin T Jose Asst. professor
JNA / JBE
JB / JNAE
JNB / JAE
SCSE,
JNL/ JGE
25
Branching Instructions
VIT University
Jasmin T Jose Asst. professor
Short/Near CALL – IP alone will be changed
Far CALLs- IP and CS will be changed
RET – returns back from sub program to main program, next instruction
after the CALL instruction.
If intra-segment subroutine, then POP IP.
If inter-segment subroutine, POP IP, POP CS
SCSE,
RET DISPLACEMENT- Stack Top = [SP] + DISP
26
Machine Control Instructions
NOP –
Used in delay program to waste the time of MP.
It will take 3 clk cycles.
VIT University
Jasmin T Jose Asst. professor
HLT - Stop the fetching and execution processes.
Again BIU can be activated by giving,
RESET
NMI
INTR
WAIT – wait for TEST or INTERRUPT signal.
SCSE,
MP will be in idle state
LOCK – Lock the BUS signal.
NO other processor can get the control of buses.
27
ESC - Pass control to the co-processor.
I/O Instructions
IN AL/AX, 8bit port add/DX –
If port address is of 8bit, it can be directly given in the instruction.
IN AL, 45h [0045] AL
IN AX, 55h [0055] AL and [0056] AH
If port address is of 16 bit, store it in DX register and use DX as source.
VIT University
Jasmin T Jose Asst. professor
MOV DX, 2020h
IN AX, DX
OUT 8bit addr/DX, AL/AX –
If port address is of 8bits, it can be directly given in the instruction.
OUT 45h, AL [AL] 0045h
OUT 55h,AX [AL] 0055h and [AH] 0056h
SCSE,
If port address is of 16 bit, store it in DX register and use DX as source.
MOV DX, 2020h
OUT DX, AX
28
Stack Instructions
PUSH S(16bit) – S can be Reg./ Seg.reg/ Memory
PUSH AX
[AX] is 3020h
SS = 7000h
SP=ST=0130h
VIT University
Jasmin T Jose Asst. professor
AL 70130 and AH 70131
POP D(16bit) –
POP AX
[70130] AL
[70131] AH
SCSE,
29
Jasmin T Jose Asst. professor SCSE,
VIT University
String Instructions
REP repeat the given instruction till CX != 0
VIT University
Jasmin T Jose Asst. professor
REPZ repeat the given instruction while ZF = 1
SCSE,
REPNZ repeat the given instruction while ZF = 0
VIT University
Jasmin T Jose Asst. professor
moves contents of byte given by DS:SI
MOVSB
into ES:DI
moves contents of word given by
MOVSW
DS:SI into ES:DI
SCSE,
moves contents of AL to byte address
STOSB
given by ES:DI; DI is incr/dec by 1
VIT University
Jasmin T Jose Asst. professor
moves the contents of AX to the word
STOSW address given by ES:DI; DI is incr/decr
by 2
SCSE,
University
professor SCSE, VIT
Jasmin T Jose Asst.
compares byte at ES:DI with AL
SCASB
and sets flags according to result
VIT University
Jasmin T Jose Asst. professor
CMPSB
DS:SI and sets flags
SCSE,
String Instructions programs
REP:- instruction prefix
MOVSB/MOVSW:- Based on DF
org 100h
;mov ax,5000h
VIT University
Jasmin T Jose Asst. professor
;mov ds,ax
;mov ax,6000h
;mov es, ax
mov cx,05h
Cld ;D flag =0; auto increment mode
mov si,2000h
SCSE,
mov di,3000h
rep movsb
hlt
ret 36
String Instructions
CMPSB/W:- Compare string bytes or word
jmp Start
str1 db 'hello'
str2 db 'hallo'
VIT University
Jasmin T Jose Asst. professor
Start:
mov cx,05h
Cld ;D flag =0; auto increment mode
lea si,str1
lea di,str2
SCSE,
repz cmpsb
hlt
37
String Instructions
SCASB/W:- Scan string bytes or word
org 100h
str1 db 'this is good enough','$'
start:
VIT University
Jasmin T Jose Asst. professor
lea di,str1
mov cx,0fh
mov ax,76h
Cld
repne scasb
jnz l1
SCSE,
Mov al,01h
hlt
l1:mov al,00h
38
hlt
ret
String Comparison
org 100h
jmp Start
str1 db 'hello'
str2 db 'hello'
Start:
VIT University
Jasmin T Jose Asst. professor
mov cx,05h
Cld ;D flag =0; auto increment mode
lea si,str1
lea di,str2
repe cmpsb
SCSE,
jnz l1
mov al,0ah
hlt
l1:mov al,00h
hlt
Reverse of a string
org 100h
jmp start l1:lea di,str2
str2 DB 7 DUP ('$') dec si
str1 db 'this is good','$'
start:
VIT University
Jasmin T Jose Asst. professor
l2:mov al,[si]
lea si,str1 mov [di],al
;;;find the length of string inc di
mov cx,00h dec si
l3:mov al,[si] loop l2
cmp al,'$' hlt
SCSE,
je l1
inc cx
inc si
jmp l3
palindrome or not
jmp start
str1 db 'malasyalam','$'
strlen1 dw $-str1
strrev db 20 dup(' ')
VIT University
Jasmin T Jose Asst. professor
pal db 'palindrome','$'
notpal db 'not palindrome.','$'
start:
mov cx, strlen1
add cx, -2
SCSE,
lea si, str1
lea di, strrev
add si, cx
L1: mov al, [si]
mov [di], al
dec si
inc di
loop L1
VIT University
Jasmin T Jose Asst. professor
mov [di], al
inc di
mov dl, '$'
mov [di], dl
mov cx, strlen1
SCSE,
;palindrom or not
lea si, str1
lea di, strrev
repe cmpsb
jne nopal
;pal:
mov ah, 09h
lea dx, pal
int 21h
jmp Exit
VIT University
Jasmin T Jose Asst. professor
nopal:
mov ah, 09h
lea dx, notpal
int 21h
SCSE,
Exit:
mov ax, 4c00h
int 21h
DOS function codes with int 21h
INT 21h is a type of interrupt.
Calls the interrupt handler at this position in the Interrupt
vector table.
It is the DOS Function
Number of function values are available with this dos function
VIT University
Jasmin T Jose Asst. professor
Function values should be in AH register
MOV AH,01H
MOV AH,02H
MOV AH,07H
MOV AH,08H
MOV AH,09H
SCSE,
MOV AH,2CH
MOV AH,4CH
DOS function codes with int 21h
Commonly used function values are:
01h: -Read character from STDIN (keyboard)
AL stores the character
MOV AH,01H/07/08H ;WITH/WITHOUT ECHO
VIT University
Jasmin T Jose Asst. professor
INT 21H
02h: -Write the character in STDOUT
Character should be in DL
MOV DL,41H (A)
MOV AH,02H/05H ;STDOUT/PRINTER
INT 21H
09H: - Display a string
SCSE,
LEA DX, STRING1
MOV AH,09H
INT 21H
DOS function codes with int 21h
CH,CL,DH,DL stores the TIME
MOV AH,2ch ;get the time
INT 21H
0AH: - Read a string to the specified buffer
VIT University
Jasmin T Jose Asst. professor
LEA DX, BUFFER
MOV AH,0AH
INT 21H
SCSE,
org 100h
JMP START
string db 'Enter the First character : $\'
string2 db 'entered character is:$\'
s_size dw $-string
START:
VIT University
Jasmin T Jose Asst. professor
MOV Ax,s_size
lea dx,string
mov ah,09h
int 21h
mov ah,01h
int 21h
SCSE,
mov bl,al
lea dx,string2
mov ah,09h
int 21h
mov ah,02h