mie438 - spring 2014 - lecture slides 3(1)_615279

Upload: moises-henriques

Post on 04-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    1/32

    MIE438Microprocessors and Embedded

    Microcontrollers

    Department of Mechanical and Industrial Engineering

    Lecture 3Addressing Modes

    1

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

    http://www.sparkfun.com/news/395http://www.sparkfun.com/news/395
  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    2/32

    Code Compilation

    Op-Codes

    2

    High level (and assembly) code is compiledinto

    program data; atomic instructions each have an

    op-codewhich is decoded by the C

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    3/32

    Addressing Modes

    Inherent Addressing

    3

    Simplest mode: Address, data, or register is inherently

    specified by the instruction.

    NOP SEI / CLI

    No Operation

    PC + 1PC

    Inhibit / Allow Interrupts

    HCF

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    4/32

    Addressing Modes

    Immediate Addressing

    4

    For immediate addressing, a value is explicitly stored

    within the code

    MOVR0, 0xAF

    0xA000

    0xA001

    0xA002

    Opcode

    0xAFRegister 0

    0xAF

    0xA000

    0xA001

    0xA002

    Opcode

    0x00

    0xAF

    0xA000

    0xA001

    0xA002

    Opcode

    0xAF

    0x00

    8-bit register 16-bit register, Big Endian 16-bit, Little Endian

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    5/32

    Addressing Modes

    ImmediateAdvantages / Disadvantages

    5

    Best used to implement constantsor other fixed

    datatypes

    Advantages:

    No memory reference (generally faster)

    Disadvantages:

    Increases size of code in memory

    Not useful for general-purpose variables

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    6/32

    Addressing Modes

    Register Addressing

    6

    For register addressing, the operands are registers only

    MOVR0, R1Register 1Register 0

    ADD R0, R1R0 + R1R0

    MOVA, R1Register 1Accumulator A

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    7/32

    Addressing Modes

    RegisterAdvantages / Disadvantages

    7

    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)

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    8/32

    Addressing Modes

    Direct Addressing

    8

    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])

    MOVR0, [0xA000](Memory [0xA000])R0

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

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

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

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    9/32

    Addressing Modes

    DirectAdvantages / Disadvantages

    9

    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

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    10/32

    Addressing Modes

    Indirect Addressing

    10

    For indirect addressing, the memory location in the

    operand contains the actual address of the data to be

    used by the instruction

    MOVR0, @[0xA000]

    (Memory 0xB000)Register 0

    0xA000

    0xA001

    0xB000

    0xB0

    0x00

    0xCD

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    11/32

    Addressing Modes

    IndirectAdvantages / Disadvantages

    11

    May be multiplelevels 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.

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    12/32

    Addressing Modes

    Register Indirect Addressing

    12

    Similar to basic indirect addressing, except the memory

    address of the operand is stored in a registerinstead of

    another memory location

    MOVR0, @R1

    (Memory 0xB000)Register 0

    R0

    R1

    0xB000

    ???

    0xB000

    0xC000

    0xC000 0x0F0A

    MOVR0, @@R1

    (Memory 0xC000)Register 0

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    13/32

    Addressing Modes

    Register IndirectAdvantages / Disadvantages

    13

    Best for variablepointers, variablearrays, etc.

    Advantages:We can easily have variable addresses for the

    first level of indirection

    Disadvantages:

    Address space is limited to register width

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    14/32

    Addressing Modes

    Indexed Addressing

    14

    A family of addressing modes used to add indexingto

    many of the previous modes of addressing

    BRR 0x05PC + 5PC

    MOVR0, @R1+5

    (Memory 0xB000 + 5)Register 0

    R1 0xB000

    Relative

    Base Register

    MOVR0, @[R1+R2]Base Register

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    15/32

    Addressing Modes

    Register IndirectAdvantages / Disadvantages

    15

    Better handling of arrays, structures, etc.

    Advantages:Combines many steps involved in complex

    data manipulation

    Allows pointer arithmetic

    Disadvantages:

    Requires arithmetic within instruction

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    16/32

    Addressing Modes

    Exotic

    16

    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

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    17/32

    Programming Example

    Exam Example 6

    17

    Exam Example 6Write a program to calculate the

    factorial of a number:

    Assume the number is stored in R0

    Ignore checking pre-conditions

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    18/32

    Programming Example

    Exam Example 7

    18

    Exam Example 7Write 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)

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    19/32

    Program Flow

    Some comments

    19

    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

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    20/32

    MIE438Microprocessors and Embedded

    Microcontrollers

    Department of Mechanical and Industrial Engineering

    Loops, Subroutines, and Code Structure

    20

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    21/32

    Branch Instructions

    A necessity for meaningful programs

    21

    We require branching and conditional branching to

    write most useful programs:

    Anything with loops or non-linear paths

    Some texts use branchandjumpinterchangeably;

    we will distinguish two types of instructions

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    22/32

    Direct Branches

    Simple PC-modifying instruction

    22

    A direct branchis typically a simplified or low-cost

    version of a full jump instruction

    It loads PC with the direct value that followsMost instruction sets do notallow any branch

    or jump instructions to use registers or memory

    as operands

    BRA 0xF5

    0xF5PC

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    23/32

    Direct Branches

    Simple PC-modifying instruction

    23

    A direct branchis typically a simplified or low-cost

    version of a full jump instruction

    Typically addresses a smaller range of memorySome implementations use them to address

    special-purpose code (ex: interrupts)

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    24/32

    Relative Branches

    24

    Increments or decrementsthe PC by a specifiedvalue relative to the current value

    Remember: PC typically points to nextinstr. to

    be read

    must factor this in when calculating

    BRR 0x05

    PC + 0x0005PC

    PC: 0x0100

    BRR 0xF5

    PC + 0xFFF5PC

    PC: 0x0105

    example made

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    25/32

    Relocatable Code

    25

    Relative branches allow code to be relocated

    Relocatable code can exist anywherein memory; it

    does not hard-code any constant addresses

    Benefits:

    Easy to compile and link

    Better cross-compatibility and upgradeability

    Self-modifying code

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    26/32

    Direct Jump

    Complete PC-modifying instruction

    26

    Nearly identical to direct branch

    It loads PC with the direct value that follows

    Typically addresses the entirememory range ofthe microcontrollerlonger instruction

    JMP 0xF005

    0xF005PC

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    27/32

    Conditional Branch / Jump

    Conditional Execution

    27

    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

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    28/32

    Conditional Branch / Jump

    Flavors

    28

    There are many different types (depend on ISA):

    BEQ / JEQ

    BNE / JNEBLT / JLT

    BGT / JGT

    BLE / JLE

    BGE / JGE

    BCC / JCC

    BCS / JCS

    BZ / JZ

    BNZ / JNZ

    BO / JO

    BNO / JNO

    BS / JS

    BNS / JNS

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    29/32

    Conditional Branch / Jump

    Implementation

    29

    Most of these simply check various status bits

    resulting from arithmetic (carry, overflow, etc.)

    Can actually be used after anyarithmeticoperation in most microcontrollers

    If we are comparing two numbers / registers, we

    typically use a subtraction operation:

    SUB R0, R1

    BNE0xF001

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    30/32

    Conditional Branch / Jump

    Implementation

    30

    However, many ISAs give the user an explicit

    compare instruction:

    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

    CMP R0, R1BNE0xF001

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    31/32

  • 8/13/2019 MIE438 - Spring 2014 - Lecture Slides 3(1)_615279

    32/32

    Next Sections

    32

    1. Functions and Subroutines, Code Structure

    2. Optimization and Execution