You are on page 1of 32

MIE438 Microprocessors and Embedded Microcontrollers

Lecture 3 Addressing Modes

http://www.sparkfun.com/news/395

Department of Mechanical and Industrial Engineering

Code Compilation
Op-Codes

High level (and assembly) code is compiled into program data; atomic instructions each have an op-code which is decoded by the C

Addressing Modes
Inherent Addressing

Simplest mode: Address, data, or register is inherently specified by the instruction.

NOP
No Operation PC + 1 PC

SEI / CLI
Inhibit / Allow Interrupts

HCF
3

Addressing Modes
Immediate Addressing

For immediate addressing, a value is explicitly stored within the code

MOV R0, 0xAF


0xAF Register 0

0xA000 0xA001 0xA002

Opcode 0xAF

0xA000 0xA001 0xA002

Opcode 0x00

0xA000 0xA001 0xA002

Opcode 0xAF

0xAF

0x00

8-bit register

16-bit register, Big Endian

16-bit, Little Endian


4

Addressing Modes
Immediate Advantages / Disadvantages

Best used to implement constants or other fixed data types Advantages: No memory reference (generally faster)
Disadvantages: Increases size of code in memory Not useful for general-purpose variables
5

Addressing Modes
Register Addressing

For register addressing, the operands are registers only

MOV R0, R1
Register 1 Register 0

ADD R0, R1
R0 + R1 R0

MOV A, R1
Register 1 Accumulator A

Addressing Modes
Register Advantages / Disadvantages

Best used to implement transfers/assignment between variables Advantages: Can often be stored with just the opcode
Disadvantages: No access to memory yet Need a source of data (constants or I/O)
7

Addressing Modes
Direct Addressing

Direct addressing allows one to directly specify a memory location to access (with varying integration)

MOV [0xA000], 0xAF


Value 0xAF (Memory [0xA000])

MOV [0xA000], R0
Register R0 (Memory [0xA000])

MOV R0, [0xA000]


(Memory [0xA000]) R0

ADD [0xA000], 0x02


(Mem. [0xA000]) + 0x02 ([0xA000])

ADD [0xB000], [0xA000], 0xAF


(Mem. [0xA000]) + 0xAF (Mem. [0xB000])
8

Addressing Modes
Direct Advantages / Disadvantages

Best used to implement constant loading, memory-mapped I/O, etc. (anything at a constant address!)
Advantages: Gives direct access to memory Disadvantages: Address space limited by operand width
9

Addressing Modes
Indirect Addressing

For indirect addressing, the memory location in the operand contains the actual address of the data to be used by the instruction
0xA000 0xA001 0xB000 0xB0 0x00 0xCD

MOV R0, @[0xA000]


(Memory 0xB000) Register 0

10

Addressing Modes
Indirect Advantages / Disadvantages

May be multiple levels of indirection Best used to implement pointers, arrays, etc.
Advantages: Allows efficient implementation of pointers, arrays, lists, etc. Disadvantages: Two or more memory accesses per instr.
11

Addressing Modes
Register Indirect Addressing

Similar to basic indirect addressing, except the memory address of the operand is stored in a register instead of another memory location
R0 R1 ??? 0xB000

MOV R0, @R1


(Memory 0xB000) Register 0

0xB000 0xC000

0xC000 0x0F0A

MOV R0, @@R1


(Memory 0xC000) Register 0

12

Addressing Modes
Register Indirect Advantages / Disadvantages

Best for variable pointers, variable arrays, etc.


Advantages: We can easily have variable addresses for the first level of indirection Disadvantages: Address space is limited to register width
13

Addressing Modes
Indexed Addressing

A family of addressing modes used to add indexing to many of the previous modes of addressing

Relative Base Register

BRR 0x05
PC + 5 PC

MOV R0, @R1+5


(Memory 0xB000 + 5) Register 0

R1

0xB000

Base Register

MOV R0, @[R1+R2]


14

Addressing Modes
Register Indirect Advantages / Disadvantages

Better handling of arrays, structures, etc.


Advantages: Combines many steps involved in complex data manipulation Allows pointer arithmetic Disadvantages: Requires arithmetic within instruction
15

Addressing Modes
Exotic

There are many more exotic addressing modes: Base plus Index plus Offset Effective Address = [Base + Index + Offset] Scaled Effective Address = [Base + Scale X Index] Register Auto-increment/decrement Automatically increments a register after effective address is calculated Many more
16

Programming Example
Exam Example 6

Exam Example 6 Write a program to calculate the factorial of a number: Assume the number is stored in R0 Ignore checking pre-conditions

17

Programming Example
Exam Example 7

Exam Example 7 Write a program to calculate the square root of a number S by the following procedure: Assume S is stored in R0 Set xn = S / 2 Iterate the following: xn+1 = *(xn + S/xn)

18

Program Flow
Some comments

You may have noticed that it is difficult for us to write useful programs at this point We are missing basic components for flow control:
Condition Checking and Conditional Branching Subroutines and Calling

19

MIE438 Microprocessors and Embedded Microcontrollers


Loops, Subroutines, and Code Structure

Department of Mechanical and Industrial Engineering

20

Branch Instructions
A necessity for meaningful programs

We require branching and conditional branching to write most useful programs: Anything with loops or non-linear paths Some texts use branch and jump interchangeably; we will distinguish two types of instructions

21

Direct Branches
Simple PC-modifying instruction

A direct branch is typically a simplified or low-cost version of a full jump instruction It loads PC with the direct value that follows Most instruction sets do not allow any branch or jump instructions to use registers or memory as operands

BRA 0xF5
0xF5 PC
22

Direct Branches
Simple PC-modifying instruction

A direct branch is typically a simplified or low-cost version of a full jump instruction Typically addresses a smaller range of memory Some implementations use them to address special-purpose code (ex: interrupts)

23

Relative Branches
Increments or decrements the PC by a specified value relative to the current value Remember: PC typically points to next instr. to be read must factor this in when calculating

PC: 0x0100 BRR 0x05


PC + 0x0005 PC

example made

BRR 0xF5
PC + 0xFFF5 PC

PC: 0x0105

24

Relocatable Code
Relative branches allow code to be relocated Relocatable code can exist anywhere in memory; it does not hard-code any constant addresses
Benefits: Easy to compile and link Better cross-compatibility and upgradeability Self-modifying code
25

Direct Jump
Complete PC-modifying instruction

Nearly identical to direct branch It loads PC with the direct value that follows Typically addresses the entire memory range of the microcontroller longer instruction

JMP 0xF005
0xF005 PC
26

Conditional Branch / Jump


Conditional Execution

Both branch and jump instructions typically allow conditional branching, where a test is performed A conditional branch implies a conditional test Typically it is a subtraction, which may be explicit, implicit, or a general compare

27

Conditional Branch / Jump


Flavors

There are many different types (depend on ISA): BEQ / JEQ BZ / JZ BNE / JNE BNZ / JNZ BLT / JLT BO / JO BGT / JGT BNO / JNO BLE / JLE BS / JS BGE / JGE BNS / JNS BCC / JCC BCS / JCS
28

Conditional Branch / Jump


Implementation

Most of these simply check various status bits resulting from arithmetic (carry, overflow, etc.) Can actually be used after any arithmetic operation in most microcontrollers
If we are comparing two numbers / registers, we typically use a subtraction operation:

SUB R0, R1 BNE 0xF001


29

Conditional Branch / Jump


Implementation

However, many ISAs give the user an explicit compare instruction:

CMP R0, R1 BNE 0xF001


Aside from being user-friendly, this may have other benefits, again depending on the ISA Ex: May leave original carry / overflow / etc. flags intact after comparison
30

Conditional Branch / Jump


Example

Lets implement our factorial program again


To makes things more specific: Assume code is located starting at 0x0100 Make the code relocatable Assume all instructions are two bytes in length

31

Next Sections

1. Functions and Subroutines, Code Structure 2. Optimization and Execution

32

You might also like