Basic Microprocessor: Barbaros Özdemirel
Basic Microprocessor: Barbaros Özdemirel
Lecture 2
Basic Microprocessor
Barbaros Özdemirel
This lecture is a rapid review of microprocessor
functionality and organization from a user's point of
view.
The lecture contents provide an introduction to
microprocessor basics for a first time user.
Microprocessor Functions
• Execute a series of instructions in memory.
• Access memory to move data between
microprocessor registers and memory.
• Calculate or perform arithmetic and logic functions.
• Compare: Check for equality, less-than, greater-than
conditions.
• Branch: Make decisions according to comparison
results and call subprograms.
• Communicate: Send or receive data and control
signals to/from peripheral devices.
• Respond: Take action in response to external events,
such as user controls and peripheral device signals.
1
Focus on the first microprocessor function:
"Execute a series of instructions in memory"
A Random Access Memory (RAM) device can store data, and
it can return the stored data at the specified address.
The purpose is to build a digital machine that will execute
instructions coded as sequence of binary data in a RAM.
Address
? 16
8
Data
memory
digital RnW
machine Enable
8
Data
memory
digital RnW
machine Enable
2
Machine Language
Operation code (opcode in short)
memory tells microprocessor what to do.
contents Opcode specifies the operation
00110101 opcode type (LOAD, ADD, JUMP, ...) and a
11110011 data/addr method of accessing the data or
00101000 opcode address required for the operation.
01101110 data/addr After the opcode, microprocessor
10000110 reads the following memory
00000110 opcode location(s) to retrieve the data or
00110101 opcode address information. An opcode
01011110 data/addr together with this data or address
∙∙∙∙∙ ꞏꞏꞏꞏꞏ information forms a machine
instruction.
Microprocessor Organization
Registers
Controller
Program
Counter
Arithmetic and
Logic Unit
Address Data
Memory Interface
3
Program Counter (PC)
Program Counter is a special-purpose address register
that keeps track of the memory address of the machine
instruction to be executed. Following shows the typical
utilization of PC to handle program flow:
• Regular program flow: Microprocessor incremets PC to the
address of the next operation code.
• Jump to another location: Microprocessor loads the target
address from memory into the PC.
• Call a subprogram: Microprocessor saves the incremented
PC (address of the next operation code) at a safe memory
location (usually stack) and loads the target subprogram
address into the PC. PC value is restored when a "RETURN"
instruction is executed at the end of the subprogram.
Microprocessor Organization
Registers
Controller
Program
Counter
Arithmetic and
Logic Unit
Address Data
Memory Interface
4
Register?
R e g i s t e r I n p u t s
... IB7 IA7 ... IB1 IA1 ... IB0 IA0
InSel ...
D Q D Q D Q
...
FF7 FF1 FF0
Clk ...
Rout7 Rout1 Rout0
R e g i s t e r O u t p u t s
Microprocessor Organization
Registers
Controller
Program
Counter
Arithmetic and
Logic Unit
Address Data
Memory Interface
5
Arithmetic and Logic Unit (ALU)
Arithmetic and Logic Unit is a block heavily loaded with
combinational logic to perform operations between the
microprocessor registers and/or data retrieved from memory.
• Arithmetic operations: Addition, subtraction, negation. Some
microprocessors have multiplication too.
• Logic operations: Bit-wise NOT (inversion), AND, OR, EXOR.
• Setting condition flags: Generation of status information
according to the result after every ALU operation:
=0 : set if the result is zero
>0 : set if the result is positive
<0 : set if the result is negative
Carry : set if there is a carry after an arithmetic operation
Overflow : set if there is an overflow after the operation
A collection of these and similar flag bits is usually called as
"Status Register".
ALU?
A L U D a t a I n p u t s
A7 B7 A1 B1 A0 B0
...
...
InA InB InA InB InA InB
Cout Cin ... Cout Cin Cout Cin
...
OpSel ...
SnA
...
ALUout7 ALUout1 ALUout0
A L U D a t a O u t p u t s
6
Microprocessor Organization
Registers
Controller
Program
Counter
Arithmetic and
Logic Unit
Address Data
Memory Interface
Controller
Controller block is a complex finite state machine that
generates the necessary control signals for the chain of events
required for execution of instructions.
• Select the source of address for memory access cycle.
• Select the source of data to be written into memory.
• Select the target of data read from memory or who grabs the
incoming data:
Controller itself: opcode fetch
PC: target address of a JUMP or call
Registers: data LOAD from memory
ALU: operand of an ALU operation
Address buffer: direct or indirect addressing for data
• Select ALU operation type.
• Decide for conditional branching according to ALU status flags.
7
Controller?
Finite State Machine Control
Reset Signals
Interrupt to Other
Combinational Control
Data from Blocks
Next State Next Output
memory OCreg Decision state
Encoder
opcode Logic DFF
storage State
Storage
Clk
Previous state
Registers
All microprocessors are equipped with a number of
registers that are internal storage units. Microprocessor can
access the registers much faster than reading or writing a
memory location.
A register is nothing but a collection of flipflops organized
with the necessary combinational logic to perform specific
operations. Number of registers, their names and functionality
varies from one microprocessor architecture to another.
Typical register functions are:
• Temporary fast storage inside the microprocessor
• Storage for operation results (accumulator)
• Keeping status information (status register)
• Address generation (index register, stack pointer). Program
counter is a register too.
8
Accumulator
It is the register closely associated with the ALU. Some
microprocessor architectures consider accumulator as a part
of the ALU, setting it aside from the other registers
Accumulator keeps the result of the last arithmetic or logic
operation. This allows execution of a series of instructions
without storing intermediate results back in the memory.
Status register flags are set according to the operation
result or the data last loaded into the accumulator.
Some microprocessors have more than one register that
can be used as accumulator.
Index Register
The purpose of index register is to use the data stored in
the microprocessor as an address to access memory. In many
microprocessor architectures, an index register is the only way
to implement the "array" structures or the "pointer" functionality
that you can see in high-level languages like C.
Microprocessor first loads the index register with the
address information stored as data in the memory, and then
uses the index register as the source of memory address to
access the target data location.
An index register is usually equipped with counter
functions that facilitate quick access to sequential memory
blocks once the first address of the block is loaded into the
index register. The machine instruction set includes increment
and decrement operations on index registers to support the
sequential memory access.
9
Addressing Modes
Addressing modes are the methods of accessing memory to retrieve an
operand or to store the result of an operation.
The access method determines how the address of an operand is
obtained, or what will follow the opcode in a machine instruction.
• Immediate addressing: Data is stored immediately after the opcode in
program memory. Program counter provides the address of operand.
In a program, constant values, that are read-only data can be accessed
through immediate addressing.
• Direct addressing: Address of data follows the opcode.
First, the target address is read from program memory using the PC
address, and then data is read from or written to that address.
Simple variables can be accessed through direct addressing.
• Indirect addressing: Address of a pointer to data (address of address)
follows the opcode.
This addressing mode is used to implement pointers and arrays in high-
level programs.
Immediate Addressing
Data is stored immediately after the opcode. This
addressing mode is restricted to read-only operand data
stored with the executable code.
addr data comment 1) Controller gets opcode
1A00 3B at PC address.
1A01 28 2) Controller increments
1A02 C3 PC.
PC 1A03 57 opcode: LOADim Acc 3) Accumulator gets data
1A04 33 data: 0x33 loaded into Acc
at PC address.
1A05 55
1A06 1A
4) Controller increments
1A07 21 PC.
1A08 38
1A09 0E
1A0A C3
∙∙∙∙ ∙∙∙∙
10
Direct Addressing
Address of data follows the opcode. This addressing mode
can be used for storing data in memory as well as retrieving an
operand from memory.
addr data comment 1) Controller gets opcode at
1A00 3B PC address.
1A01 28 2) Controller increments PC.
1A02 C3 3) Address buffer gets high
PC 1A03 56 opcode: LOADdc Acc addr. byte at PC address.
1A04 1A addr: high-byte of addr.
4) Controller increments PC.
1A05 09 addr: low-byte of addr.
1A06 08
5) Address buffer gets low
1A07 A5 addr. byte at PC address.
1A08 55 6) Controller increments PC.
1A09 33 data: 0x33 loaded into Acc 7) Accumulator gets data at
1A0A 21 the location targeted by the
∙∙∙∙ ∙∙∙∙ address buffer.
Controller Registers
OCreg
Program
Counter
Address Data
Memory Interface
Address flow Data flow
11
Indirect Addressing
Indirect addressing through memory storage is usually an
inefficient way to implement pointers and arrays because it requires:
1. read opcode addressed by PC,
2. read address of pointer to data addressed by PC,
3. read pointer to data using the retrieved address, and
4. read/write data using the pointer value.
Most of the time, we want to modify a pointer (or array index)
before or after accessing data, so it is better to read the pointer into
an index register in the microprocessor:
1. read opcode addressed by PC,
2. read address of pointer addressed by PC,
3. read pointer into an index register
This register-indirect addressing is much faster when the address
is readily available in the processor.
1. read opcode addressed by PC,
2. read/write data using the pointer value in the index register.
Register-Indirect Addressing
Address of data is stored in an index register. An opcode that
requires indirect addressing also specifies which register is to be
used for addressing. In the following example, 16-bit address is
loaded into the index register with the LOADim XR instruction
before using XR for indirect addressing.
addr data comment Target address is previously loaded
1A00 3B into XR index register (2-byte).
1A01 28 opcode: LOADim XR 1) Controller gets opcode at PC
1A02 09 im. data: low-byte of addr. address.
1A03 1A im. data: high-byte of addr. 2) Accumulator gets data at the
PC 1A04 C3 opcode: LOADid Acc, @XR
location (1A09) targeted by XR .
1A05 56 opcode: INC XR 3) Controller increments PC.
1A06 A3 opcode: ADDid Acc, @XR 4) Controller gets opcode and
1A07 70 increments XR.
1A08 55 5) Controller increments PC.
1A09 33 data: 0x33 loaded into Acc 6) Controller gets opcode.
1A0A 21 data: 0x21 added to Acc 7) Data from the address in XR
∙∙∙∙ ∙∙∙∙
(1A0A) added to Acc.
8) Controller increments PC.
12
Memory Access: Address Paths
• From PC: Opcode fetch. Loading immediate data
that follow the opcode in the program. Reading
address of data in direct addressing mode.
• From Registers: Address of data is obtained from
an index register.
• From Address Buffer: Address of data follows the
opcode in direct addressing mode. First, address of
data is loaded into the address buffer while PC is
the address source, then data is loaded or stored
while address buffer is the address source.
Controller Registers
OCreg
Program
Counter
Address Data
Memory Interface
Address flow Data flow
13
Memory Access: Data Paths
• To Controller: Opcode fetch.
• To Registers: Loading data from memory into a register.
• From Registers: Saving register data into memory.
• To ALU: Retrieving an operand data for an operation.
• From ALU: Storing result of an operation.
• To PC: Retrieving a new program address (through the
address buffer) for a branch or call operation.
• To Address Buffer: Retrieving address of data for direct
addressing.
• To Memory: Storing contents of a register in memory.
Instruction Execution:
LOAD A, #0; Load Register-A Immediate
14
Instruction: Load Register-A Immediate
Cycle-1: Opcode Fetch
Address
Buffer
Address Data
Memory Interface 2
Address flow Data flow
Address
Buffer
Address Data
Memory Interface 6
Address flow Data flow
15
Instruction Execution:
ADD A, #11; Add Register-A Immediate
Address Data
Memory Interface 6
Address flow Data flow
16
Instruction: Add Register-A Immediate
Cycle-3: Add operand and store result
5) PC => memory address
Controller Index Register
6) Memory wait state
Stack Pointer
7) ALU gets data:
A B Register-A => ALUinA
PC 8
10 7 Memory data => ALUinB
ALU 9 8) Increment PC
5
9) ALU performs
Address addition operation.
7
Buffer
10) Store the result:
Address Data ALUout => Register-A
Memory Interface 6
Address flow Data flow
Instruction Execution:
LOAD A,<addr>; Load Register-A Direct
address data
Initial values:
PC=1A04, Opcode=76, Addr. Buf.=????,
1A00 36
Register‐A=11
1A01 00
1A02 76 After Cycle-1: Fetch opcode from memory
1A03 11 PC=1A05, Opcode=62
PC >
1A04 62
1A05 1A After Cycle-2: Get high byte of the target address
1A06 55 from the memory byte following opcode (PC+1)
1A07 A1 PC=1A06, Addr.Buf.=1A??
1A08 20
After Cycle-3: Get low byte of the target address
1A09 BB
from the next memory address (PC+2)
... ... PC=1A07, Addr.Buf.=1A55
1A55 33
1A56 After Cycle-4: Read data into Register-A from
00
... ... memory address 1A55
PC=1A07, Register‐A=33
17
Instruction: Load Register-A Direct
Draw data/address paths and mark sequence
5)
Controller Index Register 6)
Stack Pointer 7)
A B
PC 8)
9)
ALU
10)
11)
Address
Buffer 12)
13)
Address Data 14)
Memory Interface 15)
Address flow Data flow
18
Instruction: Load Register-A Direct
Cycle-3: Get low byte of operand address
5) PC => memory address
Controller Index Register 6) Memory wait state
Stack Pointer 7) Get high byte of address:
A B Mem. data => Addr. buffer
PC 8 12 8) Increment PC
9) PC => memory address
ALU
5 10) Memory wait state
9 11) Get low byte of address:
Address 7 Mem. data => Addr. buffer
Buffer 11 12) Increment PC
13)
Address Data 14)
Memory Interface 6 10 15)
Address flow Data flow
19
Instruction Execution:
JUMP <addr>; Jump to Absolute Address
ALU 9)
10)
11)
Address
Buffer 12)
20
Instruction: Jump to Absolute Address
Cycle-2: Get high byte of target address
5) PC => memory address
Controller Index Register 6) Memory wait state
Stack Pointer 7) Get high byte of
A B address:
PC 8 Mem. data => Addr. buffer
8) Increment PC
ALU
5 9)
10)
Address 7
11)
Buffer
12)
Address Data
13)
Memory Interface 6
Address flow Data flow
21
Instruction: Jump to Absolute Address
Cycle-4: Transfer target address to PC
5) PC => memory address
Controller Index Register 6) Memory wait state
Stack Pointer 7) Get high byte of address:
A B Mem. data => Addr. buffer
PC 8 12 8) Increment PC
13
ALU 9) PC => memory address
5 10) Memory wait state
9 11) Get low byte of addr.:
Address 7
Mem. data => Addr. buffer
Buffer 11 12) Increment PC
22
Relative Addressing Benefits
Faster execution: Absolute addressing requires two memory
read cycles to retrive an address whereas relative addressing
takes only one cycle. Time savings can be significant assuming
that memory access is the most time-consuming cycle.
Saves memory: Relative addressing instructions use less
memory.
Code relocation: A program segment or a subprogram that
uses relative addressing can be moved to a different memory
location without changing the code. On the other hand, all
absolute address references had to be updated whenever an
absolute target is relocated.
Data records: If the base address is stored in an index
register, then a single subprogram can be used on different data
sets simply by changing the address in the index register.
23
I/O With Outside World
A microprocessors can communicate with external devices
through the same address and data lines used for memory
access. There are two widely used addressing options:
1. There are dedicated instructions for I/O operations.
Microprocessor signals external devices through an additional
control line while executing I/O instructions. This signal enables
addressing of other devices instead of the memory.
2. A section of the memory address space is reserved for I/O
operations. An external address decoder redirects any memory
access at these addresses to I/O devices. This I/O addressing
method is called "memory mapped I/O."
In case of a microcontroller, I/O ports, channels, and other
peripherals are integrated in a single device. Several special-
function-registers (SFR) are used as a common method of
access to these peripherals.
24