Microprocessor Based Systems and Assembly Language Programming
Microprocessor Based Systems and Assembly Language Programming
2
About the Course
3
Course Outline
1. Introduction to Microprocessors
2. Addressing Modes
14. Microcontroller
6
Course objectives
• After successful completion of this course, you will be
able to
• Explain all basic operations of a computer
• Understand the psychology of computer
• (After seeing from so close) understand the limitations
and capabilities of a computer
• Your programming logic will become fine-grained. This
is the major objective.
7
Why to learn Assembly Language?
• Why to learn Assembly language when there exist easy
to use High-Level Languages (HLL)?
• HLLs use translators; translators are not accurate.
• When Japanese is translated to English, a word may be
replaced by a sentence.
• Translator destroy beauty of language.
• Similarly, compilers produce lot of garbage code.
• In normal programs, extra garbage code can be acceptable.
8
Why to learn Assembly Language?
• Example:
Each pixel contains 3 colors (RGB)
Consider a single video frame sized 800x640 pixels
A video generally contains 30 frames per second
Video compression/decompression algorithms deals
with pixels.
Few extra garbage instructions at pixel-level can cause
hours of extra processing time.
9
Why to learn Assembly Language?
• Assembly language also used for real-time systems.
• Real-time systems require time-bound response.
• Assembly language gives full control over instructions
being executed.
• For HLLs we cannot tell how many instructions are used.
10
Introduction to Assembly
Language
11
Basic Computer Architecture
• A computer system contains three basic components.
• Processor performs operations on data stored in memory.
Processor Memory
Peripherals
12
Basic Computer Architecture
• There need to be a mechanism to tell memory that
Processor wants to read data from memory, or write to it
Which data processor wants to read.
How to transfer this data from memory to processor
Processor Memory
Peripherals
13
Basic Computer Architecture
• Address bus: Group of bits that processor uses to inform
memory about which element to read or write to memory.
Processor Memory
Peripherals
14
Basic Computer Architecture
• A processor uses address, data and control buses in a
synchronous fashion to perform a meaningful operation.
• A programmer specifies this meaningful operation
Processor Memory
Peripherals
15
Basic Computer Architecture
• Suppose, memory contains 2000 different cells.
• The binary number represented by address bus tells that
which memory cell we want to read from or write to.
• Each memory cell is n-bit wide.
• n-bit data is transferred from/to processor on data bus.
address
data
Processor Memory
control
Peripherals
16
Registers
• A computer performs operations on operands.
• Suppose we want to perform an addition operation on
two operands placed in memory.
• We have only one data bus, and one address bus.
• One operand can be accessed from memory at a time.
• How to perform an operation with two operands?
• Temporary storage in a processor stores operands.
address
data
Processor Memory
control
Peripherals
17
Registers
• This temporary storage inside a processor are called as
registers.
• Registers are just like normal memory cells.
• Each register has a precise location.
• Registers remember what is stored in them.
• Registers are very scarce and precious resource.
• They are relatively very less in number as compared to
memory cells.
• Different registers are named by their unique names.
• Some manufactures name them as r1, r2, r3 ….
• Others name them as A, B, C….
• Some name them according to their functionality like index
register, etc
18
Accumulator Register
• This is a central register in every processor.
• Traditionally all mathematical and logical operations are
performed on the accumulator.
• The word size of a processor is generally defined by the
width of its accumulator.
19
Pointer/Index or Base Register
• The name of this register varies from manufacturer to
manufacturer.
• This register does not hold data; it holds address of data.
• The need for this register can be understood by examining a
“for” loop in higher level language.
20
Flag Register or Program Status Word
• This register is meaningless as a single unit.
• Individual bits of this register carry different meanings.
Example:
• An example of a bit present in this register is a
“carry flag”.
• A 16-bit number added to a 16-bit accumulator gives result in
17 bits.
• The 17th bit is stored in the “carry flag”.
• Without the 17th bit, the result of two 16-bit additions is
incomplete.
• More examples will be given later in detail.
21
Program Counter or Instruction Pointer
• A processor understands only binary language.
• Even the instructions must be translated to binary format
• All the instructions in High Level Languages are represented
as binary numbers at Assembly Level.
• These instructions can comprise of Multiplication, Addition,
etc, or Encapsulation, Inheritance, etc
• This one number tells the processor that it has to add, where
its operands are, and where to store the result.
23
Program Counter or Instruction Pointer
• The number representing an instruction is called as an
opcode.
• The instruction pointer moves from one opcode to another.
25
Data Movement Instructions
• These instructions are used to move data from one place to
another.
• These places can be registers, memory, or even peripheral
devices.
26
Arithmetic and Logic Instructions
• Arithmetic Instructions consists instruction like
• Addition, Subtraction, Multiplication, Division, etc
27
Program Control Instructions
• These instructions control program execution and flow
• We know that Instruction pointer consists of next instruction
to be executed.
• The instructions are placed in an order.
• Sometimes, we don’t want to follow a particular order.
• The Instruction pointer can be modified to change the flow of
program execution.
• So, the program control instruction control the program flow
by manipulating the Instruction Pointer.
• Some examples are
cmp ax, 0
jne 1234
28
Special Instructions
• These instructions have some special functionaliy
• These are rarely used, but are important
29
Intel IAPX88 Architecture
30
Why IAPX88?
• Wide availability
• Free assemblers and debuggers
• Wide use in variety of applications
31
What is iAPX88 Architecture?
• iAPX88 stands for “Intel Advanced Processor Extensions
88”.
• Also called at 8088 architecture.
• Used in very first IBM PC machine.
• We will study 8088, and 80386 architectures.
32
What is iAPX88 Architecture?
• 8088 is a 16-bit processor
• Accumulator and registers are 16-bit wide
33
History
• Intel released few 4-bit processors.
• 8080, an 8-bit processor, was first successful processor
• Popular due to simplistic design, versatile architecture
• Later, Intel launched 8085 architecture…
• 8088, first 16-bit architecture
• 8085 allowed maximum 65K memory
• 8088 allowed maximum 1MB memory
• IBM embedded 8088 in their PC. It ran on 4.43 MHz.
34
Register Architecture
• iAPX88 architecture consists of 14 registers
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL DX
35
General Registers (AX, BX, CX, DX)
• AX, BX, CX, DX behave as general purpose registers
• These registers also perform some special functions
• X means extended, i.e. they are 16-bit registers
• For example
• AX refers to 16-bit register
• Lower and upper 8-bit accessed through AH, AL
• AH means A High byte, AL means A Low byte
• Change in AH or AL is reflected on AX.
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
36
DX
General Registers (AX, BX, CX, DX)
• A of AX stands for Accumulator
• All general purpose registers can act as accumulator
• Some particular functions only related to AX
• B of BX stands for Base
• Because of its role in memory addressing
• C of CX stands for Counter
• Certain instructions work with automatic count
• D of DX stands for Destination
• It acts as destination in some I/O operations
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
37
DX
Index Registers (SI, DI)
• SI stands for Source Index
• DI stands for Destination Index
• These are index registers of Intel architecture
• They are used as source and destination in special class of
instructions, called as String Instructions.
• Mathematical and logical instructions are also allowed on
Index Registers.
• SI, DI are 16-bit; cannot be used as 8-bit register pairs
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
38
DX
Instruction Pointer (IP)
• Contains the address of next instruction to be executed
• No mathematical or memory operations done through IP
• It is out of our direct use; automatically used
• Program control instructions change IP register
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
39
DX
Stack Pointer (SP)
• Points to memory
• Indirectly used by a set of stack related instructions
• Will be discussed later with system stack.
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
40
DX
Base Pointer (BP)
• Points to memory
• Indirectly used by a set of stack related instructions
• Will be discussed later with system stack.
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
41
DX
Flags Register
• Not meaningful as a unit.
• It is bit wise significant
• Accordingly, each bit is named separately.
• Bits not named are unused
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
42
DX
Flags Register – Carry
• C for Carry
• 16 bit + 16 bit generates 17 bit result
• 8 bit + 8 bit generates 9 bit result
• Extra 1 bit of result is stored in Carry bit of flag register
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
43
DX
Flags Register – Parity
• P for Parity
• Parity is the number of 1s in a binary number
• Parity can be either odd or even
• For example 1000010000000010 has odd parity
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C
CS SP
DS BP • Parity is generally used to verify the
SS SI
integrity of a number during
ES DI
AH AL AX
communication.
IP BH BL BX
CH CL CX
FLAGS DH DL
44
DX
Flags Register – Auxiliary Carry
• A for Auxiliary Carry
• Numbers in base 16 are called as Hexa numbers
• A single hexa digit can be represented in 4 bits
• 4 bits are also called as nibble
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
46
DX
Flags Register – Sign
• S for Sign flag
• A signed number is represented in the form of 2’s
complement
• MSB of a -ve number is 1, and 0 for a +ve number
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
48
DX
Flags Register – Interrupt
• I for Interrupt flag
• It tells if the processor can be interrupted from outside
• Programmer can set or reset this bit using special instructions
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
O D I T S Z A P C
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
51
DX
Segment Registers (CS, DS, SS, ES)
• CS for Code Segment Register
• DS for Data Segment Register
• SS for Stack Segment Register
• ES for Extra Segment Register
• These are special registers for Intel segmented memory
model; Discussed later…
CS SP
DS BP
SS SI
ES DI
AH AL AX
IP BH BL BX
CH CL CX
FLAGS DH DL
52
DX
First Assembly Language
Program
53
Pseudocode for adding 3 numbers
move 5 to ax
move 10 to bx
add bx to ax
move 15 to bx
add bx to ax
54
Assembly Language for addition
ex01.asm
63
Segmented Memory Model
• Earlier processors (8080, 8085) used linear memory model
• Linear Memory Model can access 64K memory using 16-bit
wide address bus
• Designers of 8088 wanted to access more than 64K memory;
but wanted to remain compatible with 8080/8085
• Thus, segmented memory model was introduced.