Professional Documents
Culture Documents
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
OF DF IF TF SF ZF X AC X PF X CF
OF DF IF TF SF ZF X AC X PF X CF
OF DF IF TF SF ZF X AC X PF X CF
+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
OF DF IF TF SF ZF X AC X PF X CF
• 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.)
• 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.)
• 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
– 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 [0A340H], AX
• Copy 16-bit data from AX to offset address A340H in data
segment.
• 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
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.
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
• PA = ES X 10H + BX + SI
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.
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)
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.
• PA = DS X 10H + LIST + BX + DI
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