You are on page 1of 44

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: MOV destination, source Action: Copies the data from src to dest Src: Immediate data/reg/mem location Dest: reg/mem location Flags: No flags are affected

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 BX = 9ABCH DS = 5678H AX = 1234H DS Memory PA = BX + (DS)*10H = 9ABC + 5678*10H = 6023CH 60239H 6023AH 6023BH 6023CH 6023DH 6023EH AFTER BX = 9ABCH DS = 5678H AX = 4433H DS Memory 60239H 6023AH 6023BH 6023CH 6023DH 6023EH

11H
22H 33H

11H
22H 33H

44H 55H

44H 55H

Memory read

MOV 1234H[BP],SI
Memory write BEFORE SI = 5678H BP = AB12H SS = 9000H SS Memory PA = 1234 + BP + (SS)*10H = 1234 + AB12 + 9000*10H 9BD43H = 9BD46H 9BD44H 9BD45H 9BD46H 9BD47H 9BD48H 9BD43H 9BD44H 9BD45H 9BD46H 9BD47H 9BD48H AFTER SI = 5678H BP = AB12H SS = 9000H SS Memory

11H
22H 33H

11H 22H 78H 56H 55H

44H 55H

MOV CS:[BX][DI],53H
Memory write BEFORE DI = 1234H BX = AB12H CS = 9000H CS Memory PA = DI + BX + (CS)*10H = 1234 + AB12 + 9000*10H 9BD43H = 9BD46H 9BD44H 9BD45H 9BD46H 9BD47H 9BD48H 9BD43H 9BD44H 9BD45H 9BD46H 9BD47H 9BD48H AFTER DI = 1234H BX = AB12H CS = 9000H CS Memory

11H
22H 33H

11H 22H 53H 44H 55H

44H 55H

MOV [BX],AL
Memory write BEFORE AL= 34H BX = 5678H DS = 9000H Memory PA = BX + (DS)*10H = 5678 + 9000*10H = 95678H AFTER AL = 34H BX = 5678H DS = 9000H Memory

95678H

99H

95678H

34H

LEA -Load Effective Address(EA)


LEA reg, src 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 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]


LEA BX, 56H[SI] BEFORE BX = 1000H SI = 7894H FLAG = 0234H AFTER BX = 78EAH SI = 7894H FLAG = 0234H

EA = 56H + 7894H = 78EAH

LEA BP, 05H[BX+DI]


BEFORE LEA BP, 05H[BX+DI] BX = A1B2H DI = 5678H BP = 3239H AFTER BX = A1B2H DI = 5678H BP = F82FH

EA = 05H + A1B2H + 5678H = F82FH

LDS-Load Register And Data Segment


LDS reg, src 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 AX, [1234H]


LDS AX, [1234H] BEFORE DS = 2000H AX = 0000H Memory PA = 1234H + (DS)*10H = 1234 + 20000H = 21234H AFTER DS = 7856H AX = 3412H Memory

21234 21235 21236 21237

12H 34H 56H 78H

21234 21235 21236 21237

12H 34H 56H 78H 1st word

2nd word

LES-Load Register And Extra Segment


LES reg, src 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 BX, [1234H]


LES BX, [1234H] BEFORE DS = 2000H BX = 0000H ES = 9000H Memory PA = 1234H + (DS)*10H = 1234 + 20000H = 21234H AFTER DS = 2000H BX = 3412H ES = 7856H Memory

21234 21235 21236 21237

12H 34H 56H 78H

21234 21235 21236 21237

12H 34H 56H 78H 1st word

2nd 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 AL = 0AH BX = 0100H DS = 9000H PA = (AL) + (BX) +(DS)*10H = 0AH + 0100H + 90000H = 9010AH Starting addr of DS Starting addr of lookup table 90000H -----90100 H Read from table LOOKUP TABLE AFTER AL = 41H BX = 0100H DS = 9000H

DS Memory

9010AH

41H

LAHF (Load AH with lower byte of flag reg) -- no operands


SF
7

ZF
6

0
5

AF
4

0
3

PF
2

1
1

CF
0

STC MOV AX,46H LAHF

; AX=0046H ; AX=0346H

SAHF (Store AH contents in lower byte of flag reg) -- no operands


XCHG (Exchange operation) Syntax: XCHG op1,op2 XCHG BX, 1234[BX+SI] Invalid Instruction XCHG [BX],[SI] XCHG CS,BX

STACK

PUSH BX
BEFORE SP = 1234H SS = 5000H BX = 5678H AFTER SP = 1232H SS = 5000H BX = 5678H

PA = 1234H + (SS)*10H = 1234 + 50000H .. = 51234H 5122FH


Top

SS Memory

SS Memory

.. 5122FH

51232H Top of stack C3H

51232H

78H 56H C3H

51234H

51234H

POP BX
BEFORE SP = 1232H SS = 5000H BX = 1234H AFTER SP = 1234H SS = 5000H BX = 5678H

PA = 1232H + (SS)*10H = 1234 + 50000H .. = 51232H 5122FH

SS Memory

SS Memory

.. 5122FH

51232H Top of stack

78H 56H C3H


Top

51232H C3H

51234H

51234H

STRING INSTRUCTIONS

An array (a sequence) of characters is called a STRING STR DB SVIT


.. 53 56 49 54 . . Base address of array STR

STR[0] STR[1] STR[2] STR[3]

2000=2000+0 2000=2000+1 2000=2000+2 2000=2000+3

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. 2. 3. 4. 5. 6. MOVSB/ MOVSW CMPSB/ CMPSW SCASB/ SCASW LODSB/ LODSW STOSB/ STOSW 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,12H MOV CX,5 REP STOSB


ES=4F57

12
DI=02

12
DI=03

12
DI=04

12
DI=05

12
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,1234H MOV CX,5 REP STOSW


ES=4F57

1234
DI=02

1234
DI=04

1234
DI=06

1234
DI=08

1234
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, x MOV CX,5 REP LODSB


AL=41h AL=42h AL=43h

x db A, B , C , D , E

AL=44h

AL=45h

41
SI=02

42
SI=03

43
SI=04

44
SI=05

45
SI=06

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, x MOV CX,5 REP LODSW


AX=0111h AX=0222h

x dw 111h, 222h, 333h, 444h, 555h

AX=0333h

AX=0444h

AX=0555h

11 01
SI=02

22 02
SI=04

33 03
SI=06

44 04
SI=08

55 05
SI=0A

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 direction LEA SI, x y db 5 dup(0) x db 1, 2, 3, 4, 5 LEA DI, y MOV CX,5 REP MOVSB
01 Data Segment DS SI=01 02 SI=02 03 SI=03 04 SI=04 05 SI=05

01 00 Extra Segment ES DI=06

00 02 DI=07

03 00 DI=08

04 00 DI=09

05 00 DI=0A

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,ax Lea si,str Lea di,dest Cld Mov cx,N Rep movsb Mov ah,4ch Int 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 direction LEA SI, x y dw 5 dup(0) x dw 1, 2, 3, 4, 5 LEA DI, y MOV CX,5 REP MOVSW
01 00 Data Segment DS SI=02 02 00 SI=04 03 00 SI=06 04 00 SI=08 05 00 SI=0A

01 00 00 Extra Segment ES DI=07

02 00 00 DI=09

03 00 00 DI=0A

04 00 00 DI=0C

05 00 00 DI=0E

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,ax Lea si,str Lea di,dest Cld Mov cx,N Rep movsw Mov ah,4ch Int 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

CLD MOV AX,data MOV DS,AX MOV ES,AX MOV SI,STR1 MOV DI,STR2 MOV CX, size ;set counter to string length REPE CMPSB ;compare until equal JNZ not_equal MOV 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

CLD MOV AX,data STR1 Dw 1234h, 5678h, 9012h, 3456h MOV DS,AX STR2 Dw 1234h, 5678h, 9012h, 3456h MOV ES,AX MOV SI,STR1 MOV DI,STR2 MOV CX, size ;set counter to string length REPE CMPSW ;compare until equal JNZ not_equal MOV AL,y JMP exit not_equal: MOV AL,n exit: MOV AH,04CH INT 21H

You might also like