Chapter 3 Programming With 8086 Microprocessor PDF
Chapter 3 Programming With 8086 Microprocessor PDF
Chapter-3
Programming with 8086 microprocessor
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 1
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
- It has data bus of width 16 bits and address bus of width 20 bits. So it always accesses a
16 bit word to or from memory.
- The 8086 microprocessor is divided internally into two separate units which are Bus
interface unit (BIU) and the execution unit (EU).
- The BIU fetches instructions, reads operands and write results.
- The EU executes instructions that have already been fetched by BIU so that instructions
fetch overlaps with execution.
- A 16 bit ALU in the EU maintains the MP status and control flags, manipulates general
register and instruction operands.
- Code segment register and instruction pointer (IP): The CS contains the base or start of
the current code segment. The IP contains the distance or offset from this address to
the next instruction byte to be fetched. Code segment address plus an offset value in
the IP indicates the address of an instruction to be fetched for execution.
- Data Segment
Data segment Contains the starting address of a program’s data segment. Instructions
use this address to locate data. This address plus an offset value in an instruction, causes
a reference to a specific byte location in the data segment.
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 2
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
- Extra Segment(ES)
It is used by some string (character data) to handle memory addressing. The string
instructions always use the ES and destination index (DI) to determine 20 bit physical
address.
- AX Register
AX register is called 16 bit accumulator and AL is called 8 bit accumulator. The I/O (IN or
OUT) instructions always use the AX or AL for inputting/Outputting 16 or 8 bit data from
or to I/O port.
- BX Register
BX is known as the base register since it is the only general purpose register that can be
used as an index to extend addressing. The BX register is similar to the 8085’s H, L
register. BX can also be combined with DI or SI as C base register for special addressing.
- CX register:
The CX register is known as the counter register because some instructions such as
SHIFT, ROTATE and LOOP use the contents of CX as a Counter.
- DX register:
The DX register is known as data register. Some I/O operations require its use and
multiply and divide operations that involve large values assume the use of DX and AX
together as a pair. DX comprises the rightmost 16 bits of the 32-bit EDX.
- Index register:
The two index registers SI (Source index) and DI (Destination Index) are used in indexed
addressing. The instructions that process data strings use the SI and DI index register
together with DS and ES respectively, in order to distinguish between the source and
destination address.
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 3
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
- Flag register:
The 8086 has nine 1 bit flags. Out of 9 six are status and three are control flags. The
control bits in the flag register can be set or reset by the programmer.
O O D I T S A P C
O D I T S Z A P C
D15 D0
- O- Overflow flag This flag is set if an arithmetic overflow occurs, i.e. if the result of a
signed operation is large enough to be accommodated in a destination register.
- D-Direction Flag This is used by string manipulation instructions. If this flag bit is ‘0’ , the
string is processed beginning from the lowest address to the higher address, i.e. auto
incrementing mode otherwise the string is processed from the highest address towards
the lowest address, i.e. autodecrementing mode.
- I-Interrupt flag If this flag is set the maskable interrupts are recognized by the CPU,
otherwise they are ignored.
- T- Trap flag If this flag is set the processor enters the single step execution mode. In
other words, a trap interrupt is generated after execution of each instruction. The
processor executes the current instruction and the control is transferred to the Trap
interrupt service routine.
- S - Sign flag: This flag is set when the result of any computation is negative. For signed
computations, the sign flag equals the MSB of the result.
- Z- Zero This flag is set when the result of the computation is or comparison performed
by the previous instruction is zero. 1 for zero result, 0 fir nonzero result
- A- Auxiliary Carry This is set if there is a carry from the lowest nibble, i.e. bit three
during the addition or borrow for the lowest nibble i.e. bit three, during subtraction.
- P- Parity flag This flag is set to 1 if the lower byte of the result contains even number of
1s otherwise reset.
- C-Carry flag This flag is set when there is a carry out of MSB in case of addition or a
borrow in case of subtraction.
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 4
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
Instructions in 8086
1) Arithmetic Instructions
f) DIV reg/mem
E.g. DIV R8 AX/R8 (Remainder AH) & (Q AL)
DIV R16 DX:AX/R16 (R DX) & (Q AX)
IDIV- Signed division
Same operation as DIV but takes sign into account.
g) INC/DEC (Increment/Decrement by 1)
INC/DEC reg./mem. (8 bit or 16bit)
E.g. INC AL DEC BX
INC NUM1
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 5
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
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
SHL -logical shift left
SHR - logical shift right
Shifts bit in true direction and fills zero in vacant place
E.g. SHL reg/mem, 1/CL
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
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 6
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
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
TEST: test bits (AND operation)
TEST reg/mem, reg/mem/immediate
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
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 7
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
5) STACK Operations
PUSH reg16
POP reg16
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
8) Type conversion
CBW: Convert byte to word
CWD: Convert word to double word
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 8
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
AX DX: AX
9) String instructions
a) MOVS/ MOVSB/MOVSW ; Move string
DS: SI source
DS: DI destination
CX: String length
b) CMPS/ CMPSB/CMPW ; Compare string
c) LODS /LODSB/LODW ; Load string
d) REP ; Repeat string
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.
E.g. NUM EQU 1374 H
MOV AL HIGH Num ; ( [AL] 13 )
7) OFFSET: returns offset address of a variable
8) SEG: returns segment address of a variable
9) PTR: used with type specifications
BYTE, WORD, RWORD, DWORD, QWORD
E.g. INC BYTE PTR [BX]
10) Segment override
MOV AH, ES: [BX]
11) LENGTH: returns the size of the referred variable
12) SIZE: returns length times type
E.g.: BYTE VAR DB?
WTABLE DW 10 DUP (?)
MOV AX, TYPE BYTEVAR ; AX = 0001H
MOV AX, TYPE WTABLE ; AX = 0002H
MOV CX, LENGTH WTABLE ; CX = 000AH
MOV CX, SIZE WTABLE ; CX = 0014H
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 9
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
Advantage of ALP
- They generate small and compact execution module.
- They have more control over hardware.
- They generate executable module and run faster.
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
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 10
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
Statements:
- ALP consists of a set of statements with two types
- Instructions, e. g. MOV, ADD
- Directives, e. g. define a data item
Directives:
The directives are the number of statements that enables us to control the way in which the
source program assembles and lists. These statements called directives act only during the
assembly of program and generate no machine-executable code. The different types of
directives are:
1) The page and title listing directives:
The page and title directives help to control the format of a listing of an assembled
program. This is their only purpose and they have no effect on subsequent execution of
the program.
The page directive defines the maximum number of lines to list as a page and the
maximum number of characters as a line.
PAGE [Length] [Width]
Default: Page [50][80]
TITLE gives title and place the title on second line of each page of the program.
TITLE text [comment]
2) SEGMENT directive
It gives the start of a segment for stack, data and code.
Seg-name Segment *align+*combine+*‘class’+
Seg-name ENDS
- Segment name must be present, must be unique and must follow assembly language
naming conventions.
- An ENDS statement indicates the end of the segment.
- Align option indicates the boundary on which the segment is to begin; PARA is used to
align the segment on paragraph boundary.
- Combine option indicates whether to combine the segment with other segments when
they are linked after assembly. STACK, COMMON, PUBLIC, etc are combine types.
- Class option is used to group related segments when linking. The class code for code
segment, stack for stack segment and data for data segment.
3) PROC Directives
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]
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 11
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
…………….
…………….
…………….
PROC - name ENDP
- FAR is used for the first executing procedure and rest procedures call will be NEAR.
- Procedure should be within segment.
4) END Directive
- An END directive ends the entire program and appears as the last statement.
- ENDS directive ends a segment and ENDP directive ends a procedure. END PROC-Name
5) ASSUME Directive
- An .EXE program uses the SS register to address the stack, DS to address the data
segment and CS to address the code segment.
- Used in conventional full segment directives only.
- Assume directive is used to tell the assembler the purpose of each segment in the
program.
- Assume SS: Stack name, DS: Data Segname CS: codesegname
6) Processor directive
- Most assemblers assume that the source program is to run on a basic 8086 level
computer.
- Processor directive is used to notify the assembler that the instructions or features
introduced by the other processors are used in the program.
E.g. .386 - program for 386 protected mode.
VAL1 DB 25
ARR DB 21, 23, 27, 53
MOV AL, ARR [2] or
MOV AL, ARR + 2 ; Moves 27 to AL register
9) DUP Directive
- It can be used to initialize several locations to zero.
e. g. SUM DW 4 DUP(0)
- Reserves four words starting at the offset sum in DS and initializes them to Zero.
- 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.
Page 60,132
TITLE SUM program to add two numbers
;-----------------------------------------------------
STACK SEGMENT PARA STACK ‘Stack’
DW 32 DUP(0)
STACK ENDS
;----------------------------------------------------
DATA SEG SEGMENT PARA ‘Data’
NUM1 DW 3291
NUM 2 DW 582
SUM DW?
DATA SEG ENDS
;------------------------------------------------------
CODE SEG SEGMENT PARA ‘Code’
MAIN PROC FAR
ASSUME SS: STACK, DS:DATASEG, CS:CODESEG
MOV AX, @DATA
MOV DS, AX
MOV AX, NUM1
ADD AX, NUM2
MOV AX, 4C00H
INT 21H
MAIN ENDP
CODESEG ENDS
END MAIN
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 13
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
- STACK contains one entry, DW (define word), that defines 32 words initialized to zero,
an adequate size for small programs.
- DATASEG defines 3 words NUM1, NUM2 initialized with 3291 and 582 and sum
uninitialized.
- CODESEG 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 DATASEG to DS register so that the processor uses the address in DS for
addressing DATASEG.
- Assign CODESEG to the CS register so that the processor uses the address in CS for
addressing CODESEG.
When the loading a program for disk into memory for execution, the program loader
sets the correct segment addresses in SS and CS.
SUM DW ?
.CODE
MAIN PROC FAR
MOV AX, @ DATA ; set address of data segment in DS
MOV DS, AX
MOV AX, NUM1
ADD AX, NUM2
MOV SUM, AX
MOV AX, 4C00H ; End processing
INT 21H
MAIN ENDP ; End of procedure
END MAIN ; End of program
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.
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 15
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
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.
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 16
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
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.
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 17
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
.MODEL SMALL
.STACK 64
.DATA
VAL1 DW 3241
VAL2 DW 571
SUM DW ?
.CODE
MAIN PROC FAR
MOV AX, @ DATA
MOV DS, AX
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 18
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
1) Register Addressing:
For this mode, a register may contain source operand, destination operand or both.
E.g. MOV AH, BL
MOV DX, CX
2) Immediate Addressing
In this type of addressing, immediate data is a part of instruction, and appears in the
form of successive byte or bytes. This mode contains a constant value or an expression.
E.g. MOV AH, 35H
MOV BX, 7A25H
3) Direct memory addressing:
In this type of addressing mode, a 16-bit memory address (offset) is directly specified in
the instruction as a part of it. One of the operand is the direct memory and other
operand is the register.
E.g. ADD AX, [5000H]
Note: Here data resides in a memory location in the data segment, whose effective
address may be computed using 5000H as the Offset address and content of DS as
segment address. The effective address, here, is 10H*DS + 5000H.
9) String addressing:
This mode uses index registers, where SI is used to point to the first byte or word of the
source string and DI is used to point to the first byte or word of the destination string,
when string instruction is executed. The SI or DI is automatically incremented or
decremented to point to the next byte or word depending on the direction flag (DF).
E.g. MOVS, MOVSB, MOVSW
Examples:
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 20
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
MOV CX, 10
MOV AX, 0
LEA BX, ARR
L2: ADD Al, [BX]
JNC L1
INC AH
L1: INC BX
LOOP L2
MOV SUM, AX
MOV AX, 4C00H
INT 21H
MAIN ENDP
END MAIN
The Intel CPU recognizes two types of interrupts namely hardware interrupt when a peripheral
devices needs attention from the CPU and software interrupt that is call to a subroutine located
in the operating system. The common software interrupts used here are INT 10H for video
services and INT 21H for DOS services.
INT 21H:
It is called the DOS function call for keyboard operations follow the function number. The
service functions are listed below:
# 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
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 23
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
END MAIN
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.
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 28
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
<Video –modes>
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 29
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
Attribute
Background Foreground
Attribute: BL R G B I RG B
Bit number: 7 6 54 32 1 0
I – Intensity, BL - Blink
Color Hex
Value
Black 0
Blue 1
Green 2
Cyan 3
Red 4
Magnet 5
Brown 6
White 7
Gray 8
Light Blue 9
Light Green A
Light cyan B
Light red C
Light magenta D
Yellow E
Bright white F
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 30
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 31
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
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
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 32
Microprocessors Chapter 3 : Programming with 8086 Microprocessor
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
INT 21H
MAIN ENDP
END MAIN
Compiled by: Er. Hari Aryal Email: haryal4@gmail.com Reference: Peter Abel | 34