Beruflich Dokumente
Kultur Dokumente
MICROPROCESSOR 8086
Data Flow
Instructions
Binary Codes Microprocessor Processed
Hexadecimal Programs
(Data Processor) Data
Codes
Mneumonics
Low Level
Language/Machine
Language
Hexadecimal Code
language
Assembly Language Input Data
High Level Language • Memory
• IO Device
Assembler, Linker and Loader
Assembly Language
Machine Language
program ASSEMBLER MEMORY
Modules
Object Code:
machine Language
Module 1
Executable:
Object Code: LINKER Machine Language LOADER
machine Language Program
Module 2
Status X Undefined
Flags CF Carry Flag, set if carry generated from msb
PF Parity Flag, Set if result has Even Parity
AF Auxiliary Flag set from lower nibble, used for BCD operation
ZF Zero Flag, set if result is zero
SF Sign Flag set is msb is copied (i.e. result is negative)
OF Over flow Flag for signed result, if result is out of range
Control TF Trap Flag; for single stepping Mode
Flags IF Interrupt Flag, set to recognize maskable interrupt request for enabling
interrupt
DF Direction flag for string operation
Instructions
XCHG
XCHG reg/mem, reg
XCHG AX, REG16
PUSH/POP instruction
PUSH reg16/mem16, POP reg16/mem16
PUSH segreg, POP segreg
STACK MEMORY SEGMENT
PUSHF, POPF
Lower 8 bits of
16 bit flag AH
register
SAHF: Store Register AH to 8 LSBs of Flag Register Data. All 8085 flags are
Changed
Lower 8 bits of
AH 16 bit flag
register
XLAT
Translate or convert any 8 bit old code into any other 8 bit new code using
codes look up table
This is a specialized data transfer instruction to read new code from lookup
table whose position is indexed by the value of old code
MOX AX, 7000H
LA Memory DS=7000H MOV DS, AX
XXXX:XXXX XX BX=0000H MOV BX, 0000H
7000:0000H FCH AL=04H MOV AL, 04H
XLAT
7000:0001H 60H HLT
7000:0002H DAH
7000:0003H F2H XLAT AL
7000:0004H 66H 66H
7000:0005H B6H
DS:[BX+AL] AL
XXXX:XXXX XX
Steps of initializations before using XLAT
Transfer SBA of DMS in DS register
Initialize LUT in DMS
Load the EA of the starting location of LUT in register BX
The 8 bit old code for which we have to obtain new code should be stored in
AL
A program to read an LUT that starts from PA 65007H and read new code for the old code
02h:
Code_segment SEGMENT
ASSUME CS: Code_segment, DS: Data_segment
start:
; set segment registers:
MOV AX, Data_segment
MOV DS, AX
; add your code here
LEA BX, LUT
MOV AL, OC
XLAT
MOV NC, AL
D(8/16) S(8/16)
Addition (8/ 16 bit)
REG MEM
ADD D, S
MEM REG
ADC D, S
REG REG
DAA
REG DATA
AAA
MEM DATA
(i) ADD D, S
Operation
D←D+S
All flags are affected
(ii) ADC D, S
Operation
D←D+S+CF (carry flag)
All flags are affected
DAA (Decimal adjust after addition)
(iii) DAA
1. Decimal Adjust after Addition
2. Used to convert Binary addition results of Packed BCD numbers into
a valid BCD addition results in Packed BCD format.
3. Implicit addressing mode. Modifies the data of AL.
4. Used after ADD/ADC instructions
Logic
1. If (Lower Nibble of AL) > 9 or AC=1
Then:
- Add 6H to lower nibble of AL
- Make AC=1
2. If (Higher nibble of AL) >9 or CF = 1
Then:
- Add 6H to higher nibble of AL
- Make CF = 1
Example
39H+42H should give using ADD instruction a binary addition result of 7BH.
However if 39H and 42H are indicated as their BCD equivalents; 39D and 42D
respectively, then the results of addition of these two BCD numbers should
have been 81H.
However, addition of these two BCD numbers in microprocessor using ADD
instruction will be 7BH, which is an invalid BCD result.
Invoking the DAA, will adjust the invalid BCD result, 7BH in AL; into a valid
BCD addition result in packed BCD format i.e. 81H.
Example Program for DAA
Theoretical result of BCD result 39D+42D=81D
Data_Seg SEGMENT
OP1 DB 39H, 42H
RES DB 2DUP (?)
Data_Seg ENDS
Code_Seg SEGMENT
ASSUME CS: Code_Seg, DS: Data_Seg
Start:MOV AX, Data_Seg
MOV DS, AX
MOV AL, OP1
ADD AL, OP1+1
DAA
MOV AH, 00H
ADC AH, 00H
MOV RES, AL
MOV RES+1, AH
MOV AX, 4C00H
INT 21H
Code_Seg ENDS
END Start
Illustrative Programs
Add two decimal numbers 89 and 42 present in extra memory segment
from EA 3000H. Store the result in extra memory segment
Extra_Seg SEGMENT
OP1 DB 89H, 42H
RES DB 2 DUP ?
Extra_Seg ENDS
Code_Seg SEGMENT
ASSUME CS: Code_Seg, ES: Extra_Seg
Start:MOV AX, Extra_Seg
MOV ES, AX
MOV AL, ES: OP1
ADD AL, ES: OP1+1
DAA
MOV AH, 00H
ADC AH, 00H
MOV ES: RES, AL
MOV ES: RES+1, AH
MOV AX, 4C00H
INT 21H
Code_Seg ENDS
END Start
Code_Seg SEGMENT
Illustrative Programs ASSUME CS: Code_Seg, DS: Data_Seg, ES: Extra_Seg
Start: MOV AX, Data_Seg
Store two 100 digit DECIMAL numbers in MOV DS, AX
data memory segment using assembler MOV AX, Extra_Seg
directives. Write a program to add these MOV ES, AX
two numbers and store the result of LEA SI, OP1
addition in extra memory segment. LEA BX, OP2
100 digit decimal number in packed BCD LEA DI, Res
format means 50 bytes of data. As 2 MOV CL, 32H
decimal digits takes one Byte in packed CLC
BCD format(e.g 99D is represented in HORZADD:MOV AL, [SI]
packed BCD format as 99H) (remember in
ADC AL, [BX]
Unpacked BCD format 1 decimal digit is
represented as a 1 Byte BCD, e.g 99 D is
DAA
represented as 0909H). Therefore, As a MOV ES:[DI], AL
solution to the first part of the program, INC SI
Use assembler directive DB (Define Byte) INC BX
to store 50 bytes of data. INC DI
DEC CL
Solution:
JNZ HORZADD
Data_Seg SEGMENT MOV AH, 00H
OP1 DB 50 DUP (89H) ADC ES:[DI], 00H
OP2 DB 50 DUP (42H) MOV AX, 4C00H
Data_Seg ENDS INT 21H
Extra_Seg SEGMENT Code_Seg ENDS
Res DB 51 DUP (?) END Start
Extra_Seg ENDS
AAA
ASCII Adjust after Addition Logic
Used to convert Binary addition results 1. If (Lower Nibble of AL) > 9 or AC=1
of ASCII numbers into a valid BCD Then:
addition results in unpacked BCD format. • Add 6H to lower nibble of AL
Implicit addressing mode. Modifies the • Make AC=1 and CF = 1
data of AL. • AH = AH+1 (Increment AH)
Used after ADD/ADC instructions 2. Mask the higher nibble of AL
Note:
(i) If after the execution of DAS instruction, CF=0 the result of subtraction is +ve
(ii) If after the execution of DAS instruction, CF=1, then the result of subtraction is –ve and the result is
obtained in 10’s complement form. (10’s complement method is decimal analogue of 2’s complement
method)
Example: Write a program to store 4 bytes of two multi-byte decimal
numbers in DMS. Write a program to subtract these two numbers and Store
the results in EMS
Data_seg segment iter: mov al, [si]
; add your data here! sbb al, [bx]
op1 db 4 dup (39h) das
op2 db 4 dup (42h) mov es:[di], al
Data_seg ends inc si
Extra_seg segment inc bx
op3 db 5 dup(?) inc di
Extra_seg ends loop iter
mov al, 0
code segment adc al, 0
start: mov es:[di], al; Store
; set segment registers: last borrow
mov ax, Data_seg
mov ds, ax mov ax, 4c00h ; exit to
mov ax, Extra_seg operating system.
mov es, ax int 21h
lea si, op1 code ends
lea bx, op2 end start
lea di, op3
clc
mov cx,04h
AAS (Ascii adjust after addition)
ASCII Adjust after Subtraction Logic
Used to convert Binary subtraction results of ASCII 1. If (Lower Nibble of AL) > 9 or AC=1
numbers into a valid BCD subtraction result in Then:
unpacked BCD format. Subtract 6H from lower nibble of AL
Implicit addressing mode. Modifies the data of AL. Make AC=1 and CF = 1
AH = AH-1 (Decrement AH)
Used after SUB/SBB instructions
2. Mask the higher nibble of AL
If the subtraction result is negative the
difference is obtained in 10’s complement
format.
Note: AAM is also used to convert any binary number less than or equal to 63h in
AL into an UPBCD (0909h) number in AX.
Problems:
The problems involving AAM instruction could take one of the following
forms:
1. Multiply two ASCII numbers and represent result in ASCII
Steps: subtract 30h from operands, multiply two operands and get product in AL, AAM and then
ADD 3030H with the AX
2. Multiply two UPBCD numbers and represent result in UPBCD
Steps: Multiply two UPBCD operands and get product in AL, AAM
3. Multiply two UPBCD numbers and represent result in packed BCD
Steps: Multiply two UPBCD operands and get product in AL, AAM, and convert UPBCD result to
packed BCD
4. Convert Binary or Hexadecimal number (00H to 63H) to decimal/Packed BCD numbers
(00D-99D)
Steps: Load the hex number in AL, AAM, Convert UPBCD number in AX to Packed BCD number in
AL
AAD
ASCII adjust before division
Implicit addressing mode
Converts a UPBCD number in AX into a Binary number in AL
This instruction is called before the division process.
Result obtained will be correct if the quotient is of single
digit that is from 0-9.
Logic
(i) AH × (0A)H + AL → AL & (ii) AH←00H
Example:
0402h is the UPBCD representation of 42D. The binary
representation of 42D is 2AH. Invocation of AAD on 0402H (in AX)
data will change the value of AL to 2AH and AH will be reset.
Given following code:
MOV AX, 0402H
00H 04h × (0A)H+02H = 2AH
AAD
AH AL
Results in following operation on AH and AL
Problem variations:
Conversion of a 2 digit ASCII number into binary:
E.g. Convert ASCII 94 into binary
MOV AH, ‘9’
MOV AL, ‘4’
SUB AX, 3030H
AAD
Conversion of 2 digit UPBCD number into binary:
Convert UPBCD of 94d in binary
MOV AX, 0904h
AAD
Conversion of 2 digit decimal number (Packed BCD) into binary:
MOV AL, 94H
MOV AH, AL
AND AL, 0FH; AL=04H
AND AH, F0H; AH= 90H
MOV CL, 04H; load shift count value
SHR AH, CL; AL=09H, Shift logical right AH data across by 4 bit positions
AAD; AL now contains binary of 94D.
Byte and word extension instructions
Soln:
8 bit register cannot be multiplied with 16 bit register.
Thus the unsigned data of 8 bit register has to be extended to 16 bit.
Data Extension rule: Zero Padding for unsigned data
MUL instruction expects unsigned operands; hence the higher order byte of
16 bit equivalent of data in DL has to be composed of all zeroes.
Codes
MOV AH, 00H
MOV AL, DL
MUL SI; Product in DX-AX pair
IMUL (reg8/16)/mem(8/16)
Used for multiplication of Signed numbers. The range of signed results will be –(215)--0--+(215-1) (for
16 bit result, i.e. during multiplication of 8 bit operands) or -(231)--0--+(231-1) (for 32 bit result, i.e.
during multiplication of 16 bit operands).
The operands/results should be stored in registers or memory in a sign extended binary in 2’s
complement format. The result obtained would be in twos complement format for negative numbers.
The MSB of result and operand indicates the sign; 1-negative, 0-positive.
The content of accumulator(AL for 8 bit operand/or AX for 16 bit operand) and register/memory
(8/16) bit are multiplied and result is stored in AX (for 16 bit result) or DX-AX pair (for 32 bit result in
case of multiplication of 16 bit operands).
For byte operands
MUL reg8/mem8
(AX)←(AL)×(reg8/mem8)
For word operands
MUL reg16/mem16
(DX-AX)←(AX)×(reg16/mem16)
Note: How the Flags are affected during IMUL instruction
(i) For 16 bit result (for 8 bit operands), the higher order byte of result is stored in AH and
lower byte in AL.
a) When the data of AH is not the sign bit extension of AL then CF=OF=1, which means
that the magnitude of result is larger than 8 bits of AL. In this case the data of AX is
the result.
b) if AH is composed of sign bit extension of AL, then CF=OF=0, which means that the
magnitude of signed result is confined within AL. In this case the data of AL may be
considered as the product.
c) Thus whenever the size of the signed product crosses 8 bits of AL then CF & OF becomes 1.
(ii) For 32 bit result, the higher order word of result is stored in DX and lower word in AX.
a) When the data of DX is not the sign bit extension of AX then CF=OF=1, which means
that the magnitude of result is larger than 16 bits of AX. In this case the data of DX-
AX pair is the result
b) if DX is composed of sign bit extension of AX, then CF=OF=0, which means that the
magnitude of signed result is confined within AX.
c) In summary, whenever the size of the signed product crosses 16 bits of AX then CF & OF
becomes 1 and we have to consider the product as full 32 bits of DX-AX pair.
Prog: Perform multiplication of 8-bit signed number in with a 16
bit signed number present in DMS. Store 32 bit signed result in
DMS
Data SEGMENT
OP1 DB -12
OP2 DW 3456H
OP3 DD ?
Data ENDS
Code SEGMENT
ASSUME CS: Code, DS: Data
Start: MOV AX, Data
MOV DS, AX
MOV AL, OP1
CBW; CONVERT SIGNED BYTE TO WORD BY SIGN BIT EXTENSION
IMUL OP2
MOV WORD PTR OP3, AX
MOV WORD PTR OP3+2, DX
MOV AX, 4C00H
INT 21H
Code ENDS
END Start
DIV S NOTE:
1. If the quotient exceeds the size of
S (reg8/16)/mem(8/16), Not Immediate Data destination register like AL and AX
No flags are changed then INTO (8086 dedicated
interrupt 4; INT 4, for division
Division of unsigned operands and the result is also unsigned overflow error) is executed. The
subprogram of INTO is used to
Case1: 16 bit number divided by 8 bit number display error message on output
screen regarding division overflow.
REMAINDER(8 bits) 2. If a valid quotient is obtained, the
AX (16 bit accumulator) AH 8/16 bit quotient is obtained in AL
Source register/memory QUOTIENT (8 bits) or AX respectively
(8 Bits) AL
CF=0, OF=0,AC=Not Affected, Rest of the status flags are modified according to the result
Rotate and shift instructions
SAL Reg/Mem(8/16), 1
Shift Operation in 8086: SAL Reg(8/16), CL
Shifts the byte or word in a register/Memory by one or
multiple places.
Last outgoing bit is copied to CF and rest outgoing bits
are lost AH
Incoming bits are predefined to either zero or sign bit 1 0 1 1 1 0 0 1 =-71
SAL (Shift arithmetic left)
SAR (Shift arithmetic right)
SHL (Shift Logical left, Incoming bit is always zero, Same MSB 1 1 0 1 1 1 0 0 1
as SHL)
SHR (Shift logical right, Incoming bit is always zero) CF
SAR AH, 01
0 0 1 1 1 0 0 1 =-36
=57 Note for SAR
1. The conservation of sign bit for shift arithmetic
right operation. Incoming bit is the sign bit
2. The division by 2 of the number
1 0 1 1 1 0 0 1 0 0 Note for SAL
1. Incoming bit is always 0
=114
2. The multiplication by 2 of the number
CF SAL AH, 01
Rotate Instructions
ROR (Rotate right without carry)
ROL (Rotate left without carry) ROR Reg/Mem(8/16), 1
ROR Reg(8/16), CL
RCR (Rotate right with carry)
RCL (Rotate left with carry)
AH
B7 B6 B5 B4 B3 B2 B1 B0 CF
ROR AH, 01
RCR AH, 01
B7 B6 B5 B4 B3 B2 B1 B0 AH
CF
Summary of shift instructions
Instructions Feature of instructions
Shift Arithmatic Left
SAL • Shift arithmetic left the 8/16 bit data in Register/Memory specified.
• Outgoing bit is MSB. Thus, MSB is copied into CF.
• New bits inserted at LSB position is 0.
• Has an effect of multiplication by 2Shift Count.
Shift Logical Left
SHL • Shift Logical left the 8/16 bit data in Register/Memory specified.
• Outgoing bit is MSB. Thus, MSB is copied into CF.
• New bits inserted at LSB position is 0.
• Has an effect of multiplication by 2Shift Count.
• SAME IN OPERATION AS SAL
Shift Arithmatic Right
SAR • Shift Arithmetic Right the 8/16 bit data in Register/memory specified.
• Outgoing bit is LSB. Thus, LSB is copied into CF.
• New bits inserted at MSB position is THE SIGN BIT i.e. previous bit of MSB is copied and inserted as new bit in MSB position.
• Has an effect of Division by 2Shift Count.
• Meaningful for Signed numbers.