8086 Programming
8086 Programming
Addressing Mode
Program
A set of instructions written to solvea
problem.
Instruction
Directions which a microprocessor follows
to execute a task or part of a task.
Computer language
Mode
➢ Program is a set of instructions written to solve a problem. Instructions are the
directions which a microprocessor follows to execute a task or part of a task.
Broadly, computer language can be divided into two parts as high-level language
and low level language. Low level language are machine specific. Low level
language can be further divided into machine language and assembly language.
_
Microprocessor and Microcontroller
1. Register Addressing
Group I : Addressing modes for register
2. Immediate Addressing and immediate data
3. Direct Addressing
5. Based Addressing
Group II : Addressing modes for memory
6. Indexed Addressing
data
7. Based Index Addressing
8. String Addressing
Addressing Mode
1. Register Addressing The instruction will specify the name of the register which
holds the data to be operated by the instruction.
2. Immediate Addressing
Example:
3. Direct Addressing
MOV CL, DH
4. Register Indirect Addressing
8. String Addressing
_
Microprocessor and Microcontroller
7. Based Index Addressing The 16-bit data (0A9FH) given in the instruction is moved to
AX register
8. String Addressing
(AX) 0A9FH
9. Direct I/O port Addressing
_
Microprocessor and Microcontroller
Adder
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
[SI]
[BX + SI] [BX + SI + d8]
[DI]
[BX + DI] [BX + DI + d8]
d16 (variable offset only)
[BP + SI] [BP + SI + d8]
[BX]
[BP + DI] [BP + DI + d8]
BX SI
+ disp
BP DI
_
Microprocessor and Microcontroller
Addressing Mode Here, the effective address of the memory location at which
the data operand is stored is given in the instruction.
1. Register Addressing The effective address is just a 16-bit number written directly
in the instruction.
2. Immediate Addressing
Example:
3. Direct Addressing
MOV BX, [1354H]
4. Register Indirect Addressing
MOV BL, [0400H]
5. Based Addressing
The square brackets around the 1354 H denotes the contents
6. Indexed Addressing of the memory location. When executed, this instruction will
copy the contents of the memory location into BX register.
7. Based Index Addressing
This addressing mode is called direct because the
8. String Addressing displacement of the operand from the segment base is
specified directly in the instruction.
9. Direct I/O port Addressing
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
Addressing Mode
In Based Addressing, BX or BP is used to hold the base value
for effective address and a signed 8-bit or unsigned 16-bit
1. Register Addressing displacement will be specified in the instruction.
Addressing Mode
SI or DI register is used to hold an index value for memory
1. Register Addressing data and a signed 8-bit or unsigned 16-bit displacement will
be specified in the instruction.
2. Immediate Addressing
Displacement is added to the index value in SI or DI register to
3. Direct Addressing obtain the EA.
6. Indexed Addressing
Example:
7. Based Index Addressing
MOV CX, [SI + 0A2H]
8. String Addressing
Operations:
9. Direct I/O port Addressing
FFA2 H A2 H (Sign extended)
10. Indirect I/O port Addressing
EA = (SI) + FFA2H BA = (DS) x 1610
11. Relative Addressing MA = BA + EA
(CL) (MA)
(CH) (MA + 1)
_
Microprocessor and Microcontroller
Addressing Mode
In Based Index Addressing, the effective address is computed
1. Register Addressing from the sum of a base register (BX or BP), an index register
(SI or DI) and a displacement.
2. Immediate Addressing
Example:
3. Direct Addressing
MOV DX, [BX + SI + 0AH]
4. Register Indirect Addressing
5. Based Addressing
Operations:
_
Microprocessor and Microcontroller
Addressing Mode
Employed in string operations to operate on string data.
1. Register Addressing
The effective address (EA) of source data is stored in SI
2. Immediate Addressing register and the EA of destination is stored in DI register.
5. Based Addressing
Example: MOVS BYTE
6. Indexed Addressing
Operations:
7. Based Index Addressing
Calculation of source memory location:
8. String Addressing EA = (SI) BA = (DS) x 1610 MA = BA + EA
_
Microprocessor and Microcontroller
Addressing Mode
These addressing modes are used to access data from
1. Register Addressing standard I/O mapped devices or ports.
_
Microprocessor and Microcontroller
Addressing Mode
1. Register Addressing In this addressing mode, the effective address of a program
instruction is specified relative to Instruction Pointer (IP) by
2. Immediate Addressing an 8-bit signed displacement.
_
Microprocessor and Microcontroller
Addressing Mode
1. Register Addressing
2. Immediate Addressing
3. Direct Addressing
Instructions using this mode have no operands. The
4. Register Indirect Addressing instruction itself will specify the data to be operated by the
instruction.
5. Based Addressing
Example: CLC
6. Indexed Addressing
This clears the carry flag to zero.
8. String Addressing
_
Microprocessor and Microcontroller
Instruction Set
2. Arithmetic Instructions
3. Logical Instructions
_
Microprocessor and Microcontroller
➢ A 8-bit data can only be moved to 8-bit register/ memory and a 16-
bit data can be moved to 16-bit register/ memory.
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
ADD A, data
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
ADDC A, data
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
SUB A, data
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
SBB A, data
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
_
Microprocessor and Microcontroller
Mnemonics: ADD, ADC, SUB, SBB, INC, DEC, MUL, DIV, CMP…
CMP A, data
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
❑ 8086 instruction set includes instruction for string movement, comparison, scan, load and store.
❑ Offset or effective address of the source operand is stored in SI register and that of the
destination operand is stored in DI register.
_
Microprocessor and Microcontroller
REP
REPNZ/ REPNE
While CX 0 and ZF = 0, repeat execution of stringinstruction
(Repeat CMPS or SCAS until ZF = 1) and
(CX) (CX) - 1
_
Microprocessor and Microcontroller
MOVS
(MAE) (MA)
_
Microprocessor and Microcontroller
CMPS
_
Microprocessor and Microcontroller
LODS
_
Microprocessor and Microcontroller
STOS
_
Microprocessor and Microcontroller
CLC Clear CF 0
NOP No operation
ESC opcode mem/ reg Used to pass instruction to a coprocessor which shares the
address and data bus with the 8086
_
Microprocessor and Microcontroller
Mnemonics Explanation
CALL reg/ mem/ disp16 Call subroutine
➢ Checks flags
_
Microprocessor and Microcontroller
_
Microprocessor and Microcontroller
Mnemonics Explanation
JC disp8 Jump if CF = 1
JP disp8 Jump if PF = 1
JO disp8 Jump if OF = 1
JS disp8 Jump if SF = 1
_
Assembly Language
Programming
_
Machine/Assembly Language
Machine Language:
Assembly Language:
Assembler:
_
Machine Language vs Assembly Language
Programming
_
Why Assembly Language Programming
_
Programmer’s Model
_
Organization of 8086 Processor
_
CPU Registers
_
CPU Registers
_
Fetch-Execute Process
➢ Fetch-Execute process
✓ Repeat process
_
Fetch-Execute Process
Main Memory
0
1
PC
65535
IR
0010101100111111
_
Assembly Language Syntax
➢ Statement syntax
➢ Name field
✓ Names are 1-31 characters including letters, numbers and special characters ? . @ _ $ %
✓ Case insensitive
_
Assembly Language Syntax
_
Assembly Language Syntax
➢ Operation field
✓ instruction
✓ Describes operation’s function; e.g. MOV, ADD, SUB, INC. • Assembler directive
➢ Operand field
INC AX
ADD AX, 2
_
Assembly Language Syntax
➢ Comment field
✓ A semicolon marks the beginning of a comment
✓ A semicolon in beginning of a line makes it all a comment line
✓ Good programming practice dictates comment on every line
➢ Examples
✓ MOV CX, 0 ; move 0 to CX
✓ Do not say something obvious
✓ MOV CX, 0 ; CX counts terms, initially 0
✓ Put instruction in context of program
✓ ; initialize registers
_
Data Representation
➢ Numbers
✓ 1011 decimal
✓ 11011B binary
✓ 64223 decimal
✓ -21843D decimal
✓ FFFFH illegal hex numbe, does not begin with with digit
_
Data Representation
➢ Characters
✓ must be enclosed in single or double quotes
_
Data Representation
_
Variable Declaration
➢ Data-defining pseudo-ops
✓ DB define byte
✓ DW define word
➢ Each pseudo-op can be used to define one or more data items of given
type.
_
Byte Variables
➢ K DB 5, 3, -1 allocates 3 bytes
_
Word Variables
✓ I DW 4
✓ J DW -2
✓ K DW 1ABCH
✓ L DW “01”
_
Double Word Variables
✓ I DD 1FE2AB20H
✓ J DD -4
_
Named Constants
➢ LF EQU 0AH
✓ MOV DL, LF
✓ MDG DB PROMPT
_
DUP Operator
✓ Line DB 5, 4, 2, 0, 0, 0, 1, 2, 0, 0, 0, 1, 2, 0, 0, 0, 1
_
General Rules
✓ MOV i, j illegal
✓ ADD 2, AX illegal
_
Memory Segmentation
_
Memory Segmentation
➢ Data Segment
✓ contains variable definitions
✓ declared by .DATA
➢ Stack segment
✓ used to store the stack
➢ Code segment
✓ contains program’s instructions
✓ declared by .CODE
_
Memory Models
➢ SMALL
✓ code in one segment & data in one segment
➢ MEDIUM
✓ code in more than one segment & data in one segment
➢ COMPACT
✓ code in one segment & data in more than one segment
➢ LARGE
✓ code in more than one segment & data in more than one segment & no array larger than
64K bytes
➢ HUGE
✓ code in more than one segment & data in more than one segment & arrays may be larger
than 64K bytes
_
Program Structure: An Example
TITLE PRGM1
.MODEL SMALL
.STACK 100H
.DATA
A DW 2
B DW 5
SUM DW ?
.CODE
MAIN PROC
; initialize DS
MOV AX, @DATA
MOV DS, AX
_
Program Structure: An Example
;add the numbers
MOV AX, A
ADD AX, B
MOV SUM, AX
; exit to DOS
INT 21H
MAIN ENDP
END MAIN
_
Assembly Language Programming: Addition
MOV CX, 0000H Initialize counter CX
_
Assembly Language Programming: Multiplication
MOV AX,[1200] Get the first data
_
Assembly Language Programming: COPYING A
STRING
MOV SI,1200H Initialize destination address
REP MOVSB Copy the contents of source into destination until count
reaches zero
HLT Stop
_
Assembly Language Programming: Ascending Order
MOV SI,1200H Initialize memory location for array size
MOV CL,[SI] Number of comparisons in CL
L4 : MOV SI,1200H Initialize memory location for array size
MOV DL,[SI] Get the count in DL
INC SI Go to next memory location
MOV AL,[SI] Get the first data in AL
L3 : INC SI Go to next memory location
MOV BL,[SI] Get the second data in BL
CMP AL,BL Compare two data’s
JNB L1 If AL < BL go to L1
DEC SI Else, Decrement the memory location
MOV [SI],AL Store the smallest data
MOV AL,BL Get the next data AL
JMP L2 Jump to L2
L1 : DEC SI Decrement the memory location
MOV [SI],BL Store the greatest data in memory location
L2 : INC SI Go to next memory location
DEC DL Decrement the count
JNZ L3 Jump to L3, if the count is not reached zero
MOV [SI],AL Store data in memory location
DEC CL Decrement the count
JNZ L4 Jump to L4, if the count is not reached zero
HLT Stop
_
8086 Assembly Language Program LOGICAL OR
CODE SEGMENT
ASSUME CS: CODE
START: MOV AL, 85H
MOV BL, 99H
OR AL, BL
INT 3H
CODE ENDS
END START
_
8086 Assembly Language program
Adding two multi byte numbers and store the result as the third number
_
8086 Assembly Language program : Moving a Block using strings
DATA SEGMENT
SRC DB ‘MICROPROCESSOR’
DB 10 DUP (?)
DST DB 20 DUP (0)
DATA ENDS
CODE SEGMENT
ASSUME CS: CODE, DS: DATA, ES: DATA
START: MOV AX, DATA
MOV DS, AX
MOV ES, AX
LEA SI, SRC
LEA DI, DST
MOV CX, 20
CLD
REP MOVSB
INT 3H
CODE ENDS
END START
_