8086 Microprocessor
8086 Microprocessor
8086 Microprocessor
1) CS Register
CS holds the base (Segment) address for the Code Segment.
All programs are stored in the Code Segment.
It is multiplied by 10H (16d), to give the 20-bit physical address of the Code Segment.
Eg: If CS = 4321H then CS × 10H = 43210H Starting address of Code Segment.
CS register cannot be modified by executing any instruction except branch instructions
2) DS Register
DS holds the base (Segment) address for the Data Segment.
It is multiplied by 10H (16d), to give the 20-bit physical address of the Data Segment.
Eg: If DS = 4321H then DS × 10H = 43210H Starting address of Data Segment.
3) SS Register
SS holds the base (Segment) address for the Stack Segment.
It is multiplied by 10H (16d), to give the 20-bit physical address of the Stack Segment.
Eg: If SS = 4321H then SS × 10H = 43210H Starting address of Stack Segment.
4) ES Register
ES holds the base (Segment) address for the Extra Segment.
It is multiplied by 10H (16d), to give the 20-bit physical address of the Extra Segment. Eg: If
ES = 4321H then ES × 10H = 43210H Starting address of Extra Segment.
Viva Question: Explain the real procedure to obtain the Physical Address?
The Segment address is left shifted by 4 positions, this multiplies the number by 16 (i.e. 10h) and then the
offset address Is added.
Eg: If Segment address is 1234h and 0ffset address is 0005h, then the physical address (12345h) is
calculated as follows:
1234h = (0001 0010 0011 0100) binary
Left shift by four positions and we get (0001 0010 0011 0100 0000) binary i.e. 12340h
Now add (0000 0000 0000 0101) binary i.e. 0005h and we get (0001 0010 0011 0100 0101) binary i.e.
12345h.
F1 E1 E2 E3 E4 E5 Time
F2 F3 F4 F5
Overlapping fetching
and execution
AX Register (16-Bits)
It holds operands and results during multiplication and division operations.
All IO data transfers using IN and OUT instructions use A reg (AL/AH or AX). It functions as
accumulator during string operations.
BX Register (16-Bits)
Holds the memory address (offset address), in Indirect Addressing modes.
CX Register (16-Bits)
Holds count for instructions like: Loop, Rotate, Shift and String Operations.
DX Register (16-Bits)
It is used with AX to hold 32 bit values during Multiplication and Division.
It is used to hold the address of the IO Port in indirect IO addressing mode.
c) ALU (16-Bits)
It has a 16-bit ALU. It performs 8 and 16-bit arithmetic and logic operations.
d) Operand Register
It is a 16-bit register used by the control register to hold the operands temporarily. It is not
available to the Programmer.
e) Instruction Register and Instruction Decoder (Present inside the Control Unit)
The EU fetches an opcode from the queue into the Instruction Register. The Instruction
Decoder decodes it and sends the information to the control circuit for execution.
STATUS FLAGS
1) Carry flag (CY)
It is set whenever there is a carry {or borrow} out of the MSB of a the result (D7 bit for an 8-bit
operation D15 bit for a 16-bit operation) .
2) Parity Flag (PF)
It is set if the result has even parity.
3) Auxiliary Carry Flag (AC)
It is set if a carry is generated out of the Lower Nibble. It is used only in 8-bit operations like DAA and
DAS.
4) Zero Flag (ZF)
It is set if the result is zero.
CONTROL FLAGS
Code Segment
This segment is used to hold the program to be executed. Instruction are fetched from the Code
Segment.
CS register holds the 16-bit base address for this segment.
IP register (Instruction Pointer) holds the 16-bit offset address.
Data Segment
This segment is used to hold general data.
This segment also holds the source operands during string operations.
DS register holds the 16-bit base address for this segment.
BX register is used to hold the 16-bit offset for this segment.
SI register (Source Index) holds the 16-bit offset address during String Operations.
Stack Segment
This segment holds the Stack memory, which operates in LIFO manner.
SS holds its Base address.
SP (Stack Pointer) holds the 16-bit offset address of the Top of the Stack.
BP (Base Pointer) holds the 16-bit offset address during Random Access.
Extra Segment
This segment is used to hold general data
Additionally, this segment is used as the destination during String Operations.
ES holds the Base Address.
DI holds the offset address during string operations.
Advantages of Segmentation:
It permits the programmer to access 1MB using only 16-bit address.
Its divides the memory logically to store Instructions, Data and Stack separately.
Disadvantage of Segmentation:
Although the total memory is 16*64 KB, at a time only 4*64 KB memory can be accessed.
MEMORY BANKING IN 8086
As 8086 has a 16-bit data bus, it should be able to access 16-bit data in one cycle.
To do so it needs to read from 2 memory locations, as one memory location carries only one byte. 16-
bit data is stored in two consecutive memory locations.
However, if both these memory locations are in the same memory chip then they cannot be accessed at
the same time, as the address bus of the chip cannot contain two address simultaneously.
Hence, the memory of 8086 is divided into two banks each bank provides 8-bits.
The division is done in such a manner that any two consecutive locations lie in two different chips.
Hence each chip contains alternate locations.
∴ One bank contains all even addresses called the “Even bank”, while the other is called “Odd
bank” containing all odd addresses.
Generally, for any 16-bit operation, the Even bank provides the lower byte and the ODD bank
provides the higher byte. Hence the Even bank is also called the Lower bank and the Odd bank is
also called the Higher bank.
Eg: MOV CX, [4320H] ; Moves data from location 4320H and 4321H
; in the data segment into CL and CH resp.
Eg: MOV CL, [BX+4] ; Moves a byte from the address pointed by BX+4 in
; Data Seg to CL.
; Physical Address: DS * 10H + BX + 4H
Eg: MOV 12H [BP], CL ; Moves a byte from CL to location pointed by BP+12H in
; the Stack Seg.
; Physical Address: SS * 10H + BP + 12H
BASE INDEXED ADDRESSING MODE
Here, operand address is calculated as Base register plus an Index register.
Eg: MOV CL, [BX+SI] ; Moves a byte from the address pointed by BX+SI
; in Data Segment to CL.
; Physical Address: DS * 10H + BX + SI
Eg: MOV [BP+DI], CL ; Moves a byte from CL into the address pointed by
; BP+DI in Stack Segment.
; Physical Address: SS * 10H + BP + DI
Eg: MOV CL, [BX+DI+20] ; Moves a byte from the address pointed by
; BX+SI+20H in Data Segment to CL.
; Physical Address: DS * 10H + BX + SI+ 20H
Eg: MOV [BP+SI+2000], CL ; Moves a byte from CL into the location pointed by
; BP+SI+2000H in Stack Segment.
; Physical Address: SS * 10H + BP+SI+2000H
Instructions in 8086
1) Arithmetic Instructions
a) ADD reg8 /mem8 , reg8/mem8/ Immediate8
ADD reg16/mem16 , reg16/ mem16/ Immediate16
E.g. ADD AH, 15 ; It adds binary number
ADD AH, NUM1
ADD Al, [BX]
ADD [BX], CX
ADD AX, [BX]
b) ADC: Addition with Carry
ADC reg/ mem, reg/mem/Immediate data
2) Logical/shifting/comparison instructions
a) Logical
AND/OR/XOR reg/mem, reg/mem/immediate
NOT reg/mem
E. g. AND AL, AH
XOR [BX], CL
b) Rotation
ROL- rotate left, ROR-rotate right
E.g. ROL AX, 1 ; rotated by 1
ROL AX, CL ; if we need to rotate more than one bit
RCL-rotate left through carry
RCR-rotate right through carry
E.g. RCL AX, 1
RCL AX, CL ; Only CL can be used
c) Shifting Shifts bit in true direction and fills zero in vacant place
SHL -logical shift left
SHR - logical shift right
E.g. SHL reg/mem,
SHL CL
SAL -arithmetic shift left
SAR - arithmetic shift right
Shifts bit/word in true direction, in former case place zero in vacant place and in later
case place previous sign in vacant place.
E.g. 1 011010 1 11011010
d) Comparison: CMP –--->compare
CMP reg/mem, reg/mem/immediate
E.g. CMP BH, AL
Operand1 Operand 2 CF SF ZF
0 0 0
0 0 1
1 1 0
A: AL/AX
MOV AL, BL
Thus to transfer an immediate data into the segment register, the convert procedure is
given below:
Note: Both the source and destination operands cannot be memory locations (Except for
string instructions)
IN AL, 0300H
IN AX; This instruction reads data from a 16-bit port whose address is in DX (implicit) and
stores it in AX.
OUT: Output to the Port:
This instruction is used for writing to an output port.
The address of the output port may be specified in the instruction directly or
implicitly in DX.
Contents of AX or AL are transferred to a directly or indirectly addressed port after
execution of this instruction. The data to an odd addressed port is transferred on D8
–D15 while that to an even addressed port is transferred on D0-D7.
The registers AL and AX are the allowed source operands for 8- bit and 16-bit
operations respectively.
OUT AX; This sends data available in AX to a port whose address is specified implicitly in
DX.
4) Flag Operation
CLC: Clear carry flag
CLD: Clear direction flag
CLI: Clear interrupt flag
STC: Set Carry flag
STD: Set direction flag
STI: Set Interrupt flag
CMC: Complement Carry flag
LAHF: Load AH from flags (lower byte)
SAHF: Store AH to flags
PUSHF: Push flags into stack
POPF: Pop flags off stack
5). STACK Operations
PUSH reg16
POP reg16
OR BX, AX
AND DX, AX
LOOP Label
The execution proceeds in sequence, after the loop is executed, CX number of times. IF CX is already
00H, the execution continues sequentially. No flags are affected by this instruction.
LOOPE: Loop while equal
LOOPZ: loop while zero
LOOPNE: loop while not equal
LOOPNZ: loop while not zero
7. Branching instruction
a) Conditional
JA: Jump if Above
JAE: Jump if above/equal
JB: Jump if below
JBE: Jump if below/equal
JC: Jump if carry
JNC: Jump if no carry
JE: Jump if equal
JNE: Jump if no equal
JZ: Jump if zero
JNZ: Jump if no zero
JG: Jump if greater
JNG: Jump if no greater
JL: Jump if less
JNL: Jump if no less
JO: jump if overflow
JS: Jump if sign
JNS: Jump if no sign
JP: jump if plus
JPE: Jump if parity even
JNP: Jump if no parity
JPO: Jump if parity odd
b) Unconditional
CALL: call a procedure RET: Return
INT: Interrupt IRET: interrupt return
JMP: Unconditional Jump RETN/RETF: Return near/Far
Ex: MOV AX, SEG1 ; Segment address of String1, i.e. SEG1 is moved to AX.
Operators in 8086
- Operator can be applied in the operand which uses the immediate data/address.
- Being active during assembling and no machine language code is generated.
- Different types of operators are:
1) Arithmetic: +, -, * , /
2) Logical: AND, OR, XOR, NOT
3) SHL and SHR: Shift during assembly
4) [ ]: index
5) HIGH: returns higher byte of an expression
6) LOW: returns lower byte of an expression.
MOV AL HIGH Num ; ( [AL] 13 )
Advantage of ALP
Disadvantages of ALP:
- Machine dependent.
- Lengthy code
- Error prone (likely to generate errors).
Program comments:
- The use of comments throughout a program can improve its clarity.
- It starts with semicolon (;) and terminates with a new line.
- E.g. ADD AX, BX ; Adds AX & BX
Reserved words:
- Certain names in assembly language are reserved for their own purpose to be used only under special
conditions and includes
- Instructions: Such as MOV and ADD (operations to execute)
- Directives: Such as END, SEGMENT (information to assembler)
- Operators: Such as FAR, SIZE
- Predefined symbols: such as @DATA, @ MODEL
Identifiers:
- An identifier (or symbol) is a name that applies to an item in the program that expects to reference.
- Two types of identifiers are Name and Label.
- Name refers to the address of a data item such as NUM1 DB 5, COUNT DB 0 - Label refers to the
address of an instruction.
- E. g: MAIN PROC FAR
- L1: ADD BL, 73
Statements:
ALP consists of a set of statements with two types:
▪
Instructions, e. g. MOV, ADD
▪
Directives, e. g. define a data item
Identifiers operation operand comment Directive: COUNT DB 1; initialize count
3) PROC Directives
The PROC directive marks the start of a named procedure in the statement.
The code segment contains the executable code for a program, which consists of one or more
procedures, defined initially with the PROC directives and ended with the ENDP directive.
PROC-NAME PROC [FAR/NEAR]
…………….
…………….
…………….
PROC-NAME ENDP
Ex: RESULT PROC NEAR
…….
RESULT ENDP
- FAR is used for the first executing procedure and rest procedures call will be NEAR.
- Procedure should be within segment.
- Also used to reserve several locations that need not be initialized. In this case (?) is used with DUP
directives.
E. g. PRICE DB 100 DUP(?)
- Reserves 100 bytes of uninitialized data space to an offset PRICE.
Program written in Conventional full segment directive
Page 60,132
TITLE SUM program to add two numbers
;-----------------------------------------------------
STACK SEGMENT
DW 32 DUP (0)
STACK ENDS
;----------------------------------------------------
DATA SEGMENT
NUM1 DW 3291H
NUM 2 DW 582H
SUM DW ?
DATA ENDS
;------------------------------------------------------
CODE SEGMENT
MAIN PROC FAR
ASSUME SS: STACK-SEG, DS: DATA-SEG, CS: CODE-SEG
MOV AX, @DATA
MOV DS, AX
MOV AX, NUM1
ADD AX, NUM2
MOV AX, 4C00H
INT 21H
MAIN ENDP
CODESEG ENDS
END MAIN
Description for conventional program:
- STACK contains one entry, DW (define word), that defines 32 words initialized to zero, an adequate
size for small programs.
- DATA-SEG defines 3 words NUM1, NUM2 initialized with 3291H and 582H and sum uninitialized.
- CODE-SEG contains the executable instructions for the program, PROC and ASSUME generate no
executable code.
- The ASSUME directive tells the assembler to perform these tasks.
- Assign STACK to SS register so that the processor uses the address in SS for addressing STACK.
- Assign DATA-SEG to DS register so that the processor uses the address in DS for addressing DATA-
SEG.
- Assign CODE-SEG to the CS register so that the processor uses the address in CS for addressing
CODE-SEG. When the loading a program for disk into memory for execution, the program loader sets
the correct segment addresses in SS and CS.
DOS Debug(TASM)
1) Save the code text in .ASM format and save it to the same folder where masm and link files
are stored.
2) Open dos mode and reach within that folder.
3) \>tasm filename.asm makes.obj
4) \>tlink filename makes .exe
5) \>filename.exe run the code
6) \>td filename.exe debug the code [use F7 and F8]
- Assembling converts source program into object program if syntactically correct and generates an
intermediate .obj file or module.
- It calculates the offset address for every data item in data segment and every instruction in code
segment.
- A header is created which contains the incomplete address in front of the generated obj module during
the assembling.
- Assembler complains about the syntax error if any and does not generate the object module.
- Assembler creates .obj .lst and .crf files and last two are optional files that can be created at run time.
- For short programs, assembling can be done manually where the programmer translates each mnemonic
into the machine language using lookup table.
- Assembler reads each assembly instruction of a program as ASCII character and translates them into
respective machine code.
Assembler Types:
There are two types of assemblers:
a) One pass assembler:
- This assembler scans the assembly language program once and converts to object code at the same
time.
This assembler has the program of defining forward references only.
- The jump instruction uses an address that appears later in the program during scan, for that case the
programmer defines such addresses after the program is assembled.
2) Linking:
- This involves the converting of .OBJ module into .EXE(executable) module i.e. executable
machine code.
- It completes the address left by the assembler.
It combines separately assembled object files.
- Linking creates .EXE, .LIB,. MAP files among which last two are optional files.
3) Loading and Executing:
Macro Assembler:
- A macro is an instruction sequence that appears repeatedly in a program assigned with a specific
name.
- The macro assembler replaces a macro name with the appropriate instruction sequence each time
it encounters a macro name.
When same instruction sequence is to be executed repeatedly, macro assemblers allow the macro
name to be typed instead of all instructions provided the macro is defined.
- Macro are useful for the following purposes:
o To simplify and reduce the amount of repetitive coding.
o To reduce errors caused by repetitive coding. o To make an assembly language program
more readable. o Macro executes faster because there is no need to call and return.
o Basic format of macro definition:
Macro name MACRO [Parameter list] ; Define macro
……………………….
……………………….
[Instructions] ; Macro body
……………………….
……………………….
ENDM ; End of macro
# 01H
MOV, AH 01H; request keyboard input INT 21H
- Returns character in AL. IF AL= nonzero value, operation echoes on the screen. If Al= zero means
that user has pressed an extended function key such as F1 OR home.
# 02H
MOV AH, 02H; request display character
MOV DL, CHAR; character to display
INT 21H
- Display character in D2 at current cursor position. The tab, carriage return and line feed characters
act normally and the operation automatically advances the cursor.
# 09H
MOV Ah, 09H; request display
LEA DX, CUST_MSG; local address of prompt
INNT 21H
CUST_MSG DB “Hello world”, ‘$’
- Displays string in the data area, immediately followed by a dollar sign ($ or 24H), which uses to
end the display.
# OAH
MOV AH, 0AH ; request keyboard input
LEA DX, PARA_ LIST ; load address of parameter list
INT 21H
- LABEL directive tells the assembler to align on a byte boundary and gives location the name PARA
_LIST.
- PARA_LIST & MAX_LEN refer same memory location, MAX_LEN defines the maximum no of
defined characters.
- ACT_LEN provides a space for the operation to insert the actual no of characters entered.
- KB_DATA reserves spaces (here 20) for the characters.
Example:
TITLE to display a string
.MODEL SMALL
.STACK 64
.DATA
STR DB ‘programming is fun’, ‘$’
.CODE
MAIN PROC FAR
MOV AX, @DATA
MOV DS, AX
MOV AH, 09H ;display string
LEA DX, STR
INT 21H
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
INT 10H
It is called video display control. It controls the screen format, color, text style, making windows,
scrolling etc. The control functions are:
# 00H – set video mode
MOV AH, 00H ; set mode
MOV AL, 03H ; standard color text
INT 10H ; call interrupt service
Calling procedure/subroutine
Here the code segment consists only one procedure. The FAR operand in this case
informs the assembler and linker that the defined procedure name is the entry point for
program execution, whereas the ENDP directive defines the end of the procedure. A code
segment however, may contain any number of procedures, each distinguished by its own
PROC and ENDP directives.
A called procedure is a section of code that performs a clearly defined task known as
subroutine which provides following benefits.
• Reduces the amount of code because a common procedure can be called from any
number of places in the code segment.
• Encourage better program organization.
• Facilitates debugging of a program because defects can be more clearly isolated.
• Helps in the ongoing maintenance of programs because procedures are readily
identified for modification.
A CALL to a procedure within the same code segment is NEAR CALL<. A FAR CALL calls a
procedure labeled FAR, possibly in another code segment.
<Video –modes>
07 Mono-hrome 0 16 colors
25 80 720 400
Graphic mode Color Pages Resolution No of colors
04 Color 8 4
320 200
05 Color 8 4
320 200
06 Color 8 2
640 200
0D Color 8 16
320 200
0E Color 4 16
640 200
0F Mono chrome 2 1
640 350
10 Color 2 16
640 350
11 Color 1 2
640 480
12 Color 1 16
640 480
13 Color 1 256
320 200
Attribute
Background Foreground
Attribute: BL R G B I RG B
Bit number: 7 6 54 32 1 0
I – Intensity, BL - Blink
TITLE to display string at (10,40) with green background and red foreground dosseg .Model small
.Stack 100H
.Code
MAIN PROC FAR
MOV AX, @ DATA
MOV DX, AX
MOV SI, OFFSET VAR1
L2: MOV AH, 02H ; Set cursor position
MOV DH, ROW
MOV DL, COL
INT 10H
MOV AL, [SI]
CMP AL, ‘$’
JE L1
MOV AH, 09H
MOV DH, ROW
MOV DL, COL
MOV BL, 24H ;background & foreground
MOV BH, 00h ; page
MOV CX, 01H ; no. of repeated characters
INT 10H
INC SI
INC COL
JMP L2
L1: MOV AX, 4C00H
INT 21H
MAIN ENDP
.DATA
ROW DB 10
COL DB 40
VAR1 DB “video model”, ‘$’
END MAIN
TITLE TO GENERATE MULTIPLICATION TABLE
.MODEL SMALL
.STACK 32
.DATA
NUM1 DB 5
NUM2 DB 1
TAB DB 10 DUP (?)
.CODE
MAIN PROC FAR
MOV AX, @ DATA
MOV DS, AX
MOV BX, 0
MOV CX, 10
L1: MOV AL, NUM1
MUL NUM2
MOV TAB [BX], AL
INC BX
INC NUM2
LOOP L1
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
TITLE to add 10 sixteen bit Numbers in memory table
.MODEL SMALL
.STACK 32
.DATA
NUM DW DUP (2)
NUM DW DUP (3)
SUMH DW 0
SUML DW 0
Note: To access the data of the
.CODE memory i.e. table.
We use e.g. NUM[BX]
MAIN PROC FAR Increasing the BX register by 2
MOV AX, @ DATA
MOV DS, AX
MOV CX, 10
MOV AX, 0
MOV BX, 0
L1: ADD AX, NUM
[BX]
MOV SUML, AX
JNC L2
INC SUMH
L2: ADD BX, 2
LOOP L1
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
SUBROUTINE TO CLEAR THE SCREEN
SCR_CLEAR PROC NEAR
MOV AX, 0600H ; Request scroll
MOV BH, 61H ; blue on brown for attribute on pixel(generally (07H) white on black
MOV CX, 0000 ; Full screen
MOV DX, 184FH
INT 10H
RET
SCR_CLEAR ENDP
AH-06h: Scroll upward of lines in a specified area of the screen.
AL- 00H caused entire screen to scroll up, effectively clearing it. Setting a nonzero value in AL
causes the number of lines to scroll up.
Differentiate between
MIN MODE MAX MODE
It is a multiprocessor mode. Along
It is a uniprocessor mode. 8086 is
1 with 8086, there can be other processors
the only processor in the circuit.
like 8087 and 8089 in the circuit.
2
Here MN/ MX is connected to Vcc. Here MN/ MX is connected to Ground.
ALE for the latch is given by 8086 As there are multiple processors, ALE for the
3
itself. latch is given by 8288 bus controller.
As there are multiple processors, DEN
DEN and DT/ R for the
4
transreceivers are given by 8086 and DT/ R for the transreceivers is given by
itself. 8288 bus controller.
Since 74138 does not independently Since 8288 independently generates control
9 generate any signals, it does not need a signals, it needs a CLK from 8284 clock
CLK. generator.
The circuit is simpler but does not The circuit is more complex but supports
10
support multiprocessing. multiprocessing.
Differentiate between
8085 8086
ALU cannot perform powerful arithmetic ALU can perform powerful arithmetic like
10
like MUL and DIV. MUL and DIV.
Differentiate between
8088 8086
Memory banking not needed. Hence Memory is divided into two banks. Hence
2
circuit is simpler. circuit is more complex.
Since data bus is 8-bits, it can transfer 1 Since data bus is 16-bits, it can transfer 2
3 byte in 1 cycle. bytes in 1 cycle.
Hence is slower. Hence is faster.