Avr A & A: Rchitecture Ssembly
Avr A & A: Rchitecture Ssembly
ASSEMBLY
General
purpose
registers
Special On-chip
function Flash
registers (Program)
Memory
Execution
Engine
WHY USE ASSEMBLY PROGRAMMING?
Full access to hardware features
Compiler limits a programmers access to the hardware
features that the compiler writer decided to implement
Writing time critical portions of code
Allows tight control over what the CPU is doing on every clock
cycle
Debugging
It in not uncommon when trying to debug odd system behavior
to have to look at disassembled code
http://class.ece.iastate.edu/cpre288 4
ATMEGA128 ARCHITECTURE OVERVIEW
8 bit processor
size of bus is 8 bits
http://class.ece.iastate.edu/cpre288
size of registers is 8
bits
RISC architecture
Harvard architecture
separate data and
instruction memory
133 instructions
5
ATMEGA128 BLOCK DIAGRAM
CPU
http://class.ece.iastate.edu/cpre288 6
ATMEGA128: RISC CPU ARCHITECTURE
What is RISC?
Reduced Instruction Set Computer (with respect to
CISC, Complex Instruction set Computer)
Typical RISC
Load/Store based: ALU to memory transaction via
registers
Most instruction are the same length
Typically fewer instructions than a CISC
architecture
Typically many more registers than CISC since Data
must be moved into a register before it can be
operated on
Low number of instructions typically makes
hardware design simpler (as compared to CISC)
Ref: http://www.seas.upenn.edu/~palsetia/cit595s07/RISCvsCISC.pdf (Diana Palsetia)
http://class.ece.iastate.edu/cpre288 7
ATMEGA128 CPU CORE SUMMARY
Most instructions are 16-bit or 32-bit
Takes one or two cycles to fetch
http://class.ece.iastate.edu/cpre288 8
ATMEGA128 BLOCK DIAGRAM
Program
Memory
http://class.ece.iastate.edu/cpre288 9
ATMEGA128 BLOCK DIAGRAM
Data
Memory
http://class.ece.iastate.edu/cpre288 10
ATMEGA128: HARVARD ARCHITECTURE
Program Data
Memory Memory
64K
(128KB) CPU (4KB) 4K rows
rows
16‐bits 8‐bits
Program memory
Flash based: Program persists even if power turned off (non-volatile)
16-bits wide, instructions are 16-bit (typical) or 32-bit wide.
Data Memory
SRAM based: Data disappears if power is turned off (volatile)
8-bits wide: all data and registers are stored as 8-bit chunks.
11
http://class.ece.iastate.edu/cpre288
ATMEGA128: LOGICAL DATA MEMORY ORGANIZATION
CPU
Data
32 8-bit Memory
4K
Registers (4KB) rows
ALU
Registers: 8‐bits
8-bits wide
Directly accessible by ALU
Data Memory:
8-bit wide
Must use a register to source data to ALU/store the result of ALU
http://class.ece.iastate.edu/cpre288 12
ATMEGA128: MEMORY MAP ORGANIZATION
General
CPU Reg
I/O Reg
Ext: I/O
Reg
4KB 4K
(Internal rows
Address layout: SRAM)
First 32 rows (0 – 0x1F) are gen purpose registers
Next 64 rows (0x20-0x5F) are I/O registers 8‐bits
Next 160 rows (0x60-0xFF) are Extended I/O registers
Next 4096 rows (0x0100 – 0x10FF ) are Internal SRAM
http://class.ece.iastate.edu/cpre288 13
General Purpose Registers
8-bits
R0
R1
R2
R26
X
R27
R28
Y
R29
R30
Z
R31
GENERAL PURPOSE REGISTER FILE
http://class.ece.iastate.edu/cpre288 15
ATMEGA128: GP REGISTERS (CONT.)
32 8-bit general purpose registers
Used for accessing SRAM
Used for storing function parameters
Used for instructions to execute operations on
What is an 8-bit register.
Basically just 8 D-Flip-flops connected together
http://class.ece.iastate.edu/cpre288 16
ATMEGA128: GP REGISTERS (CONT.)
R16 – R31 are the only registers that can have immediate
values loaded (e.g. LDI R17, 5)
http://class.ece.iastate.edu/cpre288 17
ATMEGA128: GP REGISTERS
16-bit Data stored across adjacent registers
Used for accessing SRAM
Used for storing function parameters
Used for instructions to execute operations on
32-bit data stored across adjacent registers.
http://class.ece.iastate.edu/cpre288 18
SREG: Status Register
I T H S V N Z C
http://class.ece.iastate.edu/cpre288 20
STATUS REGISTER (CONT.)
http://class.ece.iastate.edu/cpre288 21
STATUS REGISTER (CONT.)
http://class.ece.iastate.edu/cpre288 22
SPECIAL FUNCTION REGISTERS (SFR)
SREG
MCUCR
ADCSR
PINF
OCR1A
SREG: Status Register
I T H S V N Z C
X
Memory
+8 (displacement)
R1
SUB Rd, Rr Subtract without Rd ←Rd - Rr
Carry
SUBI Rd, K Subtract Imm Rd← Rd - K
CLR Rd CLR Rd Rd ← Rd ^ Rd
INC Rd Increment Rd ←d + 1
IN Rd, P In Port Rd ← P
.DSEG
StartA: .BYTE 100 ;100 bytes for Array A
.CSEG
LDI R26, low(StartA) ;XL – LB of starting address for
A
LDI R27, high(StartA);XH – MB of starting address for
A
LDI R17, 100 ; i = 100
LOOP: LD R16, X ; A[i]
SUBI R16, -1 ; A[i]=A[i]+1
ST X+, R16
SUBI R17, 1 ;i=i-1
BRNE LOOP ; branch on not zero
.ESEG
StartA: .DB 1,1,1,1,1,1,1,1,….,1 ;100 bytes
for Array A defined in EEPROM
.DEF temp=R16
.CSEG
.ORG 0x1200
StartA: .DB 1,1,1,1,1,1,1,1,….,1 ;100 bytes
for Array A defined in flash/text memory
MACROS
.MACRO SUBI16 ; Start macro definition
subi r16,low(@0) ; Subtract low byte
sbci r17,high(@0) ; Subtract high byte
.ENDMACRO ; End macro definition