Download - 8086 Instruction Set
8086 INSTRUCTION SET
•Data transfer instruction•Arithmetic instruction•Bit manipulated instruction•String instruction•Iteration control instruction•Branch instruction•Data size conversion instruction•Processor control instruction
MOV Instruction
• Syntax:• Action: Copies the data from src to dest• Src: Immediate data/reg/mem location• Dest: reg/mem location• Flags: No flags are affected
MOV destination, source
• MOV AX, BX• MOV AX, [BX]• MOV AL,[1234H]• MOV AL,BL• MOV AL,A• MOV SP, BP
• MOV A,B• MOV [BX],[SI]• MOV 1234H,BX• MOV IP, AX• MOV CS, AX• MOV DS, ES• MOV DS, 1234H• MOV AL,BX
Valid instruction
Invalid instruction
MOV AX, [BX]
BEFORE AFTER
BX = 9ABCHDS = 5678HAX = 1234H
BX = 9ABCHDS = 5678HAX = 4433H
PA = BX + (DS)*10H = 9ABC + 5678*10H = 6023CH
11H
22H
33H
44H
55H
………60239H6023AH
6023BH6023CH6023DH
6023EH
DS MemoryDS Memory
………60239H6023AH
6023BH6023CH6023DH
6023EH
Memory read
11H
22H
33H
44H
55H
MOV 1234H[BP],SI
BEFORE AFTER
SI = 5678HBP = AB12HSS = 9000H
SI = 5678HBP = AB12HSS = 9000H
PA = 1234 + BP + (SS)*10H = 1234 + AB12 + 9000*10H = 9BD46H
11H
22H
33H
44H
55H
11H
22H
78H
56H
55H
………9BD43H9BD44H
9BD45H9BD46H9BD47H
9BD48H
SS Memory SS Memory
………9BD43H9BD44H
9BD45H9BD46H9BD47H
9BD48H
Memory write
MOV CS:[BX][DI],53H
BEFORE AFTER
DI = 1234HBX = AB12HCS = 9000H
DI = 1234HBX = AB12HCS = 9000H
PA = DI + BX + (CS)*10H = 1234 + AB12 + 9000*10H = 9BD46H
11H
22H
33H
44H
55H
11H
22H
53H
44H
55H
………9BD43H9BD44H
9BD45H9BD46H9BD47H
9BD48H
CS Memory CS Memory
………9BD43H9BD44H
9BD45H9BD46H9BD47H
9BD48H
Memory write
MOV [BX],AL
BEFORE AFTER
AL= 34HBX = 5678HDS = 9000H
AL = 34HBX = 5678HDS = 9000H
PA = BX + (DS)*10H = 5678 + 9000*10H = 95678H
99H 34H95678H
Memory Memory
95678H
Memory write
LEA -Load Effective Address(EA)
• Syntax: reg - BX,SI,DI,BP,SP src - memory location
• Action: 1) instr copies the EA of the memory & it is transferred to the specified reg.
2) instr is reqd for initializing the memory pointers (BX,SI,DI,BP,SP)
3) instr does not transfer data from memory.
LEA reg, src
• LEA BX, DS:1234H• LEA BX, [1234H]• LEA BX, MSG• LEA CX, MSG• LEA BP, MSG• LEA SP, MSG
• LEA DS, 56H[SI]• LEA BX, SI• LEA BX, 1234H• LEA IP, MSG
Valid instruction
Invalid instruction
LEA BX, 56H[SI]
BEFORE AFTER
BX = 1000HSI = 7894HFLAG = 0234H
BX = 78EAHSI = 7894HFLAG = 0234H
LEA BX, 56H[SI]
EA = 56H + 7894H = 78EAH
LEA BP, 05H[BX+DI]BEFORE AFTER
BX = A1B2HDI = 5678HBP = 3239H
BX = A1B2HDI = 5678HBP = F82FH
LEA BP, 05H[BX+DI]
EA = 05H + A1B2H + 5678H = F82FH
LDS-Load Register And Data Segment
• Syntax: reg – 16 bit reg except IP src - memory location
• Action: 1) instr transfers 32-bit data from memory.
2) 1st 16-bits transferred to reg specified 3) 2nd 16-bits transferred to DS reg .
LDS reg, src
LDS AX, [1234H]
BEFORE AFTER
DS = 2000HAX = 0000H
DS = 7856HAX = 3412H
LDS AX, [1234H]
PA = 1234H + (DS)*10H = 1234 + 20000H = 21234H
12H
34H
56H
78H
12H
34H
56H
78H
2123421235
2123621237
2123421235
2123621237
Memory Memory
2nd word1st word
LES-Load Register And Extra Segment
• Syntax: reg – 16 bit reg except IP src - memory location
• Action: 1) instr transfers 32-bit data from memory.
2) 1st 16-bits transferred to reg specified 3) 2nd 16-bits transferred to ES reg .
LES reg, src
LES BX, [1234H]
BEFORE AFTERDS = 2000HBX = 0000HES = 9000H
DS = 2000HBX = 3412HES = 7856H
LES BX, [1234H]
PA = 1234H + (DS)*10H = 1234 + 20000H = 21234H
12H
34H
56H
78H
12H
34H
56H
78H
2123421235
2123621237
2123421235
2123621237
Memory Memory
2nd word1st word
XLAT-Translate Instruction
• Action: 1) instr is useful to convert from one code to another
using lookup table 2) has no operands & is implied mode of addressing. So
prior to execution of this instr, BX should be initialized to contain the start address of the lookup table & AL should contain the code to be converted.
3) After executing the instr, 1 byte data stored in offset address AL+BX in the DS memory is copied into AL ie
(AL)Ds:[(BX) + (AL)]4) AL contains the converted code.
XLAT
BEFORE AFTERAL = 0AHBX = 0100HDS = 9000H
AL = 41HBX = 0100HDS = 9000H
PA = (AL) + (BX) +(DS)*10H = 0AH + 0100H + 90000H = 9010AH
41H
90000H ------90100 H
9010AH
DS Memory
LOOKUP TABLE
Read from table
Starting addr of DS
Starting addr of lookup table
• LAHF (Load AH with lower byte of flag reg) -- no operands
STCMOV AX,46H ; AX=0046HLAHF ; AX=0346H
• SAHF (Store AH contents in lower byte of flag reg) -- no operands
• XCHG (Exchange operation) Syntax: XCHG op1,op2XCHG BX, 1234[BX+SI]XCHG [BX],[SI]XCHG CS,BX
Invalid Instruction
SF ZF 0 AF 0 PF 1 CF
7 6 5 4 3 2 1 0
STACK
PUSH BX
BEFORE AFTER
SP = 1234HSS = 5000HBX = 5678H
SP = 1232HSS = 5000HBX = 5678H
PA = 1234H + (SS)*10H = 1234 + 50000H = 51234H
C3H
78H
56H
C3H
…….. 5122FH
51232H
51234H
SS Memory SS Memory
Top of stack
…….. 5122FH
51232H
51234H
Top
POP BX
BEFORE AFTER
SP = 1232HSS = 5000HBX = 1234H
SP = 1234HSS = 5000HBX = 5678H
PA = 1232H + (SS)*10H = 1234 + 50000H = 51232H
78H
56H
C3H C3H
…….. 5122FH
51232H
51234H
SS Memory SS Memory
Top of stack
…….. 5122FH
51232H
51234HTop
STRING INSTRUCTIONS
• An array (a sequence) of characters is called a STRING• STR DB “SVIT”
……..
………
53
56
49
54
…….
…….
STR[0]STR[1]STR[2]STR[3]
2000=2000+02000=2000+12000=2000+22000=2000+3
Base address of array STR
•Data segment register (used as source segment)•Extra segment register (used as destination segment)
•Source index register(SI) should be associated with DS•Destination index register(DI) should be associated with ES
STRING INSTRUCTIONS IN 8086
1. MOVSB/ MOVSW2. CMPSB/ CMPSW3. SCASB/ SCASW4. LODSB/ LODSW5. STOSB/ STOSW6. REP/REPE/REPZ/REPNE/REPNZ
• During string manipulation, SI register should contain offset address of the source byte or word with respect to data segment.
LEA SI, STR1 or MOV SI, OFFSET STR1
• During string manipulation, DI register should contain offset address of the destination byte or word with respect to extra segment.
LEA DI, STR2 or MOV DI, OFFSET STR2
STOSB -store byte from AL into string element addressed by DI in ES & updates DI (ES:[DI])
• ES:[(DI)] [AL]• No flags affected• If DF=0 then DI=DI+1• If DF=1 then DI=DI-1• Segment for memory access : ES
MOV AL,12HMOV CX,5REP STOSB
ES=4F57
12 12 12 12 12
DI=02 DI=03 DI=04 DI=05 DI=06
STOSW -store word from AX into string element addressed by DI in ES & updates DI (ES:[DI])
• ES:[(DI)] [AX]• No flags affected• If DF=0 then DI=DI+2• If DF=1 then DI=DI-2• Segment for memory access : ES
MOV AX,1234HMOV CX,5REP STOSW
ES=4F57
1234 1234 1234 1234 1234
DI=02 DI=04 DI=06 DI=08 DI=0A
LODSB –load byte at DS:[SI] into AL & updates SI
• DS:[SI] AL• No flags affected• If DF=0 then SI=SI+1• If DF=1 then SI=SI-1• Segment for memory access : DS
LEA SI, xMOV CX,5REP LODSB
AL=41h
41 42 43 44 45
SI=02 SI=03 SI=04 SI=05 SI=06
x db ‘A’, ’B’ , ’C’ , ’D’ , ’E’
AL=42h AL=43h AL=44h AL=45h
LODSW –load word at DS:[SI] into AX & updates SI
• AXDS:[SI] • No flags affected• If DF=0 then SI=SI+2• If DF=1 then SI=SI-2• Segment for memory access : DS
LEA SI, xMOV CX,5REP LODSW
AX=0111h
11 01 22 02 33 03 44 04 55 05
SI=02 SI=04 SI=06 SI=08 SI=0A
x dw 111h, 222h, 333h, 444h, 555h
AX=0222h AX=0333h AX=0444h AX=0555h
MOVSB –Copy Byte at DS:[SI] to ES:[DI] & updates SI and DI
• ES:[DI] DS:[SI] • No flags affected• If DF=0 then SI=SI+1, DI=DI+1• If DF=1 then SI=SI-1, DI=DI-1• Segment for memory access : DS and ES
CLD ;set forward directionLEA SI, xLEA DI, yMOV CX,5REP MOVSB
01 02 03 04 05
SI=01 SI=02 SI=03 SI=04 SI=05
y db 5 dup(0)x db 1, 2, 3, 4, 5
Data Segment DS
Extra Segment ES 00 00 00 00 00
DI=06 DI=07 DI=08 DI=09 DI=0A
01 02 03 04 05
Move string from one memory location to another
.model small
.data str db “Test string” N dw $-str dest db 10 dup(?)
.code Mov ax,@data Mov ds,ax Mov es,axLea si,strLea di,dest Cld Mov cx,N
Rep movsb
Mov ah,4chInt 21h
MOVSW –Copy word at DS:[SI] to ES:[DI] & updates SI and DI
• ES:[DI] DS:[SI] • No flags affected• If DF=0 then SI=SI+2, DI=DI+2• If DF=1 then SI=SI-2, DI=DI-2• Segment for memory access : DS and ES
CLD ;set forward directionLEA SI, xLEA DI, yMOV CX,5REP MOVSW
01 00 02 00 03 00 04 00 05 00
SI=02 SI=04 SI=06 SI=08 SI=0A
y dw 5 dup(0)x dw 1, 2, 3, 4, 5
Data Segment DS
Extra Segment ES 00 00 00 00 00
DI=07 DI=09 DI=0A DI=0C DI=0E
01 00 02 00 03 00 04 00 05 00
Move string from one memory location to another
.model small
.data str db “Test string” N dw ($-str)/2 dest db 10 dup(?)
.code Mov ax,@data Mov ds,ax Mov es,axLea si,strLea di,dest Cld Mov cx,N
Rep movsw
Mov ah,4chInt 21h
SCASB –Compare bytes: ES:[DI] from AL & updates DI
• AL - ES:[DI]• If DF=0 then DI=DI+1• If DF=1 then DI=DI-1• Segment for memory access : ES• All flags are updated to reflect the relationship
of the destination operand to the source operand
SCASW –Compare words: ES:[DI] from AX & updates DI
• AX - ES:[DI] • If DF=0 then DI=DI+2• If DF=1 then DI=DI-2• Segment for memory access : ES• All flags are updated according to result:
OF,SF,ZF,AF,CF,PF
CMPSB –Compare bytes: ES:[DI] from DS:[SI] & updates DI and SI
• DS:[SI] - ES:[DI] • If DF=0 then DI=DI+1, SI=SI+1• If DF=1 then DI=DI-1, SI=SI-1• Segment for memory access : DS, ES• All flags are updated to reflect the relationship
of the destination operand to the source operand
CLDMOV AX,dataMOV DS,AXMOV ES,AXMOV SI,STR1MOV DI,STR2MOV CX, size ;set counter to string lengthREPE CMPSB ;compare until equalJNZ not_equalMOV AL,’y’JMP exit
not_equal: MOV AL,’n’
exit: MOV AH,04CH INT 21H
STR1 Db ‘test string’STR2 Db ‘test string’
CMPSW –Compare words: ES:[DI] from DS:[SI] & updates DI and SI
• DS:[SI] - ES:[DI] • If DF=0 then DI=DI+2, SI=SI+2• If DF=1 then DI=DI-2, SI=SI-2• Segment for memory access : DS, ES• All flags are updated to reflect the relationship
of the destination operand to the source operand
CLDMOV AX,dataMOV DS,AXMOV ES,AXMOV SI,STR1MOV DI,STR2MOV CX, size ;set counter to string lengthREPE CMPSW ;compare until equalJNZ not_equalMOV AL,’y’JMP exit
not_equal: MOV AL,’n’
exit: MOV AH,04CH INT 21H
STR1 Dw 1234h, 5678h, 9012h, 3456hSTR2 Dw 1234h, 5678h, 9012h, 3456h