Unit III@ECE
Unit III@ECE
Topics:
8086 Architecture Functional diagram, Registers, Programming Model, Memory-
addresses and organization, Signal Descriptions and
Interrupts.
Instruction Set and Instruction formats, Addressing modes, Instruction set,
Assembly Language Assembler Directives, Macros, Programming- Control flow
Programming of 8086 instructions, sorting, and string manipulations.
Introduction :
AH AL AX Decoding
BH BL BX General
Purpose ALU (16) Circuit
CH CL CX
DH DL DX Regsiters
SP
BP FLAGS (16)
SI Execution
Timing and
DI Unit
Control Circuit
Fig.1.1: Architecture/ Functional Diagram of 8086 Clock and Control Signals
8086 Architecture
15 8 7 0 15 0
Register Organization
AH AL AX
• General Purpose registers
BH BL BX
• Segment registers
CH CL CX
• Point and Index registers DH DL DX
• Flags/ Program Status Word (PSW)
Fig.1.1(a): General purpose registers
• Instruction queue byte (6 bytes)
General Purpose registers (GPR)
• 4 GPRs are available (AX, BX, CX, & DX)
• Each of 16-bit, can be used as two 8-bit registers each (H, L)
• Used for temporary data (variables & results) holding, counter, offset address
• Also used as implicit operand or destination
8086 Architecture
General Purpose registers (GPR)
• AX (16): AH (8) & AL (8): Accumulator
• BX (16): BH (8) & BL (8): Base register (stores the offset values)
• CX (16): CH (8) & CL (8): Counter
• DX (16): DH (8) & DL (8): Data
Segment registers (SR) 15 0
• 4 segment registers – CS, DS, SS, & ES CS
• Memory in 8086 is divided into 16 logical segments each of 64 Kb DS
• CS: used for addressing a memory location in the code segment SS
• SS: used for addressing a memory location in the stack segment ES
• DS: locates an address in data segment
Fig.1.1(b): Segment registers
• ES: also points to a location in data (extra) segment
8086 Architecture
15 0
Pointers and Index Registers
SP
• SP,BP & IP: points to an offset in stack segment and code
BP
segment respectively
SI
• Index registers SI (source index) & DI (destination index) can be DI
used as general data registers
IP
• Particularly useful for string manipulation operations
• Also used for offset storage in Indexed, Based Indexed,
Fig.1.1(c): Pointers & Index registers
and Relative Based Indexed addressing modes
Flags/ PSW
• Indicates the status of the result
• Appeared as flag set (1) or reset (0)
8086 Architecture
Flags/ PSW
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
X X X X O D I T S Z X Ac X P X C
Fig.1.1(d): Flags
• Condition code/ Status flags – Lower byte of the flags register (S, Z, Ac, P, C) plus the
overflow flag (O)
• Reflect the results of the ALU operations
• Control flags: higher byte of the PSW
• Consists of three flags – D, I, and T
• S-sign flag: sets when the result is negative and for signed operations, the MSB
indicates to the sign bit.
• Z-zero flag: sets when the result of an operation (arithmetic or logical) is a zero.
• P-parity flag: sets when the lower byte of the result contains even number of 1s.
8086 Architecture
• C-carry flag: sets when the there is carry/ borrow out of the MSB and useful for sorting.
• O-overflow flag: sets if the result of a signed operation is large enough to fit in a
destination register.
• T-trap flag: processor enters the single step execution when sets. It is an interrupt.
• I-interrupt flag: sets when a maskable interrupt is recognized by the CPU, else this will
be ignored.
• D- direction flag:
• used by string manipulation instructions. Indicates the direction of string accessing.
• set: auto-decrement mode – from higher offset to lower offset.
• Reset: auto-increment mode – from lower offset to higher offset.
• Ac- Auxiliary carry flag: sets if there is a carry from the lowest nibble.
8086 Architecture
Physical Address (PA) generation
FFFFH
Fig.1.2: Memory Segmentation
Non-overlapping Overlapping
Segmentation Segmentation
Physical Memory Organization
• 1 MB Memory: Odd bank (512 KB) and Even bank (512 KB)
• Byte data with an even address transferred on D7 – D0 while the odd
address transferred on D15 – D8.
D8 – D15
D0 – D7..
ISR IP 0000:03FE
Type 3FF
ISR CS 0000:03FF
Assembly Language Instruction Formats
&
Addressing Modes
Machine language Instruction Formats
Machine language instruction consists of
Operational code or opcode Destination Source
Opcode
Operand Operand
Operands
Operational code/opcode:
W D S V Z REG
Fig.1.5: Single byte instruction format
W-bit: indicates the length of the operands
0: 8-bit operands
1: 16-bit operands
D-bit: valid in case of double operand instructions
One operand must be register specified by the REG field
0: REG is source operand and 1: REG is the destination operand
Machine language Instruction Formats
S-bit: sign extension bit
One byte Instruction: Only of 8-bit length as shown in Fig.1.5.
Tab.1.6: S & W bits description
S W Indication
8-bit operation with 8-bit immediate
0 0 operand
16-bit operation with 16-bit immediate
0 1 operand
8-bit operation with sign extended
1 0 immediate data
16-bit operation with sign extended
1 1 immediate data
Machine language Instruction Formats
V-bit: indicates the no. of shift and rotations to take place on the given
operand.
0: single shift/ rotation
1: CL contains the count
Z-bit: used by REP instruction to control the loop.
1: instruction with REP prefix will execute until the zero flag matches the
Z-bit.
0: REP prefix will not be executed.
Machine language Instruction Formats
Tab.1.7: Register codes
0 000 AL 1 000 AX
0 001 CL 1 001 CX
0 010 DL 1 010 DX
0 011 BL 1 011 BX
0 100 AH 1 100 SP
0 101 CH 1 101 BP
0 110 DH 1 110 SI
0 111 BH 1 111 DI
Machine language Instruction Formats
In general, all the addressing modes have DS as Tab.1.8: Segment registers
the default segment register.
Reg code
(2-bit) Reg
Addressing modes using SP and BP use SS as the
default segment register. 00 ES
Register to Register: 2 bytes long 01 CS
First byte specifies the opcode and length of the 10 SS
operation is specified by W-bit.
11 DS
Second byte has the register operands and R/M
field.
R/M specifies the another operand as whether Destination Source
Opcode
register or memory. Operand Operand
Machine language Instruction Formats
D7 D1 D0 D7 D6 D3 D4 D5 D2 D1 D0
D7 D0 D7 D0
Register: Data is stored in a register. All the registers, except IP, may
be used in this mode .
MOV BX,AX
ADD AL,BL
Register Indirect: 16-bit offset address of data is specified in either
BX or SI or DI register.
MOV AX,[BX]
8086 Addressing Modes
Indexed: Offset of the operand stored in any of the indexed registers.
DS is the default segment register for SI and DI.
In string instructions DS and ES are default segment registers for SI and
DI, respectively.
MOV AX, [SI] (Moves 16-bit data from offset specified by SI)
MOV DL, [DI] (Moves 8-bit data from offset specified by DI)
PA = 10H * DS + SI or PA = 10H * ES + DI
Register Relative: effective address is formed by adding an 8-bit or 16-bit
displacement with the contents of any of the registers BX, BP, SI, and DI.
MOV AX, 50H[BX] (PA = 10H * DS + 50H + BX)
MOV 10H[SI], BL (PA = 10H * DS + 10H + SI)
8086 Addressing Modes
Based Indexed: Effective address is formed by adding content of a base
register (BX or BP) to the content of the index register (SI or DI).
DS is the default segment register for SI and DI.
DS and ES are default segment registers.
MOV AX, [BX] [SI] (PA = 10H * DS + SI + BX)
MOV [BP] [DI], DX (PA = 10H * DS + BP + SI)
Relative Based Indexed: Effective address is formed by adding an 8-bit or
16-bit displacement with the sum contents of any of the registers BX or
BP and any of the index registers SI or DI.
MOV AX, 50H [BX] [SI] (PA = 10H * DS + 50H + BX + SI)
ADD 50H [BX] [SI], BP (PA = 10H * DS + 50H + BX + SI)
8086 Addressing Modes
a) Data copy/transfer
b) Arithmetic and Logical
c) Conditional/unconditional Branch
d) Conditional/unconditional Loop
e) Machine control
f) Flag manipulation
g) Shift and rotate
h) String
8086 Instructions: Data copy/transfer
a) Data copy/transfer: Used for data transferring to/from Memory, I/O, or
Registers
MOV Move
Syntax: MOV destination operand, source operand
Both the operands must be of same size.
No flags get affected.
Destination operand Source operand
Valid Register Register
Valid Any register except segment registers Immediate data
Valid Register Offset (with/without displacement)
Valid Offset (with/without displacement) Register
8086 Instructions: Data copy/transfer
Mnemonic Addressing Mode
Valid MOV AX, 5000H Immediate
Valid MOV AX, BX Register
Valid MOV AX, [SI] Indirect
Valid MOV AX, [2000H] Direct
Valid MOV AX, 50H [BX] Relative Based Indexed
Valid MOV AX, [BX] Based Indexed
Invalid MOV DS, 5000H Immediate
PUSH AX
AH AL
7AH 29H
XLAT Translate
Syntax: XLAT
No flags get affected.
Finds the codes using look-up table in case of code conversion problems.
8086 Instructions: Data copy/transfer
Mnemonic Operation
MOV AX, SEG TABLE TABLE segment address to AX
MOV DS, AX Segment address from AX to DS
MOV AL, CODE Code of the pressed KEY
MOV BX, OFFSET TABLE Offset of the pressed KEY code in LUT
XLAT Finds the equivalent code and stores in AL
Mnemonic (AAD)
AAD
MOV AL, 07H
MOV BL, 06H
DIV BL
8086 Instructions: Arithmetic and Logical
AAS ASCII adjust after subtraction
Syntax: AAS
Executed after a subtraction(SUB) instruction.
Converts the resulting content of AL to unpacked decimal digits.
First clears AH.
If lower nibble of AL is between 0 – 9 and AF = 0, then AAA sets higher
nibble of AL to 0.
If lower nibble of AL is either between 0 – 9 and AF = 1 or is greater than
9, then AAA subtracts 6 from AL decrements AH by 1.
The AF & CF are set 1.
Remaining flags unaffected.
AAM ASCII adjust after Multiplication
Syntax: AAM
8086 Instructions: Arithmetic and Logical
Converts the product in AL into unpacked BCD format.
Higher nibble of multiplication operands must be zero.
First clears AH.
Replaces content of AH by tens of the decimal multiplication and AL by the
ones of multiplication.
Is lower
nibble of
YES
the ADD
result in AL
NO is < 9
Is AF = 1
YES NO
STOP
8086 Instructions: Arithmetic and Logical
DAS Decimal adjust after subtraction
Syntax: DAS
Executed after the subtraction (SUB) instruction.
Converts the result of the subtraction of two packed BCD numbers to a
valid BCD number.
Result will present only in AL.
NEG Negate
Syntax: NEG reg/memory
Forms the 2’s complement of the specified operand.
The operand may be a reg/ memory location whose offset specified in the
instruction.
If OF = 1, the operation couldn’t be completed successfully.
All conditional code flags get affected.
8086 Instructions: Arithmetic and Logical START
Is lower
nibble of
YES
the SUB
result in AL
NO is < 9
Is AF = 1
YES NO
STOP
8086 Instructions: Arithmetic and Logical
CBW Convert signed Byte to Word
Syntax: CBW
Converts a signed byte in AL register into a signed word.
Result stored in the AX register.
The upper byte is filled with the SIGN (MSB of the 8-bit number in AL) bit.
Doesn’t affect any flag.
OR Logical OR
Syntax: OR destination operand, source operand
Performs bit-by-bit logical OR operation.
Mnemonic Operation
MOV AX, SEG1
Source string segment address SEG1 loaded to DS via AX
MOV DS, AX
MOV AX, SEG2H Destination string segment address SEG2 loaded to ES via
MOV ES, AX AX
MOV SI, OFFSET1 Source index address
MOV DI, OFFSET2 Destination Index address
LODSB/LODSW Loads AL/AX with the strings from DS:SI location
STOSB/STOSW Stores the string in AL/AX to the location pointed by ES:DI
8086 Instructions: Control Transfer/Branch
d) Control Transfer/Branch Instructions: Transfers the flow of execution to a
new location specified in the instruction directly/indirectly.
CS and IP are loaded with new location values to which the control has to
be transferred.
CS may or may not have the same content depending upon the addressing
mode.
Control Transfer
Unconditional control transfer/branching: execution control will be
transferred to the specified location independent of any condition.
Conditional control transfer/branching: control transferred to the new
location depending upon the result of the previous instruction.
Otherwise, the execution continues normally.
8086 Instructions: Control Transfer/Branch
Unconditional Transfer/Branch Instructions
CALL Unconditional call
Syntax: CALL LABEL
Increment IP by 1 and
Increment IP by 1 and
store both IP & CS in the
store IP in the Stack
Stack
Sub Routine/
Procedure
8086 Instructions: Control Transfer/Branch
RET Return from the Procedure
Syntax: RET
Mnemonic Operation
JCXZ LABEL Transfers execution to LABEL, if CX = 0
Loop through a sequence of instructions from LABEL while
LOOPZ/LOOPE LABEL ZF = 1, and CX = 0
Loop through a sequence of instructions from LABEL while
LOOPNZ/LOOPNE LABEL ZF = 0, and CX = 0
8086 Instructions: Flag manipulation and Processor control
e) Flag manipulation and Processor control: Control the functioning of
available hardware inside the processor.
Remaining flags will be modifies using either POPF or SAHF instructions.
Except carry flag no other status flags can be changed directly using flag
manipulation instructions. Mnemonic Operation
CLC Clear carry flag
CMC Complement carry flag
STC Set carry flag
CLD Clear direction flag
STD Set direction flag
CLI Clear interrupt flag
STI Set interrupt flag
8086 Instructions: Flag manipulation and Processor control
Processor control/Machine control: Control the processor and its execution
directly.
Mnemonic Operation
WAIT Wait for test input pin goes LOW
HLT Halt the processor
NOP No operation
Escape to external device like
ESC numeric co-processor or peripheral
devices
LOCK Bus lock instruction prefix
8086/88 Assembler Directives and
Operators
8086 Assembler Directives & Operators
An assembler is a program used to convert an assembly language program
(ALP) into machine language.
These machine language codes further converted to executable codes.
Assembler can only find syntax errors but not the logical or other
programming errors.
Assembler directives provide the necessary guidance to the assembler to
all these tasks.
Help the assembler to correctly understand the instructions.
An operator assists the assembler to assign a constant with label, initialize
memory locations, or labels with constants.
Operators perform certain arithmetic and logical tasks whereas, the
directives helps in correct interpretation of program.
8086 Assembler Directives & Operators
DB: Define Byte: used to define a byte or array of bytes.
It directs the assembler to allocate specified number of bytes in the
memory to a constant, variable or a string.
Reserves memory bytes with ASCII codes of the string.
Example: OPR1 DB 54H
RANKS DB 01H, 02H, 03H, 04H, 05H
STRINGS DB ‘HELLO WORLD..!’
DW: Define Word: used to define a word or array of words.
Example: OPR1 DW 2354H
RANKS DW 0123H, 0234H, 0345H, 0456H
WDATA DW 7 DUP (5555H)
8086 Assembler Directives & Operators
DQ: Define Quadword: used to reserve 4 words (8 bytes) of memory.
DT: Define Ten Bytes: used to reserve 10 bytes to the specified variable.
ASSUME: Assume Logical Segment Name: informs assembler the names of
the logical segments to be assumed for different segments used in program.
Each segment is given a name.
Example: ASSUME CS: CODE, DS: DATA, ES:EXTRA, SS: STACK
The corresponding segment register loaded with the segment address.
Assembler assumes the segment DATA as default data segment and CODE
as default code segment.
ASSUME must be at the starting of each program. Without which
‘CODE/DATA EMITTED WITHOUT SEGMENT’ will be issued by the
assembler.
8086 Assembler Directives & Operators
END: End of Program: marks the end of an assembly language program.
Assembler usually ignores the source lines after END statement.
FAR PTR: a label with FAR PTR is not available in the same segment.
Example: JMP FAR PTR LABEL
CALL FAR PTR ROUTINE
NEAR PTR: a label with NEAR PTR is available in the same segment.
Example: JMP NEAR PTR LABEL
CALL NEAR PTR ROUTINE
The default pointer is NEAR PTR if none is specified with the label.
ALP for Sorting
•assume cs:code,ds:data • cmp al,[si+1]
• data segment • jc next (jnc next )
• ary db 33h,22h,44h,11h,55h • mov bl,[si+1]
• data ends • mov [si+1],al
• code segment • mov [si],bl
• start: • next:inc si
• mov ax,data • dec dl
• mov ds,ax • jnz up2
• xor ax,ax • dec cl
• mov cl,04h • jnz up1
• up1:lea si,ary • int 03h
• mov dl,cl • code ends
• up2:mov al,[si] • end start
References