You are on page 1of 102

Processor Status Word

Basics
FLAGS
16 – bit PSW
b12 - b15 b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0

UUUU OF DF IF TF SF ZF X AC X PF X CF

8085 Flags

Control Flags Conditional Flags

UUUU – Undefined bits. Reserved for the future


uses.
FLAGS (CONTD.)
b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0

OF DF IF TF SF ZF X AC X PF X CF

• CF - CARRY FLAG - Is a Carry generated/Borrow


required during ALU operation or not?
– YES, Carry generated so CF = 1.
– NO, Carry not generated so CF = 0.

• PF - PARITY FLAG – Is the Parity of 1s in the


lower byte of Result, Even?
– YES, EVEN Numbers of 1s in result, so PF = 1.
– NO, ODD Numbers of 1s in result , so PF = 0.
FLAGS (CONTD.)
b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0

OF DF IF TF SF ZF X AC X PF X CF

• AC – AUXILIARY CARRY FLAG - Is a Carry


generated form bit b3 to b4 during ALU operation
or not?
– YES, Auxiliary Carry generated so AC = 1.
– NO, Auxiliary Carry not generated so AC = 0.
• ZF - ZERO FLAG - Is the Result of ALU operation 0
or not?
– YES, Result is 0 so ZF = 1.
– NO, Result is not 0 so ZF = 0.
FLAGS (CONTD.)
b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0

OF DF IF TF SF ZF X AC X PF X CF

• SF - SIGN FLAG - 2’s Comp Sign of the Result.


– SF =1, MS bit of the Result is 1 (2’s Comp Negative)
– SF =0, MS bit of the Result is 0 (2’s Comp Positive)

• OF – OVERFLOW FLAG - Is a Signed (i.e. 2’s


Comp) Overflow occurred during ALU
operation or not?
– YES, Signed Overflow in Result so OF = 1.
– NO, No Signed Overflow in Result so OF = 0.
SIGNED & UNSIGNED
Unsigned Binary Signed Binary
0 0000 0 0000
1 0001 +1 0001
2 0010 +2 0010
3 0011 +3 0011
4 0100 +4 0100
5 0101 +5 0101
6 0110 +6 0110
7 0111 +7 0111
8 1000 -8 1000
9 1001 -7 1001
10 1010 -6 1010
11 1011 -5 1011
12 1100 -4 1100
13 1101 -3 1101
14 1110 -2 1110
15 1111 -1 1111
SIGNED ARITHMETIC
• Add -3 & -2
-3 = 1101
-2 = 1110
Ans -5 = 11011
– -5 can be represented by 4-bit binary using 2’s comp,
so result is in the range i.e. result has not over flown.
– For 2’s comp arithmetic we discard carry, so Ans=1011.
– MS bit of result is 1, so SF =1, meaning that result is
2’s comp negative.
– Result = 2’s comp (1011) = - (0101) = -5
– There is a Carry from MSB-1 to MSB and a Carry out
from MS bit means no signed overflow. So, OF = 0.
SIGNED ARITHMETIC (CONTD.)
• Add +3 & +4 X X

+3 = 0011
+4 = 0100
Ans +7 = 0111
– +7 can be represented by 4-bit binary using 2’s comp,
so result is in the range i.e. result has not over flown.
– MS bit of result is 0, so SF =0, meaning that result is
2’s comp positive.
– Result = 0111 = +7
– There is no Carry from MSB-1 to MSB and no Carry out
from MSB, means no signed overflow. So, OF = 0.
SIGNED ARITHMETIC (CONTD.)
• Add +3 & +6 X
+3 = 0011
+6 = 0110
Ans +9 = 1 0 0 1 = -7
– +9 can not be represented by 4-bit binary using 2’s
comp, so result is not in the range i.e. result has over
flown.
– MS bit of result is 1, so SF =1, meaning that result is
2’s comp negative. This leads to wrong conclusion
because result has over flown.
– Rule: There is a Carry from MSB-1 to MSB but no Carry
out from MSB, means Signed Overflow. So, OF = 1.
SIGNED ARITHMETIC (CONTD.)
• Add -5 & -6 X
-5 = 1011
-6 = 1010
Ans -11 = 1 0 1 0 1 = +5
– -11 can not be represented by 4-bit binary using 2’s
comp, so result is not in the range i.e. result has over
flown.
– MS bit of result is 0, so SF =0, meaning that result is
2’s comp positive. This leads to wrong conclusion
because result has over flown.
– Rule: There is no Carry from MSB-1 to MSB but a Carry
out from MSB, means Signed Overflow. So, OF = 1.
FLAGS (CONTD.)
b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0

OF DF IF TF SF ZF X AC X PF X CF

• IF – INTERRUPT CONTROL FLAG – Enables or


Disables the INTR pin of 8086.
– When IF = 1, INTR pin is enabled.
– When IF = 0, INTR pin is disabled.

• TF - TRAP FLAG – Traps the processor in


single instruction execution mode.
– When TF = 1, Single Instruction Execution Mode.
– When TF = 0, Normal Execution Mode.
FLAGS (CONTD.)
b11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0

OF DF IF TF SF ZF X AC X PF X CF

• DF – STRING DIRECTION FLAG – Decides the


direction of string processing.
A
Forward Direction Backward Direction
J A to Y Y to A
A Auto Increment Auto Decrement
Mode DF = 0 Mode DF = 1
Y
Assembly Language Instructions

Some Frequently Used Instructions


Instructions (Contd.)

• MOV TARGET, SOURCE – Copy data


content of SORCE into TARGET
– TARGET ← SOURCE
– None of the conditional flags are affected.
– E.g. MOV DX, AX
– Let initially DX = 4532 & AX = 1234
then after Execution of MOV DX, AX
DX = 1234 & AX = 1234
Instructions (Contd.)
• Examples of MOV
– MOV AL, CL Copy CL into AL (8-bit Operation)

– MOV DI, DX Copy DX into DI (16-bit Operation)


– MOV AX, DH INVALID, Data Size Mismatch
– MOV DS, AX Copy AX into DS. Segment registers
can only be accessed through Acc.
– MOV DS, ES INVALID, Segment to Segment MOV
is not allowed. Should be done as
MOV AX, ES
MOV DX, AX
Instructions (Contd.)

• ADD OPR1, OPR2


– OPR1 ← OPR1 + OPR2
– Affects All the conditional flags.
• ADC OPR1, OPR2
– OPR1 ← OPR1 + OPR2 + CY
– Affects All the conditional flags.
e.g.
• ADD BX, CX BX ← BX + CX
• ADD AX, DS INVALID, Segment Register can not
be Added or Subtracted.
• ADC AX, DX AX ← AX + DX + CY
Instructions (Contd.)
• SUB OPR1, OPR2
– OPR1 ← OPR1 - OPR2
– OPR1 ← OPR1 + 2’s Comp (OPR2)
– Affects All the conditional flags.

• SBB OPR1, OPR2


– OPR1 ← OPR1 - OPR2 – CY
– OPR1 ← OPR1 + 2’s Comp(OPR2)+ 2’s Comp(CY)
– Affects All the conditional flags.
e.g.
• SUB CX, SI CX ← CX – SI (= CX + 2’s Comp of SI)
• SBB DI, AX AX ← AX – DI - CY
Instructions (Contd.)

• INC OPR
– OPR ← OPR + 1
– Affects all the conditional flags except CY.
• DEC OPR
– OPR ← OPR - 1
– Affects All the conditional flags except CY.
e.g.
• INC CX CX ← CX + 1
• INC DS INVALID, Segment Register can not be
Incremented/Decremented.
• DEC AL AL ← AL - 1
Instructions (Contd.)

• JMP – Jump (Go) to an instruction to


execute.
E.g. JMP ADDRESS (Memory Address)
• IP tells where is the next instruction to be
executed, so JMP will modify the IP start
execution of the desired instruction.
• IP ← ADDRESS if Jump is in current code
segment.
• Jump in the current code segment is called
INTRASEGMENT or NEAR JUMP.
Instructions (Contd.)

• Example of JMP
: • Symbol HERE is
address where the
MOV AX, BX
instruction INC SI is
JMP HERE
stored in memory.
ADD CX, SI • Execution of JMP
DEC BL HERE, skips the
HERE: INC SI execution of ADD &
DEC CX DEC in program.

:
Instructions (Contd.)

E.g. JMP ADDRESS


• When Jump is outside of the current code
segment, it is INTERSEGMENT or FAR JUMP.
• In the Intersegment Jump, CS tells segment
address and IP tells where is the next
instruction to be executed in the new segment.
• Now JMP modifies both CS & IP to execute of
the desired instruction.
• CS ← SEGMENT Value of ADDRESS
IP ← Offset Value of ADDRESS.
Instructions (Contd.)

• JZ – Jump (Go) to instruction to execute


if ZF=1.
E.g. JZ ADDRESS (Memory Address)
• Executes similar to JMP but checks ZF before jump. If
ZF=1 then jump otherwise don’t jump.

• JNZ– Jump (Go) to instruction to execute


if ZF=0.
E.g. JZ ADDRESS (Memory Address)
• Executes similar to JZ but checks for ZF=0 before
jump. If ZF=0 then jump otherwise don’t jump.
Instructions (Contd.)

• Example of JZ
: • SUB AX, AX sets
ZF=1.
SUB AX, AX
• So JZ HERE makes a
JZ HERE
jump.
ADD CX, SI • Execution of JZ
DEC BL HERE, skips the
HERE: INC SI execution of ADD &
DEC CX DEC in program.

:
Instructions (Contd.)

• Example of JZ
SUB AX, AX • INC AX, sets ZF=0.
• So, JZ HERE does
INC AX
not make a jump.
JZ HERE
• Hence, ADD CX, SI
ADD CX, SI is executed next and
DEC BL so on.
HERE: INC SI
DEC CX
:
Conditional JMPs

• Variations of Conditional JMP


– JC Jump if a Carry Generated
i.e. Jump on CF = 1.
– JNC Jump if Carry Not Generated
i.e. Jump on CF = 0.
– JO Jump if Overflow
i.e. Jump on OF = 1.
– JNO Jump if no Overflow
i.e. Jump on OF = 0.
Conditional JMPs (Contd.)

– JPE / JP Jump on EVEN Parity


or Jump if Parity is SET
i.e. Jump on PF = 1.
– JPO / JNP Jump on ODD Parity
or Jump if Parity is NOT SET
i.e. Jump on PF = 0.
– JS Jump if sign is SET
i.e. Jump on SF = 1.
– JNS Jump if sign is NOT SET
i.e. Jump on SF = 0
CMP - COMPARE
• CMP OPR1, OPR2 – Compare OPR2 with
OPR1.
– E.g. CMP AL, BL FLAG ← AL - BL
– Compare register BL with AL.
– It performs a subtraction to compare.
– Result of the subtraction is not stored i.e. after
CMP, both AL & BL remains unaffected.
– Result of subtraction is reflected through the
Flags i.e. all the flags are affected by CMP.
CMP (Contd.)
Let initially AL = 45H and BL = 30H
Execution of CMP AL, BL means
– Perform AL–BL = 45H-30H =15H =(0001 0101)B
After Execution of CMP AL, BL
– A = 45H and B = 30H
– Flags SF=0, ZF=0, PF=0, OF=0, CY=0.
– Note that both A & B, remains unaffected from
compare but all the flags are affected.
Uses
CY=1  A<B, CY=0  A≥B, (CY=0 AND ZF=0)  A>B
JMPs for comparison (Contd.)

• Basic comparisons are Less than,


Greater than and Equal to.
– JE/JZ Jump if Equal (A = B).
or Jump if Zero.
i.e. Jump if ZF=1.
– JNE/JNZ Jump if Not Equal (A ≠ B).
or Jump if not Zero.
i.e. Jump if ZF=0.
JMPs for comparison (Contd.)

• ABOVE & BELOW are used for unsigned


numbers & their arithmetic
– JA Jump if Above (A>B).
i.e. Jump if (ZF=0) AND (CF=0).
– JAE Jump if Above or Equal (A≥B).
i.e. Jump if CF=0.
– JB Jump if Below (A<B).
i.e. Jump if CF=1.
– JBE Jump if Below or Equal (A≤B).
i.e. Jump if (ZF=1) OR (CF=1).
JMPs for comparison (Contd.)

• GREATER & LESSER are used for signed


numbers & their arithmetic
– JG Jump if Greater (A>B).
i.e. Jump if (ZF=0) AND (SF=OF).
– JGE Jump if Greater or Equal (A≥B).
i.e. Jump if SF=OF.
– JL Jump if Lesser (A<B).
i.e. Jump if SF≠OF.
– JLE Jump if Lesser or Equal (A≤B).
i.e. Jump if (ZF=1) OR (SF≠OF).
Check slides of signed arithmetic for rules.
DATA ADDRESSING MODES
Definition
An instruction may access data in variety
of methods, called data addressing modes.
– Register Addressing
– Immediate Addressing
– Direct Addressing
– Indirect Addressing
– Base Plus Indexed Addressing
– Register Relative Addressing
– Base Relative Addressing
Register Addressing
The designated data item is present in one
of the general purpose register of 8086.
– MOV AL, CL

– MOV DI, DX

– ADD CX, DI

– INC SI

– DEC CX

– SUB AX, CX
Immediate Addressing
The designated data item immediately
follows the Opcode and hence the name
immediate addressing.
– MOV CL, 10

– MOV DI, 2340

– ADD CX, 0AB25H

– Immediate Addressing is primarily used to


specify constants in the program.
Immediate Addressing
– MOV DS, 2000 INVALID, since a segment
register can only be accessed
through Accumulator.
– INC 20 INVALID, since a Constant or
Immediate data can not be
modified.
– MOV CH, 1234 INVALID, Data Size Mismatch
– ADD 20, CL INVALID, since a Constant or
Immediate operand can not be
destination.
Direct Addressing
Effective Address of the data item is specified
directly (i.e. as part of instruction) in the
instruction.
– MOV AL, [2000H]
• Copy 8-bit data into AL from offset address 2000 in data
segment.

– MOV [0A340H], AX
• Copy 16-bit data from AX to offset address A340H in data
segment.

– Both of the above instructions are optimized as


3-byte instruction.
Direct Addressing (Contd.)
– MOV DX, [50A0H]
• Copy 16-bit data into DX from offset address 50A0
in data segment.

• Physical Address (PA) = DS X 10H + 50A0H

• It is a 4-byte instruction.

– MOV ES:[0F340H], AX
• Copy 16-bit data from AX to offset address F340H
in Extra Segment.

• PA = ES X 10H + F340H
A Data Table
• Assume Data Segment Physical Data Comment
Starts at 20000H. Address
• So, DS = 2000H 20000H 30H DATA0
20001H 31H DATA1
Offset of DATA5 from 20002H 32H DATA2
the beginning is
20003H 33H DATA3
0005.
20004H 34H DATA4

So, EA = 0005 or 20005H 35H DATA5


Offset = 0005 20006H 36H DATA6
20007H 37H DATA7
20008H 38H DATA8
20009H 39H DATA9
Direct Addressing (Contd.)
• DATA 5 in the table shown, can be accessed
by following equivalent instructions
– MOV CL, [0005H]
• Copy 8-bit data into CL from offset address 0005 in
data segment.
• Offset Address or Effective Address 0005 is the
address of DATA5.
• PA = DS X 10H + 0005H
– MOV CL, DATA5
• DATA5 is symbol, used to specify memory address.
• This is preferred in Assembly language.
A Simple Program (P1)
Write an 8086 ALP to exchange the content of
two memory locations DATA1 and DATA2 in data
segment.

D_SEG SEGMENT

DATA1 DB 01

DATA2 DB 02

D_SEG ENDS
Program P1 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG
START:
MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV AL, DATA1 ; Copy DATA1 into AL.
MOV DL, DATA2 ; Copy DATA2 into DL.
MOV DATA1, DL ; Copy DL at DATA1.
MOV DATA2, AL ; Copy AL at DATA2.
C_SEG ENDS
END START
XCHG
• XCHG OPR1, OPR2 – Exchange OPR1
with OPR2.
– OPR1  OPR2
– None of the operand can be Immediate.
– Both can not be memory operand.
– E.g. XCHG DX, CX
– Let initially DX = 1234 & CX = 5678
then after Execution of XCHG DX, CX
DX = 5678 & CX = 1234
Alternate Program P1
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG
START: MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV AL, DATA1 ; Copy DATA1 into AL.
MOV DL, DATA2 ; Copy DATA2 into DL.
XCHG AL, DL ; Exchange Data in registers.
MOV DATA1, AL ; Copy AL at DATA1.
MOV DATA2, DL ; Copy DL at DATA2.
C_SEG ENDS
END START
Another Alternate Program P1
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG
START: MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV AL, DATA1 ; Copy DATA1 into AL.
XCHG AL, DATA2 ; Exchange DATA2 with AL.
MOV DATA1, AL ; Copy AL at DATA1.
C_SEG ENDS
END START
Indirect Addressing
Effective Address of the data item is specified
through a register (i.e. indirectly) in the
instruction.
– Registers BX, SI and DI are used as indirect
Registers.
– E.g. MOV CL, [BX]
• Copy 8-bit data into CL from offset address specified by
register BX.
• Content of BX can be changed to access different memory
locations.
Data Table Revisited
• Let DS = 2000H Physical Data Comment
(PA=20000H) Address
Base Address of TABLE 20000H 30H DATA0
So Offset of the base 20001H 31H DATA1
address = 0000 i.e. 20002H 32H DATA2
DATA0 at Offset = 0000
20003H 33H DATA3
DATA1 at Offset = 0001
20004H 34H DATA4
:
If SI = 0005 then 20005H 35H DATA5
MOV CL, [SI] will copy 20006H 36H DATA6
DATA5 in CL. 20007H 37H DATA7
So, PA = DSx10H+SI 20008H 38H DATA8
= 20000H+5 = 20005H 20009H 39H DATA9
Indirect Addressing (Contd.)
– MOV [SI], 20 – Assembler Error
• Data size not clear weather to store 20 in 8-bit or 16-
bit.
• Size should be specified in such cases.
• MOV BYTE PTR [SI], 20 is correct to store 20 as 8-bit
data.
• MOV WORD PTR [SI], 20 is correct to store 20 in 16-
bit data.

– INC [SI] – Similar problem & solution as above.


A Simple Program (P2)
A data table containing 10 data items, named
TABLE1, is stored in data segment. Write an 8086
ALP to copy data from TABLE1 to another table,
namely TABLE2 in data segment.
Program P2 (Contd.)
SI ← Base Address of TABLE1
DI ← Base Address of TABLE2
CX ← Data Count (10)

Get data from TABLE1


i.e. AL ← [SI]

Store data into TABLE2


i.e. [DI] ← AL

Increment Memory Pointers


i.e. SI ← SI +1
DI ← DI + 1

Decrement the data counter

All Data Y
STOP
N Copied?
Program P2 (Contd.)
D_SEG SEGMENT
TABLE1 DB 30H, 31H, 32H, …. , 39H
TABLE2 DB 10 DUP (?)
D_SEG ENDS

C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG
START: MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV CX, 10 ; Load Count
Program P2 (Contd.)
MOV SI, OFFSET TABLE1 ; Get the Base Addresses.
MOV DI, OFFSET TABLE2
AGAIN:
MOV AL, [SI] ; Copy the source Data.
MOV [DI], AL ; Store at destination.
INC SI ; Increment Memory Pointers.
INC DI
DEC CX ; Decrement Data Counter.
JNZ AGAIN ; Go back if not complete.
C_SEG ENDS
END START
LOOP
• LOOP through if count is not zero
– E.g. LOOP ADDRESS
• Decrement the count in register CX
• Jump to ADRESS to execute next instruction if count in
register CX≠0.
• LOOP does not affect the zero flag.
– e.g. LOOPE Address
• LOOP through while Equal i.e. Normal LOOP while ZF=1.
Alternate Opcode LOOPZ ADDRESS.
– e.g. LOOPNE Address
• LOOP through while Not Equal i.e. Normal LOOP while ZF≠1.
Alternate Opcode LOOPNZ ADDRESS.
Alternate Program P2
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG

START: MOV AX, D_SEG ; Load DS Register.


MOV DS, AX
MOV CX, 10 ; Load Count.
MOV SI, OFFSET TABLE1 ; Get the Base Addresses.
MOV DI, OFFSET TABLE2
Alternate Program P2
AGAIN:
MOV AL, [SI] ; Transfer the Data items.
MOV [DI], AL
INC SI ; Increment Memory Pointers.
INC DI
LOOP AGAIN ; Loop through if not done.
C_SEG ENDS
END START
Base Plus Index Addressing
Effective Address of the data item is specified
through a Base register and an Index register
indirectly in the instruction.
– Registers BX, SI and DI are used as indirect
registers to specify Base address & index.
– E.g. MOV DL, [BX+SI]
• Copy 8-bit data into DL from offset address specified by
the addition of registers BX & SI.
• Content of BX (Base Address) can be fixed and SI can be
modified to access different data in table.
Data Table Revisited
• Let DS = 2000H Physical Data Comment
(PA=20000H) Address
Base Address of TABLE 20000H 30H DATA0
So Offset of the base 20001H 31H DATA1
address = 0000 i.e. 20002H 32H DATA2
Get BX = 0000
20003H 33H DATA3
To access 7th data of table,
get SI = 7 20004H 34H DATA4
So, MOV DL, [BX+SI] 20005H 35H DATA5
copies DATA7 in DL. 20006H 36H DATA6
So, EA = BX+SI = 0007 20007H 37H DATA7
and PA = DSx10H+EA 20008H 38H DATA8
= 20000H+7 = 20007H 20009H 39H DATA9
Base + Index Addressing (Contd.)
– MOV CX, ES:[BX+SI]
• Copy 16-bit data into CX from Extra Segment with an
offset formed by addition of BX & SI.

• PA = ES X 10H + BX + SI

– MOV DL, [BP+DI]


• Copy 8-bit data into DL from Stack Segment with an
offset formed by addition of BP & DI.
• PA = SS X 10H + BP + DI
A Simple Program (P3)
A data table containing 10 data items, named
TABLE1, is stored in data segment. Write an 8086
ALP to copy data from TABLE1 to another table,
namely TABLE2 in data segment by using Base Plus
Index addressing.
D_SEG SEGMENT
TABLE1 DB 30H, 31H, 32H, …. , 39H
TABLE2 DB 10 DUP (?)
D_SEG ENDS
Program P3 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG
START: MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV CX, 10 ; Load Count.
MOV BX, 0 ; Load the index.
MOV SI, OFFSET TABLE1 ; Get the Base Addresses.
MOV DI, OFFSET TABLE2
AGAIN: MOV AL, [SI+BX] ; Copy the Data items.
MOV [DI+BX], AL
INC BX ; Increment the Index.
LOOP AGAIN ; Loop through if not done.
C_SEG ENDS
END START
A Simple Program (P4)
A Look-Up table containing ASCII Codes of Decimal
integers is stored in data segment. Write an 8086
ALP to find out ASCII code of a given decimal integer
from this table.
D_SEG SEGMENT
LOOKUP DB 30H, 31H, 32H, …. , 39H
INTEGER DB 6
RESULT DB
D_SEG ENDS
ASCII Code Table
• Let DS = 2000H Physical Data Comment
(PA=20000H) Address
• Base Address of LOOK UP 20000H 30H ASCII_0
TABLE 20001H 31H ASCII_1
20002H 32H ASCII_2
• To reach to the location of
20003H 33H ASCII_3
ASCII code of 6, Add 6
(i.e. given integer) to 20004H 34H ASCII_4
Base address of table. 20005H 35H ASCII_5
20006H 36H ASCII_6
• If Base Addr BX=0000 &
20007H 37H ASCII_7
SI=6 then BX+SI =0006
gives EA for code of 6. 20008H 38H ASCII_8
20009H 39H ASCII_9
Program P4 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG
START: MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV AL, INTEGER ; Get the number in AL.
MOV AH, 0 ; Make AX=Number.
MOV SI, AX ; Get the number in SI.
MOV BX, OFFSET LOOKUP ; Get Base Address.
MOV AL, [BX+SI] ; Get the code in AL.
MOV RESULT, AL ; Store the result.
C_SEG ENDS
END START
XLAT
• Translates AL into a number stored in a
memory table (Look Up Table).
– E.g. XLAT
– AL ← DS:[BX+AL]
• Adds the data content of AL & BX to address a byte in
data segment i.e. EA = BX + AL.
• The addressed byte is then copied into AL.
• Only instruction that adds an 8-bit number to a 16-bit
number.
Alternate Program P4
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG
START: MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV AL, INTEGER ; Get the number in AL
MOV BX, OFFSET LOOKUP ; Get Base Address
XLAT ; Get the code in AL.
MOV RESULT, AL ; Store the result.
C_SEG ENDS
END START
A Simple Program (P5)
Using the ASCII Code Look-Up table in Program 4,
write an 8086 ALP to find out the decimal integer
corresponding to the given ASCII code.

D_SEG SEGMENT
LOOKUP DB 30H, 31H, 32H, …. , 39H
GIVEN DB 32H ; Given ASCII Code
RESULT DB
D_SEG ENDS
ASCII Code Table Revisited
• Let DS = 2000H Physical Data Comment
(PA=20000H) Address
• Base Address of LOOK UP 20000H 30H ASCII_0
TABLE (20000H) 20001H 31H ASCII_1
• Given Code is 32H. 20002H 32H ASCII_2
• Address of 32H is
20003H 33H ASCII_3
20002H.
• Integer = 20002H - 20004H 34H ASCII_4
20000H = 2 20005H 35H ASCII_5
• i.e. Integer = Address of 20006H 36H ASCII_6
GIVEN Code – Base
20007H 37H ASCII_7
Address of Table
20008H 38H ASCII_8
20009H 39H ASCII_9
Program P5 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG
START: MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV BX, OFFSET LOOKUP ; Get Base Address.
MOV DI, BX ; Copy of Base Address.
MOV AL, GIVEN ; Get the given code in AL.
AGAIN: CMP AL, [BX] ; Find out the location of code.
JZ FOUND ; If found go to obtain integer.
INC BX ; Increment pointer to next location.
JMP AGAIN ; Go to check next location.
FOUND: SUB BX, DI ; Obtain corresponding integer
MOV RESULT, BL ; Store the result.
C_SEG ENDS
END START
A Simple Problem (P6)
Write an 8086 ALP to find out square of a given
decimal integer.
• Sol: Use a Look-Up table containing square of all
the 10 decimal integers as below. Program P4
(without any change) will find the square.
D_SEG SEGMENT
LOOKUP DB 0, 1, 4, 9, 16, …., 64, 81
INTEGER DB 4
RESULT DB
D_SEG ENDS
Register Relative Addressing
In this mode, the data is present at a location
relative to one of the indirect registers of 8086.
– Effective address of data is specified by adding a
displacement to one of indirect registers.

– E.g. MOV DL, [BX+100]


• Copy 8-bit data into DL from offset address specified by
the addition of registers BX & 100.
Data Table Revisited
• Let DS = 2000H Physical Data Comment
(PA=20000H) Address
• Base Address of TABLE 20000H 00 Square_0
• So in Data Segment, 20001H 01 Square_1
Base Address = 0000 20002H 04 Square_2
• May use symbol TABLE
20003H 09 Square_3
for base address.
• Take SI = 3, to access 20004H 16 Square_4
3rd data in this table. 20005H 25 Square_5
20006H 36 Square_6
20007H 49 Square_7
20008H 64 Square_8
20009H 81 Square_9
Register Relative (Contd.)
• DATA 3 in the shown table can be accessed by
following equivalent instructions.
– MOV CL, [SI+0000] ; SI = 3
• Copy 8-bit data into CL from offset address
0003 in data segment.
• It means data is present at 0th location,
relative to SI.

– MOV CL, TABLE [SI]


• TABLE is the base address of the data table.
A Simple Program (P7)
Write an 8086 ALP to find out square of a
given decimal integer using register relative
addressing mode.

D_SEG SEGMENT
LOOKUP DB 0, 1, 4, 9, 16, …., 64, 81
INTEGER DB 4
RESULT DB
D_SEG ENDS
Program P7 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG
START: MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV BL, INTEGER ; Get the integer in BL.
MOV BH, 0 ; Make BX = INTEGER.
MOV AL, LOOKUP [BX] ; Get the square in AL.
MOV RESULT, AL ; Store the result.
C_SEG ENDS
END START
A Simple Program (P8)
Write an 8086 ALP that adds data of two tables and
store the result in another table in memory. Assume
that table contains 5 data.

D_SEG SEGMENT
TABLE1 DB 00, 01, 02, 03, 04, 05
TABLE2 DB 55, 56, 57, 58, 59, 60
RESULT DB 5 DUP(00)
D_SEG ENDS
Program P8 (Contd.)
SI ← 0, Start with Index 0
CX ← Data Count (5)

Get data from TABLE1


i.e. AL ← TABLE1[SI]

ADD with Data of TABLE2


i.e. AL← AL+TABLE2[SI]

Store the Result in TABLE3

Increment Data Index


Decrement the data counter

All Data Y
STOP
N Added?
Program P8 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG
START: MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV CX, 5 ; Load Data Count
MOV SI, 0 ; Data Index 0 i.e. element 0.
BACK: MOV AL, TABLE1[SI] ; Add the table data.
ADD AL, TABLE2[SI]
MOV RESULT[SI], AL ; Store result.
INC SI ; Provision for next element.
LOOP BACK ; Go back if not done.
C_SEG ENDS
END START
Base Relative Plus Index Addressing
In this mode, the data is present at a location
relative to a Base and an Index register.
– Effective address of data is specified by adding a
displacement to Base & Index registers.

– E.g. MOV DL, [BX+SI+100]


• Copy 8-bit data into DL from offset address specified by
the addition of registers BX & 100.
Base Relative + Index (Contd.)
– MOV LIST[BX+DI], AX
• Copy 16-bit data from AX into Data Segment with an
offset given as EA = LIST+BX+DI.

• PA = DS X 10H + LIST + BX + DI

– MOV CL, [BP+DI-7]


• Copy 8-bit data into DL from Stack Segment with an
offset given as EA = BP+DI-7.
• PA = SS X 10H + BP + DI - 7
STRINGs
Strings are group of characters that are to be
processed as a whole.
– To display on CRT processor treats complete display
information as one string.
– Usually the number of bytes in such strings amounts to
screen resolution.
– B/W Display of 50 lines with 80-characters per lines may
need 50X80=4000 bytes to be transferred at once.
– Such transfers should be efficient and normal MOVs may
not be appropriate.
SI & DI Registers
• SI – Source Index Register
– A string character is read out from Data Segment
through SI into Accumulator.
– After every read of string character, SI is incremented
or decremented automatically depending upon the
status of DF.
• DI – Destination Index Register
– A string character is stored from Accumulator in Extra
Segment through DI.
– After every write of string character, DI is incremented
or decremented automatically depending upon the
status of DF.
DF - Direction Flag (Revisited)
− Strings may be processed, first to last character
(Forward Direction).
− Strings may be processed, last to First character
(Backward Direction).
− String Direction flag DF Decides the direction of
string processing.
Address Data
Forward Direction 20000H A Backward Direction
A to Y Y to A
20001H J
Auto Increment Auto Decrement
Mode DF = 0 20002H A Mode DF = 1
20003H Y
DF - Direction Flag (Contd.)
• CLD – Clear Direction Flag i.e. DF = 0.
– DF=0 selects Auto Increment mode for string
indexing registers.
– After every string character read or write, SI or DI
respectively, will be incremented automatically.
• STD – Set Direction Flag i.e. DF = 1.
– DF=1 selects Auto Decrement mode for string
indexing registers.
– After every string character read or write, SI or DI
respectively, will be decremented automatically.
LODS
• LODSB – Load String Byte from Data
Segment through SI into Accumulator
– AL ← DS:[SI],
– SI ← SI ± 1, depending upon DF.

• LODSW – Load String Word from Data


Segment through SI into Accumulator
– AX ← DS:[SI],
– SI ← SI ± 2, depending upon DF.
STOS
• STOSB – Store String Byte in Extra Segment
through DI from Accumulator
– ES:[DI] ← AL,
– DI ← DI ± 1, depending upon DF.
• STOSW – Store String Word in Extra
Segment through DI from Accumulator
– ES:[DI] ← AX,
– DI ← DI ± 2, depending upon DF.
A Simple Program (P9)
A video text display unit contains 25-line-by-80-
character per line display. Each character needs
two bytes of information to be displayed. The
display information should be stored at address
B800H:0000. Write an 8086 ALP to clear this
display.

Given that, 00 is code for NULL character and 07 is


the code for black color background. So store 0700
to clear display.
Program P9 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG
START: MOV AX, 0B80H ; Segment address for Display in ES.
MOV ES, AX
MOV DI, 0 ; Load destination offset
MOV CX, 2000 ; String Count= 25X80=2000
MOV AX,0700 ; Load Display code in AX
AGAIN: STOSW ; Store Display code
LOOP AGAIN ; Repeat for all characters
C_SEG ENDS
END START
REP PREFIX
• REP – Repeat String Instruction while count
is not zero.
– REP STOSB
• Store the string byte.
• DI ← DI ± 1, depending upon DF.
• Decrement the count in register CX.
• Repeat the above steps if count in CX is not zero.
– REP is not Applicable to LODS.
Alternate Program P9 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG
START: MOV AX, 0B80H ; Segment address for Display in ES.
MOV ES, AX
MOV DI, 0 ; Load destination offset
MOV CX, 2000 ; String Count= 25X80=2000
MOV AX,0700 ; Load Display code in AX
REP STOSW ; Store all the Display codes.
C_SEG ENDS
END START
A Simple Program (P10)
A character string “AJAY,” is stored in data
segment. Write an 8086 ALP to copy this string in
extra segment.
D_SEG SEGMENT
STR1 DB ‘A’, ‘J’, ‘A’, ‘Y’
D_SEG ENDS
E_SEG SEGMENT
STR2 DB 4 DUP (00)
E_SEG ENDS
Program P10 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG, ES:E_SEG
START: MOV AX, E_SEG ; Load Extra Segment Register.
MOV ES, AX
MOV AX, D_SEG ; Load Data Segment Register.
MOV DS, AX
MOV SI, OFFSET STR1 ; Load Source offset.
MOV DI, OFFSET STR2 ; Load Destination offset.
MOV CX, 4 ; String Count in CX.
CLD ; Auto Increment mode.
AGAIN: LODSB ; Load string character in AL.
STOSB ; Store string character.
LOOP AGAIN ; Repeat for all characters.
C_SEG ENDS
END START
MOVS
• MOVSB – Copy String Byte in Extra Segment
through DI from Data Segment through SI.
– ES:[DI] ← DS:[SI], 8-bit transfer
– DI ← DI ± 1, SI ← SI ± 1 depending upon DF.
• MOVSW – Copy String WORD in Extra
Segment through DI from Data Segment
through SI.
– ES:[DI] ← DS:[SI], 16-bit transfer
– DI ← DI ± 2, SI ← SI ± 2, depending upon DF.
Alternate Program P10
C_SEG SEGMENT
ASSUME CS:C_SEG , DS:D_SEG, ES:E_SEG
START: MOV AX, E_SEG ; Load ES Register.
MOV ES, AX
MOV AX, D_SEG ; Load DS Register.
MOV DS, AX
MOV SI, OFFSET STR1 ; Load Source offset.
MOV DI, OFFSET STR2 ; Load Destination offset.
MOV CX, 4 ; String Count.
CLD ; Auto Increment mode.
REP MOVSB ; Transfer string characters.
C_SEG ENDS
END START
A Simple Program (P11)
A character string “AJAY,” is stored in data
segment. Write an 8086 ALP to copy this string at
some other location in data segment using MOVS
instruction.

D_SEG SEGMENT
STR1 DB ‘A’, ‘J’, ‘A’, ‘Y’
STR2 DB 4 DUP (00)
D_SEG ENDS
SCAS
• Scan the string character.
• It scans a block of memory addressed by DI in Extra
Segment for the string character present in AL or AX. In
all the cases, the content of memory is subtracted from
Accumulator without affecting the memory or
Accumulator.
• SCASB – Scan String Byte in Extra Segment through DI.
– FLAG ← AL – [ES:DI]
– DI ← DI ± 1, SI ← SI ± 1 depending upon DF.
• SCASW – Scan String Word in Extra Segment through DI.
– FLAG ← AX – [ES:DI]
– DI ← DI ± 2, DI ← DI ± 2, depending upon DF.
REP PREFIX (Revisited)
• Conditional REP – Repeat String Instruction
while count is not zero and while condition is
met.
– REPE STOSB – REP while equal. Normal REP while
ZF=1. Alternate Opcode REPZ.
– REPNE STOSB - REP while not equal. Normal REP
while ZF≠1. Alternately, REPNZ.
– The conditional REP are allowed with string
instructions that affects flags, such as SCAS . If used
with MOVS or STOS, they work as normal REP.
A Simple Program (P12)
100 ASCII characters are present in Extra segment.
Write a program that searches for character ‘A’ in this
string. Store ‘Y’ if found else store ‘N’ if not found.
D_SEG SEGMENT
RESULT1 DB ‘N’ ; Default is NOT FOUND
D_SEG ENDS
E_SEG SEGMENT
TARGET DB 76, 48, 99, 119, ………….65
E_SEG ENDS
Program P12 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG , DS:D_SEG, ES:E_SEG
START: MOV AX, E_SEG ; Load ES Register
MOV ES, AX
MOV AX, D_SEG ; Load DS Register
MOV DS, AX
CLD ; Auto Increment mode.
MOV DI, OFFSET TARGET ; Load offset. LEA DI, TARGET
MOV CX, 100 ; Count of Character
MOV AL, ‘A’ ; Load Character to be search.
REPNE SCASB ; Search all characters while match not found.
JNZ DN1 ; Match Found? NO!! Go to end.
MOV RESULT1, ‘Y’ ; YES!! Match Found, Store ‘Y’ as result
DN1: NOP ; Put NOP or may be blank.
C_SEG ENDS
END START
* The above program is called as linear search. We later see binary search or dictionary search.
LEA - Load Effective Address
– LEA DI, TARGET Load Effective Address of symbol
TARGET in DI.
Same as MOV DI, OFFSET TARGET.
– LEA BX, [DI] Load Effective Address or Offset specified
by DI in BX.
Same as MOV BX, DI
– LEA DI, [BX+SI] Load Effective Address or Offset specified
by adding BX & SI in DI.
– LEA BX, TABLE[SI] Load Effective Address or Offset specified

by adding SI & displacement TABLE in BX.

* LEA is a processor instruction while OFFSET is assembler directive.


CMPS
• Compare the string characters.
• It compares the block of memory addressed by
SI in Data Segment with the block of memory
addressed by DI in Extra Segment.
• CMPSB – Compare String Byte.
– FLAG ← [DS:SI] – [ES:DI]
– DI ← DI ± 1, SI ← SI ± 1 depending upon DF.
• SCASW – Compare String Word.
– FLAG ← [DS:SI] – [ES:DI]
– DI ← DI ± 2, DI ← DI ± 2, depending upon DF.
A Simple Program (P13)
100 ASCII characters are stored in data segment. Write a
program that copies these characters to extra segment and then
checks weather transfer is done correctly or not? Store ‘E’ if error
found else store ‘G’ if no error found.
D_SEG SEGMENT
ASCII_STR DB 76, 48, 99, 119, ………….65
RESULT2 DB ‘G’ ; Default is GOOD or NO ERROR
D_SEG ENDS
E_SEG SEGMENT
TARGET DB 100 DUP(00)
E_SEG ENDS
Program P13 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG , DS:D_SEG, ES:E_SEG
START: MOV AX, E_SEG ; Load ES Register
MOV ES, AX
MOV AX, D_SEG ; Load DS Register
MOV DS, AX
CLD ; Auto Increment mode.
MOV SI, OFFSET ASCII_STR ; Load offset of source.
MOV DI, OFFSET TARGET ; Load offset of destination.
MOV CX, 100 ; Count of Character.
REP MOVSB ; Transfer string characters.
LEA SI, ASCII_STR ; Load offset of source.
LEA DI, TARGET ; Load offset of destination.
MOV CX, 100 ; Count of Character.
REPE CMPSB ; Compare string characters.
JZ DN1 ; Error Found? NO!! Go to end.
MOV RESULT2, ‘E’ ; YES!! Error Found, Store ‘E’ as result.
DN1: NOP ; Put NOP or may be blank.
C_SEG ENDS
END START

You might also like