Professional Documents
Culture Documents
MICROPROCESSOR
AND
MICROCONTROLLER
S
(Regulation 2013)
OBJECTIVES
8/14/15
SYLLABUS
8/14/15
SYLLABUS
TEXT BOOKS
[1] Yu-Cheng Liu, Glenn A.Gibson, Microcomputer Systems:
The 8086 / 8088 Family - Architecture, Programming and
Design, Second Edition, Prentice Hall of India, 2007.
[2] Mohamed Ali Mazidi, Janice Gillispie Mazidi,
Rolin
McKinlay, The 8051 Microcontroller and Embedded
Systems: Using Assembly and C, Second Edition, Pearson
Education, 2011.
[3] Doughlas V.Hall, Microprocessors and Interfacing,
Programming and Hardware, TMH,2012
8/14/15
8/14/15
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
INTRODUCTION TO 8086
Overview of Microcomputer Systems
Two principal components
1. Hardware
CPU,
Timing circuits
Memory Units
Input / Output Subsystems
Bus control Logic
System Bus
2. Software
System Software
User Software
8/14/15
8/14/15
MICROPROCESSORS BY
OTHER COMPANIES
8/14/15
10
ARCHITECTURE OF A TYPICAL
MICROCOMPUTER SYSTEM
8/14/15
11
SOFTWARE HIERARCHY
8/14/15
12
2. Assembly Language
More readable form of machine language
Uses mnemonic codes
Assembler needed to translate to machine code
8/14/15
13
ASSEMBLER
8/14/15
14
DATA REPRESENTATION
Number Formats
Binary (or Integer)
[+ve, -ve Integers; Base 2, 8, 10, 16]
Alphanumeric Codes
EBCDIC-Extended Binary Coded Decimal
Interchange Code
ASCII American Standard Code for Information
Interchange
Introduction to 8086 (continued.....)
8/14/15
15
GENERAL OPERATION OF A
COMPUTER
8/14/15
16
GENERAL OPERATION OF A
COMPUTER
8/14/15
17
OPERATION OF P
1.
Fetch
2.
Decode
3. Execute
8/14/15
18
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
19
8086 ARCHITECTURE
8/14/15
20
8086 CHIPSET
8086 Architecture
8/14/15
21
8086 ARCHITECTURE
Features
8/14/15
22
8086 FEATURES
Has
40
Supply
10MHz
8/14/15
23
CPU-MEMORY INTERFACE
Addess Bus
CPU
8086
20
Data Bus
Memory
16
Control Bus
8086 Architecture (continued)
8/14/15
24
8086 ARCHITECTURE
8/14/15
25
8086 ARCHITECTURE
8/14/15
26
8086 ARCHITECTURE
The 8086 has two parts:
the Bus Interface Unit (BIU)
Fetches instructions,
Reads and Writes data, and
computes the 20-bit address for memory operands
Transfers instruction bytes into the 6 byte FIFO queue
the Execution Unit (EU)
Decodes and
Executes the instructions using the 16-bit ALU.
Both units operate asynchronously to give the 8086 an
overlapping instruction fetch and execution mechanism
which is called as Pipelining.
8086 Architecture (continued)
8/14/15
27
BIU
Registers
EU
Registers
Code Segment
Data Segment
Extra
Segment
Stack
Segment
Instruction
Pointer
AX
AH
AL
BX
BH
BL
CX
CH
CL
DX
DH
DL
SP
BP
SI
DI
Flags
Accumulat
or
Base
Register
Count
Register
Data
Register
Stack
Pointer
Base
Pointer
Source Index
Register
Destination Index
Register
Flag Register
8/14/15
28
8/14/15
29
8/14/15
30
8/14/15
31
AX
Accumulator Register
Used in Arithmetic, Logic and Data Transfer instructions
Used in Multiplication and Division operations
Used in I/O operations
BX
Base Register
Also serves as an address register
Used in array operations
Used in Table Lookup operations (XLAT)
CX
Count register
Used as a Loop Counter
Used in shift and rotate operations
DX
Data register
Used in Multiplication and Division
Also used in I/O operations
8086 Architecture (continued)
8/14/15
32
8/14/15
33
8/14/15
34
8/14/15
35
8/14/15
36
8/14/15
37
00000
20000
0FFF
F
1000
0
2FFF
F
Seg-15
EFFF
F
Seg-16
FFFF
F
1FFF
F
8086 Architecture (continued)
8/14/15
38
1 MB Memory
Space
5000:FFF
F
5000:025
0
5000:000
0
SegAddr:Offse
t
8/14/15
39
0 0 0 0
Adder
8/14/15
40
A9822
8/14/15
41
Overlapping segments
8/14/15
42
2.
3.
4.
8/14/15
43
8/14/15
44
Flags
Carry flag
Overflow flag
Direction flag
Parity flag
Auxiliary flag
Interrupt enable
Trap flag Zero flag
6 - status flags
Sign flag
3 - control flags
8/14/15
45
Flags
Control flags:
Used to control some operations of the MPU.
These flags are to be set by the user, in order to
achieve some specific purposes.
8086 Architecture (continued)
8/14/15
46
8/14/15
47
Control flags:
8/14/15
48
Example
ZF= 0
AF= 0
PF= 0
CF= 0
OF= 0
SF= 1
ZF= 0
AF= 1
PF= 1
CF= 0
OF= 0
8/14/15
49
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
50
ADDRESSING MODES
8/14/15
51
ADDRESSING MODES
Operand
Operand
Addressing Modes
8/14/15
52
Solution:
Example:
If CS=24F6h & IP=634Ah, show the;
1- The logical address
2- The offset address
3- The physical address
4- The lower range of the segment
5- The upper range of the segment
8/14/15
53
8/14/15
54
8/14/15
55
AAA
AAS
AAM
AAD
DAA
DAS
XLAT
8/14/15
56
Source
16 Bit
Data
8 Bit
Data
8/14/15
57
No PhyAddr,
8 BIT Operand Registers - AL, AH, BL, BH, CL, CH, DL, DH
8/14/15
58
Default Segment - DS or ES
Offset BX or SI or DI
PhyAddr = 10H *
DS
ES
BX
SI
DI
If DS=5000H; BX=10FF;
Then EffectiveAddr = 10FF
and PhyAddr = 10H*5000H + 10FFH = 510FFH
Addressing Modes (continued...)
8/14/15
59
6. Indexed Addressing
Data is pointed by the offset in the index register
specified in the instruction
DS is the default segment register for SI and DI
SI
DI
8/14/15
60
DS
ES
BX
BP
SI
DI
8/14/15
61
BX
BP
PhyAddr = 10H *
DS
ES
SI
DI
BX
BP
SI
DI
8/14/15
62
PhyAddr = 10H *
DS
ES
8 bit
16 bit
BX
BP
BX
BP
SI
DI
SI
DI
8/14/15
63
8/14/15
64
Short Jump or
Near Jump
The jump destination is
in the same code
segment
To execute the jump,
only the contents of
Instruction Pointer (IP)
register needs to be
changed
CS: 3000H
IP: 0000H
CS: 3000H
IP: 0010H
Code Segment
JMP NEXT
CS: 3000H
NEXT: INC BX
IP: 0020H
8/14/15
65
EffectiveAddr = [BX]
PhyAddr = 10H*[CS]+[BX]
8/14/15
66
675A
B
6100
0
CS = 6000H
MOV AX, BX
JMP [BX]
BX = 75ABH
6000
0
8/14/15
67
8/14/15
68
Code Segment A
CS: 3000H
IP: 0000H
CS: 3000H
IP: 0010H
JMP NEXT
Code Segment B
CS: 6000H
IP: 0000H
CS: 6000H
NEXT: INC BX
IP: 0050H
8/14/15
69
8/14/15
70
7FFF
F
7446
6
6200
6200
3
6200
2
6200
1
0
70H
00H
44H
66H
6010
0
6000
0
JMP
[2000H]
MSB
LSB
IP
44
66
CS
70
00
7000
0
8/14/15
71
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
72
8/14/15
73
8/14/15
74
Source: Seg.Register /
Gen.Register /Mem.location
Destination: Seg.Register /
Gen.Register /Mem.location
Note: Both Source and
Destination cannot be
Mem.Locn
8/14/15
75
PUSH
POP
XCHG
XLAT
OUT
8/14/15
76
LDS
LES
LAHF
SAHF
PUSHF
POPF
8/14/15
77
Instruction
MOV DS, 5000H
Comments
Not Valid; immediate addressing with
segment registers not permitted
Immediate addressing
Register addressing
Direct addressing
Indirect addressing
Register relative
/ Indexed addr
PUSH AX
PUSH DS
PUSH [5000H]
8/14/15
78
Example 1 :
*Knowing that,
DS=2042H.
SI=500H,
AX=214E,
MOV [SI], AX
AH
21
AL
4E
21
20921H
4E
20920H
SI=500
DS=2042H
Instruction Set (continued...)
DS=20420H
8/14/15
79
Example 2 :
PUSH AX
Physical Address
Stack Segment
20000
AH AL
88
44
SS = 2000
SP =
44
2FFFB
FFFB
88
2FFFC
FFFC
XX
2FFFD
FFFD
XX
2FFFE
FFFE
XX
2FFFF
FFFF
8/14/15
80
Instruction
Comments
XCHG [5000H], AX
XCHG BX, AX
1. IN AL, 03H
2. IN AX, DX
3. MOV DX, 0800H
IN AX, DX
8/14/15
81
Example 3 :
POP AX
Physical Address
Stack Segment
20000
AH AL
xx
88
xx
44
SS = 2000
SP =
44
2FFFB
FFFB
88
2FFFC
FFFC
XX
2FFFD
FFFD
2FFFE
FFFE
2FFFF
FFFF
8/14/15
82
Instruction
Comments
8/14/15
83
Example 4 :
Mem[AL+BX]
dddd+mmmm
data
AL=dddd
BX=mmmm
AL=data
for the example slides do not click the mouse to see the full animation
8/14/15
84
8/14/15
85
7-seg
Display
8255
KBD
interface
LUT- Mem.Seg
Example 5 :
Program using XLAT
83
8086 CPU
4 AL
7
BX
User
KB(0-F)
16-Keys
LUT
Comments
1. Address of seg containing
LUT is stored in DS
2. Code of key pressed
transferred to AL
3. Offset of code LUT into BX
used as Base Addr by XLAT
EffectAddr =10*SEG + BX+AL
4. AL [10*SEG + BX+AL]
8/14/15
86
Instruction
Comments
1. BX Offset (ADR)
3. BX(LB)
BX(HB)
DS/ES(LB)
DS/ES(HB)
[5000H]
[5001H]
[5002H]
[5003H]
8/14/15
87
Instruction
Comments
1. LAHF
1. AH [Flag (LB)]
2. SAHF
3. PUSHF
4. POPF
8/14/15
88
8/14/15
89
2. ARITHMETIC INSTRUCTIONS
Performs
Addition,
Subtraction,
Multiplication,
Division,
Increment,
Decrement
along with ASCII and Decimal adjustment
8/14/15
90
ADD
Addition instructions:
Add specified byte to byte or specified word to
word
ADC
INC
AAA
DAA
MUL
Multiplication instructions
Multiply unsigned byte by byte or unsigned word
by word
8/14/15
91
SUB
Subtraction Instructions:
Subtract byte from byte or word for word
flag from word.
SBB
DEC
NEG
CMP
AAS
DAS
8/14/15
92
DIV
Division Instructions:
Divide unsigned word by byte or unsigned DW by
word
IDIV
AAD
8/14/15
93
ADD
Add
1. Immediate addressing
2. ADD AX, BX
2. Register addressing
4. Direct addressing
5. Immediate addressing
6. ADD 0100H
6. Implicit addressing
(destination AX) &
Source : Regr / Mem.Locn / immediate
immediate addressing
Destination : Regr / Mem.Locn
8086 Instruction Set (continued...)
8/14/15
94
ADDC
1. ADC 0100H
1. Immediate addressing
2. ADC AX, BX
2. Register addressing
4. Direct addressing
5. Immediate addressing
6. ADD 0100H
6. Implicit addressing
(destination AX) &
Source : Regr / Mem.Locn / immediate
immediate addressing
Destination : Regr / Mem.Locn
8086 Instruction Set (continued...)
8/14/15
95
INC
1. INC AX
Increment
1. Register addressing
2. INC [BX]
3. INC [5000H]
3. Direct addressing
DEC
1. DEC AX
Decrement
1. Register addressing
2. DEC [BX]
3. DEC [5000H]
3. Direct addressing
8086 Instruction Set (continued...)
8/14/15
96
SUB
1. SUB AX, 0100H
1. Immediate addressing
2. SUB AX, BX
2. Register addressing
4. Direct addressing
5. Immediate addressing
Subtract
8/14/15
97
SBB
1. SBB AX, 0100H
1. Immediate addressing
2. SBB AX, BX
2. Register addressing
4. Direct addressing
5. Immediate addressing
8/14/15
98
CMP
1. CMP AX, 0100H
1. Immediate addressing
2. Immediate addressing
4. CMP BX, CX
4. Register addressing
5. Direct/Immediate addressing
Compare
8/14/15
99
AAA
AH (8 bits)
AL (8 bits)
ALHIGH
ALLOW
8/14/15
100
DAA
AH (8 bits)
AL (8 bits)
8/14/15
101
8/14/15
102
3. LOGICAL INSTRUCTIONS
Logical Instructions:
NOT
AND
OR
XOR
8/14/15
103
3. LOGICAL INSTRUCTIONS
Logical Instructions:
NOT
NOT AX;
NOT [5000H];
AND
OR
OR AX, 0008H;
OR AX, BX;
OR DX, [5000H];
XOR
8/14/15
104
8/14/15
105
SHR
Shift Right:
Shift bits of byte or word right, put zero(s) in MSB(s).
SAR
8/14/15
106
SHL/ SAL Shift bits of byte or word left, put zero(s) in LSB(s)
1. All flags are affected
2. Shift is through Carry Flag
3. Operand may reside in Register or Mem. Location but
not immediate
0
x Carry Flag
SHL / SAL
1st execution
MSB
LSB
10 1 01 10 01 0 10 01
1 Carry Flag
SHL / SAL
2nd execution
MSB
LSB
1 0 1 0 0 1 0 0
8/14/15
107
SHR
LSB
0 1 1 0 1 0 0 1
0 0 1 1 0 1 0 0
0 0 0 1 1 0 1 0
8/14/15
108
SAR
LSB
1 0 1 0 1 0 0 1
1 1 0 1 0 1 0 0
1 1 1 0 1 0 1 0
8/14/15
109
ROR
RCL
RCR
8/14/15
110
0
1
x Carry Flag
MSB
ROL
st
execution
21nd
execution
LSB
10 01 10 01 01 10 01 10
1
0
x
RCL
321 execution
execution
rd
nd
st
MSB
Carry Flag
LSB
8/14/15
111
8/14/15
112
REPE/REPZ
REPNE/REPNZ
MOVX/MOVSB/
MOVSW
COMPS/COMPSB/
COMPSW
SCAS/SCASB/
SCASW
LODS/LODSB/
LODSW
STOS/STOSB/STOSW
8/14/15
113
8/14/15
114
8/14/15
115
JB/JNAE
JC
JBE/JNA
JE/JZ
Jump if carry =1
JG/JNLE
JGE/JNL
JL/JNGE
JLE/JNG
JNC
JNE/JNZ
8/14/15
116
Jump if overflow
Jump if parity/ jump if parity even
JS
Jump if sign
Interrupt Instructions:
INT
INTO
IRET
8/14/15
117
Loop Instructions
LOOP
LOOPE/LOOPZ
8/14/15
118
8/14/15
119
STI
CLI
8/14/15
120
8/14/15
121
WAIT
8/14/15
122
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
123
ASSEMBLER DIRECTIVES
8/14/15
124
8/14/15
125
DIRECTIVES
Define Byte
Define Word
Define Quadword
4. DT
5. END
6. ENDP
End of Procedure
7. ENDS
End of Segment
8. ASSUME
9. SEGMENT
Logical segment
Assembler Directives (continued...)
8/14/15
126
DIRECTIVES (continued.)
10. EQU
Equate
11. EVEN
Align on even memory address
12. EXTRN and External and Public
PUBLIC
13. LOCAL
Local
14. GLOBAL
Global
15. GROUP
16. LABEL
17. LENGTH
18. NAME
19. ORG
Origin
20. PROC
Procedure
Assembler Directives (continued...)
8/14/15
127
OPERATORS USED IN
CROSOFT MACRO ASSEMBLER OR TURBO ASSEMBL
DIRECTIVES (continued.)
21. FAR PTR
Near pointer
22. NEAR PTR
Far pointer
1.OFFSET
2. PTR
3. SEG
OPERATORS
Offset of a label
Pointer
Segment of a label
4. SHORT
5. + and -
6. TYPE
Short
Addition and subtraction operators
Type
Assembler Directives (continued...)
8/14/15
128
1.DB
DIRECTIVES
DB directive is used to reserve bytes or bytes
of memory location , with data type that
may be a
-Constant
-Variable
-String etc
Example - 1
PRICE DB 49h, 98h, 29h ; Directs assembler to reserve an array of 3 bytes named as PRICE and initialize.
8/14/15
129
Example - 2
MESSAGE DB GOOD MORNING;
Reserve an array of bytes equal to the number of
characters in the string named as MESSAGE and
initialize by the ASCII equivalent of these characters
Example 3
TEMP DB 100 DUP(?) ;
Set 100 bytes of storage in memory and give it the name
as TEMP, but leave the 100 bytes uninitialized.
Program instructions will load values into
these locations.
DUP - Duplicates
8/14/15
130
2. DW
DIRECTIVES
DW directive is used to reserve words (16
bits) or words of memory location , with
data type that may be a
-Constant
-Variable
-String etc
Example - 1
WORDS DW 4987h, 1F98h, AB29h ;
Directs assembler to reserve an array of 3 Words named as
WORDS and initialize.
Lower bytes stored in lower and upper bytes stored in higher
addresses
Assembler Directives (continued...)
8/14/15
131
Example - 2
STAR DW 5 DUP (1234H);
Directs assembler to reserve an array of 5 Words
named as STAR and initializes all the locations with
Lower bytes stored in lower and upper bytes stored in
higher addresses
8/14/15
132
3. DQ
DIRECTIVES
DW directive is used to reserve 4 word bytes
(i.e 4 x 8 bits) of memory location , with data
type that may be a
-Constant
-Variable
-String etc; and may be initialized
DIRECTIVES
4. DT
8/14/15
133
DIRECTIVES
5. END
6. ENDP
8/14/15
134
7. ENDS
DIRECTIVES
END of Segment directive - marks the end
of logical segment.
-Logical segments are assigned with names
using ASSUME directive
-Must be used the last statement in the
segment along with segment name as prefix
8. ASSUME
9. SEGMENT
8/14/15
135
EXAMPLE
ASSUME CS : CODE, DS : DATA
DATA SEGMENT
STAR DB 20 DUP (?)
:
:
DATA ENDS
CODE SEGMENT
:
:
:
CODE ENDS
END
Assembler Directives (continued...)
8/14/15
136
DIRECTIVES
10. EQU
EXAMPLE
CONST1 EQU 0510H
8/14/15
137
DIRECTIVES
11. EVEN Align on even memory address directive updates
the location counter to next even address.
- While starting the assembling process, the
assembler initializes a location counter and
updates it sequentially to the program
variables; constants ; modules etc. as required.
EXAMPLE
EVEN
PROCEDURE
:
:
ROOT
ROOT
ENDP
8/14/15
138
DIRECTIVES
12. EXTERN EXTERN directive informs the assembler
and
that the names / procedures / labels declared
PUBLIC after this directive have already been defined
in some other ALP module
In the module where the names,
procedures, labels actually appear must be
declared PUBLIC
EXAMPLE
MODULE1 SEGMENT
PUBLICFACTORIAL FAR
MODULE1 ENDS
MODULE2 SEGMENT
EXTRN FACTORIAL FAR
MODULE1 ENDS
Assembler Directives (continued...)
8/14/15
139
DIRECTIVES
13. LOCAL
EXAMPLE
8/14/15
140
DIRECTIVES
14. GLOBAL LOCAL directive informs the assembler
that the labels / variables /constants /
procedures declared after this directive can
be used by OTHER modules in the program
EXAMPLE
8/14/15
141
DIRECTIVES
15. GROUP
EXAMPLE
8/14/15
142
DIRECTIVES
16. LABEL
EXAMPLE
DATA SEGMENT
DATAS DB 50H DUP (?)
DATA-LAST LABEL BYTE FAR
DATA ENDS
After reserving 50
locations for DATAS,
the next location will
be assigned a label
DATA-LAST and its
type will be Byte and
Far
8/14/15
143
DIRECTIVES
17. LENGTH Used to refer to the length of a data array or
string
EXAMPLE
EXAMPLE
NAME
8/14/15
144
DIRECTIVES
19. ORG
EXAMPLE
8/14/15
145
DIRECTIVES
20. PROC Marks the start of a named procedure
NEAR / FAR specify the type of the procedure
NEAR- called by the main program located
within 64K of physical memory
FAR Called by the main program located at a
more than 64K of physical memory
EXAMPLE
8/14/15
146
DIRECTIVES
21. FAR PTR
EXAMPLE
8/14/15
147
DIRECTIVES
22. NEAR PTR Indicates the assembler that the label
following FAR PTR is in the same
segment
Address of label is of 16 bits (2 bytes)
[2 bytes offset only]
EXAMPLE
8/14/15
148
OPERATORS
1. OFFSET
CODE SEGMENT
MOV SI, OFFSET LIST
CODE ENDS
DATA SEGMENT
LIST DB 10H
DATA ENDS
Assembler Directives (continued...)
8/14/15
149
OPERATORS
2. PTR - PTR operator is used to declare the type of a
label, variable or memory operand.
- PTR operator is prefixed by BYTE or WORD
EXAMPLE
MOV AL, BYTE PTR [SI] - Moves content of memory
location addressed by SI to AL
INC BYTE PTR [BX] - Increments byte content of
memory location addressed by BX
MOV BX, WORD PTR [2000H] - Moves 16 bit content of
memory location 2000h to BX (i.e) [2000h] to BL, [2001h] to
BH
INC WORD PTR [2000H] - Increments word content of
memory location 3000h and 3001h as a 16 bit number
Assembler Directives (continued...)
8/14/15
150
OPERATORS
3. SEG SEG operator is used to decide the segment
address of the label, variable or procedure and
substitutes the segment base address in place of
SEG label
EXAMPLE
MOV AX, SEG ARRAY
MOV DS, AX
Moves the segment address of ARRAY in which it is
appearing into the register AX and then into DS
8/14/15
151
OPERATORS
4. SHORT SHORT operator indicates to the assembler
that only one byte is required to code the
displacement for a jump
By specifying this way the assembler saves
memory.
OPERATORS
Represents arithmetic addition and subtration
8/14/15
152
OPERATORS
6. TYPE
8/14/15
153
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
154
ASSEMBLY LANGUAGE
PROGRAMMING
8/14/15
155
PROGRAM SEGMENT
STRUCTURE
Data Segments
Storage for variables
Variable addresses are
computed as offsets
from start of this
segment
Stack Segment
used to set aside storage
for the stack
Stack addresses are
computed as offsets into
this segment
Code Segment
contains executable
instructions
Segment directives
.data
.code
.stack size
8/14/15
156
MEMORY MODELS
Model memory_model
tiny
small
medium
compact
large
huge
flat
8/14/15
157
PROGRAM SKELETON
.model small
.stack 100H
.data
;declarations
.code
main proc
;code
main endp
;other procs
end main
Declare variables
Write code
organize into procedures
Mark the end of the source
file
optionally, define the
entry point
8/14/15
158
hellocod
START:
hellocod
8/14/15
159
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
Use 'assume' directive to define segment types
;
;
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
hellostk SEGMENT
;Define a segment
DB 100h DUP(?)
hellostk ENDS
hellodat
dos_print
strng
hellodat
SEGMENT
;define a segment
EQU 9
;define a constant
DB 'Hello World',13,10,'$' ;Define the character string
ENDS
hellocod
SEGMENT
;define a segment
assume cs:hellocod, ds:hellodat, ss: hellostk
mov ax, hellodat
;ax <-- data segment start address
mov ds, ax
;ds <-- initialize data segment register
mov ah, dos_print
;ah <-- 9 DOS 21h string function
mov dx,OFFSET strng
;dx <-- beginning of string
int 21h
;DOS service interrupt
mov ax, 4c00h
;ax <-- 4c DOS 21h program halt function
int 21h
;DOS service interrupt
ENDS
START
START:
hellocod
END
8/14/15
160
;define a constant
;Define the character string
.code
START:
mov
mov
int
mov
register
ah, dos_print
dx,OFFSET strng
21h
ax, 4c00h
function
int 21h
END
START
Assembly Language Programming (continued...)
8/14/15
161
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
162
MODULAR PROGRAMMING
8/14/15
163
MODULAR PROGRAMMING
Reasons for breaking a program into small parts:
1.
2.
3.
4.
5.
Modifications localized
6.
8/14/15
164
ASSEMBLER ;
LINKER AND RELOCATOR
8/14/15
165
ASSEMBLER
A Program that converts assembly language program into
equivalent machine codes, which may further be converted to
executable codes
Assembler does the following in steps:
Decides the address of each label
Substitutes the values for each of the constants and variables
Forms the machine code for mnemonics and data in the
assembly language program
During the above process, assembler finds out syntax errors, but
logical errors are not found
For completing these tasks assembler needs hints from the
programmer ; like
8/14/15
166
ASSEMBLER
8/14/15
167
ASSEMBLER
Assembler works in passes:
1. In the first pass, it determines the displacement of
the named data items, the offset of labels etc. and
puts this information to in a symbol table.
2.
3.
8/14/15
168
LINKER
Linker is a program used to join several object files into one
large object file.
1.
2.
8/14/15
169
LOCATORS
A locator is the program used to assign the specific
addresses of where the segments of object code are
to be loaded in to main memory.
1.
2.
8/14/15
170
DEBUGGER
A debugger is the program which allows to load the object
code program in to system memory.
1.
2.
3.
4.
8/14/15
171
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
172
STACKS
8/14/15
173
STACK
A STACK is a Last-Input-First-Output (LIFO)
read/write memory (i.e) data segment
It is a top-down data structure, whose elements
are accessed using SS and SP registers
Stack pointer is decremented by 2 while pushing
into the stack
Stack pointer is incremented by 2 while poping
off the stack
Stacks (continued . . .)
8/14/15
174
STACK
Stack is used to:
8/14/15
175
CODE
END
SEGMENT
MOV AX, STACK
MOV SS, AX
;initialize stack Seg. Regr.
LEA SP, STACKTOP ;initialize stack pointer
- - - - - - - - - - ;program instructions
ENDS
Stacks (continued . . .)
8/14/15
176
STACK INSTRUCTIONS
PUSH AND POP
PUSH always transfers 2 bytes of data to the stack.
Syntax : PUSH <src>;
<src> could be any 2 byte Register or Memory location
or Immediate value
When PUSH is executed, the most significant byte is
stored in stack segment pointed to by SP-1, and the
least significant byte to location pointed by SP-2.
Stacks (continued . . .)
8/14/15
177
Example -1 :
Let
SS=2000H;
SP=100H,
AX=31F2H,
PUSH AX
AX
31
F2
F2
200FD
31
200FE
20100
SP=00FD
SP=00FE
SP=0100
SS=2000H
Stacks (continued . . .)
8/14/15
178
Example - 2
Assume
SS=2000H,
SP=100H,
AX=31F2H,
BX=413AH,
CX=FFFFH.
Stacks (continued . . .)
8/14/15
179
SS=2000H,
SP=100H,
AX=31F2H,
BX=413AH,
CX=FFFFH.
PUSH
PUSH
POP
POP
AX
BX
CX
AX
CH
CL
41
FF
3A
FF
B
H
BL
41
3A
AH
AL
31
F2
SP=0100h
SP=00FEh
SP=00FCh
3A
41
200F
C
200F
F2
D
200FE
31
200FF
XX
2010
0
SS=2000H
Stacks (continued . . .)
8/14/15
180
Example 11:
Write an ALP to reverse string using stack operations.
STR DB HELLO, 00H ;
REVSTR DB 256 DUP(?)
MOV CX, 0000H
;count number of bytes
LEA SI, STR;
LEA DI, REVSTR
NEXT-CHAR: MOV AL, [SI]
CMP AL, 00
JE NEXT
PUSH AX ; only care about AL
INC CX;
JMP NEXT-CHAR
NEXT: CMP CX, 0000H; check if cx =0 or the string is null
JE DONE-REV
Stacks (continued . . .)
8/14/15
181
POP AX
MOV [DI],AL
INC DI
DEC CX;
JMP NEXT
DONE-REV: MOV [DI],00; to terminate with null
Stacks (continued . . .)
8/14/15
182
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
183
PROCEDURES
8/14/15
184
PROCEDURES
A procedure is a sequence of instructions written to
perform a particular task
Replacing a set of frequently used instructions by a
procedure saves program memory space
A CALL instruction in the main program causes 8086
to the execute the set of instructions contained in a
procedure
A RET instruction at the end of procedure returns
execution to the next instruction in the main program
Procedures (continued . . .)
8/14/15
185
MAINLINE
OR
CALLING PROGRAM
PROCEDURE
INSTRUCTIONS
CALL
NEXT MAINLINE
INSTRUCTIONS
Procedures (continued . . .)
RET
8/14/15
186
Procedures (continued . . .)
8/14/15
187
ADVANTAGES OF USING
PROCEDURES
Saves program memory space
Problem can be divided into small modules
Allows reusability of code
Disadvantage
Takes more time to execute
Procedures (continued . . .)
8/14/15
188
Procedures (continued . . .)
8/14/15
189
Procedures (continued . . .)
8/14/15
190
Procedures (continued . . .)
8/14/15
191
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
192
MACROS
8/14/15
193
MACROS
Defining a Macro:
name MACRO [optional arguments]
statements..
statements..
ENDM
Macros (continued . . .)
8/14/15
194
Macros (continued . . .)
8/14/15
195
MACROS
Advantages
1. Repeated small group of instructions replaced by one
macro
2. Errors in macros are fixed only once, in the definition
3. Duplication of effort is reduced
4. Programming is made easier and less error prone
5. Generally quicker in execution than subroutines
Disadvantages
In large programs, macros produce larger code size
than procedures
Macros (continued . . .)
8/14/15
196
2.
Advantage of using
procedures is that the
machine codes for the
group of instruction
in the procedures
needs to be loaded
into main memory
only once.
Disadvantage using
the procedures is the
need for the stack.
1. Advantage of using
macro is that it avoids
the overhead time
involved in calling and
returning from a
procedures.
2. Disadvantage is that
macros make the
program occupy more
memory than using a
procedure.
Macros (continued . . .)
8/14/15
197
8/14/15
198
:
:
DISPLAY MSG1
:
:
DISPLAY MSG2
:
:
8/14/15
199
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
200
INTERRUPTS AND
INTERRUPT SERVICE ROUTINES
8/14/15
201
WHAT IS AN INTERRUPT?
8/14/15
202
WHY INTERRUPT
CAPABILITY IS NECESSARY?
8/14/15
203
MEMORY
I/O
DEVICES
8/14/15
204
FIRST TYPE OF
COMMUNICATION
- DEDICATED I/O
BUS
I/O
DEVICE 1
8/14/15
205
SECOND TYPE OF
COMMUNICATION
- POLLED I/O OR PROGRAMMED I/O
BUS
I/O
I/O
.....
Device-n
Device-1
.
You?
You?
You?
Disadvantage:
Not fast enough
Wastes too much microprocessor time
Interrupts and ISR (continued . . .)
8/14/15
206
THIRD TYPE OF
COMMUNICATION
- INTERRUPTED I/O
BUS
P
INT
I/O
Device-1
INT
......
INT
I/O
Device-n
INT
8/14/15
207
8/14/15
208
1) Predefined interrupt
Some error condition produced by execution of an instruction,
e.g., trying to divide some number by zero. (Interrupt due to
exceptions)
8/14/15
209
8/14/15
210
1.
2.
3.
4.
5.
6.
8/14/15
211
main
Program
1. Push FLAGS
2. Clear IF
3. Clear TF
4. Push CS
5. Push IP
6. Fetch ISR
address
Interrupt
Service Routine
(ISR)
PUSH registers
...
...
...
....
POP IP
POP CS
POP FLAGS
...
....
...
POP registers
IRET
8/14/15
212
2.
8/14/15
213
2.
3.
5.
6.
7.
8.
8/14/15
214
8/14/15
215
Note :
The IP value is put in as the lower word of the vector
and CS as higher word of the vector
8/14/15
216
AVAILABLE
3FFH
...
FOR USER
(224)
TYPE 255
080H
TYPE 32
TYPE 31
...
RESERVED (27)
014H
TYPE 5
TYPE 4
010H
Overflow INTO
TYPE 3
Predefined/
00CH
Dedicated/Internal
Interrupts Pointers
TYPE 2
008H
(5)
NON-MASKABLE
TYPE 1
004H
CS Base Address
SINGLE STEP
TYPE 0
8/14/15
218
Priority
HIGHEST
LOWEST
8/14/15
219
CALL Instruction
INTn instruction
Upon the execution ,the control Upon execution the control will
will jump to any one of the 1 MB jump to a fixed location in the
of memory locations .
vector table.
3
4
When initiated ,it stores the When initiated ,it stores the
CS:IP of the next instruction on CS:IP of the next instruction and
the stack
also the flag register on the
stack.
The last instruction of the The last instruction of the ISS
subroutine will be RET
will be IRET
Interrupts and ISR (continued . . .)
8/14/15
220
8/14/15
221
8/14/15
222
OVERVIEW
INTRODUCTION TO 8086
8086 ARCHITECTURE
ADDRESSING MODES
8086 INSTRUCTION SET
ASSEMBLER DIRECTIVES
ASSEMBLY LANGUAGE PROGRAMMING
MODULAR PROGRAMMING
STACKS
PROCEDURES
MACROS
INTERRUPTS AND INTERRUPT SERVICE
ROUTINES
8/14/15
223
8/14/15
224
STRINGS
A string is a series of bytes or words stored in
successive memory locations
8086 can perform the following operations on
strings
Moving a string from one place in memory to
another
Compare two strings
Search a string for a specified character
Strings (continued . . .)
8/14/15
225
STRING INSTRUCTIONS
String instructions
MOVS
MOVSB
MOVSW
LODS
LODSB
LODSW
STOS
STOSB
STOSW
Strings (continued . . .)
8/14/15
226
MOVING A STRING
MOVSB/ MOVSW Instruction
Copies a byte or word from a location in the data
segment (DS) to the location in the extra segment
(ES)
Offset of source in data segment must be in SI
register
Offset of destination in extra segment must be in
DI register
For multiple byte/word moves the count is stored
in CX register
Strings (continued . . .)
8/14/15
227
Strings (continued . . .)
8/14/15
228
A PROGRAM TO COPY A
STRING OF BYTES FROM
ONE LOCATION IN MEMORY
TO ANOTHER
Strings (continued . . .)
8/14/15
229
Strings (continued . . .)
8/14/15
230
DATA SEGMENT
MSG1 DB TIME FOR A NEW HOME
MSG2 DB JUMP OVER TO MSG3
MSG3 DB 23 DUP(0)
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS: DATA, ES: DATA
START: MOV AX, DATA
MOV DS, AX ;initialize data segment register
MOV ES, AX
;initialize extra segment register
LEA SI, MSG1 ;point SI at source string
LEA DI, MSG3 ;point DI at destination string
MOV CX, 19
;use CX register as counter
CLD ;clear direction flag, so counter decrements
REP MOVSB ;move string bytes until all moved
CODE ENDS
Strings (continued . . .)
8/14/15
231
Strings (continued . . .)
8/14/15
232
8/14/15
233
INT
8086 recognizes two kinds of interrupts, hardware interrupts and software
interrupts. Hardware interrupts are generated when the peripheral, connected to the
CPU requests for some service. A software interrupt is a call to a subroutine located
in the operating system, usually the input-output routine.
INT (interrupt) instruction is used within application programs to request the
services of DOS, or ROM-BIOS.
The INT instruction calls an operating system subroutine, identified by a
number, in the range 0 - FFh. The syntax is INT number
The CPU processes an interrupt instruction, using the interrupt vector table (IVT).
It is situated in the first 1K bytes of memory, and has a total of 256 entries, each of
4bytes. The entry in the interrupt vector table, is identified by the number, given in
the interrupt instruction, and in turn points to an operating system subroutine. The
actual address in this table varies from machine to machine.
PROCESSING AN
INTERRUPT
STEPS
1. The number following the INT mnemonic tells the CPU which
entry to
locate in the interrupt vector table. The address of the entry can be
found by
multiplying the number by 4.
Example: the subroutine corresponding to INT 10h will be placed at
address a 40h , while for INT 3h will be placed at OCh.
2. The CPU jumps to the address stored in the interrupt vector table. The
actual routine for servicing the INT 10h, is placed at this address.
3. The CPU loads the CS register and the IP register, with this new address
in the IVT, and transfers the control to that address, just like a far CALL,
4. IRET (interrupt return) causes the program to resume execution at the
next instruction in the calling program.
21 H is called a DOS
function call. There are
some 100 different
functions supported by this
interrupt, identified by a
function number placed in
the AH register, prior to
calling INT.
INT 21 FUNCTION 01 H
Reads the character from the keyboard, and
echoes the character on the monitor. If no
character is ready, waits until one is
available.
Calls with: AH = 01
Returns: AL = 8 bit data input
INT 21 FUNCTION 01 H
Example: Read one character from the
keyboard into register AL, with echo, and
store it in the variable VAR 1
PGM: MOV AH,01H
INT 21 H
MOV VAR1,AL
VAR1 DB 0
INT 21 FUNCTION 02 H
Outputs the character on the monitor
Calls with: AH = 02
DL = 8 bit-data (usually ASCII, if you
want it printed on the screen)
Returns: nothing
Example: transmit the character '*' on the
screen
MOV AH, 02
MOV DL, '*'
INT 21h
INT 21 FUNCTION 08 H
Reads a character from the input device,
without echoing it on the output device.
Rest all other things are same as that of
function 01
INT 21 FUNCTION 09 H
Output a string on the standard output
device.
A string must be terminated by a '$'
character, which is not transmitted. Any
other code, including the control codes, like,
newline, tab etc., can be embedded in the
string
Calls with: AH = 09
DS:DX = segment:offset of string
Returns: nothing
INT 21 FUNCTION 09 H
Example:
CR EQU ODh ; code for carriage return
LF EQU OAh ; code for line feed
DATA SEGMENT
STR DB 'Hello world!',CR,LF,'$'
DATA ENDS
CODE SEGMENT
MOV AH,09
MOV DX, -DATA
MOV DS,DX
MOV DX, OFFSET -STR
INT 21h
INT 21 FUNCTION 0A H
Example:
DATA SEGMENT
BUFF DB 81 ; max length of string,;
including CR, 81 characters
DB 0 ; actual length of string used
DB 81 DUP(0)
DATA ENDS
EXAMPLE
CODE SEGMENT
MOV AH,0A H
MOV DX,DATA
MOV DS,DX
MOV DX,OFFSET BUFF
INT 21 H
CODE ENDS
INT 21 FUNCTION 4C H
END OF UNIT-1
Introduction to 8086
(continued.....)
8/14/15
24
9