Documente Academic
Documente Profesional
Documente Cultură
http://www.sparkfun.com/news/395
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
NOP
No Operation PC + 1 PC
SEI / CLI
Inhibit / Allow Interrupts
HCF
3
Addressing Modes
Immediate Addressing
Opcode 0xAF
Opcode 0x00
Opcode 0xAF
0xAF
0x00
8-bit register
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
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], R0
Register R0 (Memory [0xA000])
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
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
0xB000 0xC000
0xC000 0x0F0A
12
Addressing Modes
Register Indirect Advantages / Disadvantages
Addressing Modes
Indexed Addressing
A family of addressing modes used to add indexing to many of the previous modes of addressing
BRR 0x05
PC + 5 PC
R1
0xB000
Base Register
Addressing Modes
Register Indirect Advantages / Disadvantages
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
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
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
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
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
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:
31
Next Sections
32