CS代考 COMP9032 Week1 1

Microprocessors & Interfacing
AVR ISA & AVR Programming (I)
Lecturer :
COMP9032 Week1 1

Copyright By PowCoder代写 加微信 powcoder

Lecture Overview
• AVRISAandInstructions
– A brief overview of our target machine
• AVR Programming (I)
– Implementation of basic programming structures
COMP9032 Week1 2

Atmel AVR (8-bit)
• RISC architecture
•RISC: Reduced Instruction Set Computer
•AL: Arithmetic and Logic
– Most instructions have 16-bit fixed length
– Most instructions take 1 clock cycle to execute
• Load-store memory access architecture
– All arithmetic and logic (AL) calculations are performed on registers
• Internal program memory and data memory
• Wide variety of on-chip peripherals (digital I/O, ADC, EEPROM, UART, pulse width modulator (PWM) …).
COMP9032 Week1 3

AVR Registers
• General purpose registers
– 32 8-bit registers, R0 ~ R31 or r0 ~ r31 – Can be further divided into two groups
• First half group (R0 ~ R15) and second half group (R16 ~ R31)
• Some instructions work only on the second half group R16~R31
– Due to the limitation of instruction encoding bits » Will be covered later
– E.g. ldi rd, #number ;rd  R16~R31
COMP9032 Week1 4

AVR Registers (cont.)
• General-purpose registers
– The following register pairs can work as address registers (or address pointers)
• X, R27:R26 • Y, R29:R28 • Z, R31:R30
– The following registers can be used for specific purposes
• R1:R0 stores the result of a multiplication instruction
• R0 stores the data loaded from the program memory
COMP9032 Week1 5

AVR Registers (cont.)
• I/Oregisters
– 64+ 8-bit registers
• Their names are defined in the m2560def.inc file – Used in input/output operations
• Mainly for storing data/addresses and control signal bits
– Will be covered in detail later
• Status register (SREG)
– A special I/O register
COMP9032 Week1 6

The Status REGister (SREG) contains information about the result of the most recently executed AL instruction. This information can be used for altering program execution flow in order to perform conditional operations.
SREG is updated by hardware after an AL operation.
– Some instructions such as load do not affect SREG.
SREG is not automatically saved when entering an interrupt routine and restored when returning from an interrupt. This must be handled by software.
– Using in/out instruction to store/restore SREG – To be covered later
COMP9032 Week1 7

SREG (cont.)
Bit 7 6 5 4 3 2 1 0
• Bit 0 – C: Carry Flag
– Its meaning depends on the operation.
• For addition x+y, it is the carry from the most significant bit.
• For subtraction x-y, where x and y are unsigned integers, it indicates whether x=0
• For example
std Y+10, r14
— (Y+10)r14
COMP9032 Week1 33

Indirect Addressing with Pre- decrement
• The address of memory data is from an address pointer (X, Y, Z) and the value of the pointer is auto- decreased before each memory access.
• For example
std -Y, r14
— YY-1, (Y)  r14
COMP9032 Week1 34

Indirect Addressing with Post- increment
• The address of memory data is from an address pointer (X, Y, Z) and the value of the pointer is auto- increased after each memory access.
• For example
std Y+, r14
— (Y)r14, YY+1
COMP9032 Week1 35

Direct Program Addressing
• The instruction address is from instruction • For example
COMP9032 Week1 36

Relative Program Addressing
• The instruction address is PC+k+1 • For example
— PCPC+k+1
COMP9032 Week1 37

Indirect Memory Addressing
• The instruction address is stored in Z register
— PC(15:0)(Z), PC(21:16)0
COMP9032 Week1 38

Program Memory Constant Addressing
• The address of the constant data is stored in Z register
– The address is a byte address. • For example:
COMP9032 Week1 39

Program Memory Constant Addressing with Post-increment
• For example
lpm r16, Z+
— r16(Z), ZZ+1
COMP9032 Week1 40

AVR Programming
• Refer to the AVR Instruction Set document for the complete list of instructions
– http://www.cse.unsw.edu.au/~cs9032, follow the link: References→Documents→AVR-Instruction- Set.pdf
– We will learn individual instructions through
• Lectures, homework, and lab exercises
• The rest of the lecture demonstrates AVR assembly programming
– By implementing some basic structures with examples
• Sequence • Selection • Iteration
COMP9032 Week1 41

Sequence (1/5) – example
• Find the value of expression
z = 2x − xy − x2
– where all values including the results from multiplications are 8-bit unsigned numbers; and x, y, z are stored in registers r2, r3, and r4, respectively.
COMP9032 Week1 42

What instructions do we need?
• sub • mul
COMP9032 Week1 43

Subtract without Carry
• Operands:
• Operation:
• Flags affected: H, S, V, N, Z, C
• Words: 1
• Cycles: 1
sub Rd, Rr
Rd, Rr  {r0, r1, …, r31} Rd  Rd – Rr
COMP9032 Week1 44

• Operands:
• Operation:
mul Rd, Rr
Rd, Rr  {r0, r1, …, r31} r1:r0Rr*Rd
Multiply Unsigned
– (unsigned  unsigned * unsigned ) • Flags affected: Z, C
– C is set if bit 15 of the result is set; cleared otherwise.
• Words: 1 • Cycles: 2
COMP9032 Week1 45

What instructions do we need?
• sub • mul • ldi
COMP9032 Week1 46

• Operands:
• Operation:
• Flag affected: None
Load Immediate
Rd{r16, …, r31}, 0 ≤ k  255 Rd  k
• Encoding:
• Example:
1110 kkkk dddd kkkk
ldi r16, $42 ; Load $42 to r16
COMP9032 Week1 47
8-bit binary

• Operands:
• Operation:
• Flag affected: None • Words: 1
• Cycles: 1
Copy Register
mov Rd, Rr
Rd, Rr {r0,r1,…,r31} Rd  Rr
COMP9032 Week1 48

• AVR code for
Sequence (2/5)
z = 2x − xy − x2
– where all values including results from multiplications are 8- bit unsigned numbers; and x, y, z are stored in registers r2, r3, and r4, respectively.
ldi r16, 2 mul r16, r2 mov r5, r0 mul r2, r3 sub r5, r0 mul r2, r2 sub r5, r0 mov r4, r5
; r1:r0  2x
; r1:r0xy
; r52x-xy
; r1:r0x2
; r52x-xy- x2 ; r4z
– 8 instructions and 11 cycles
COMP9032 Week1 49

• AVR code for
Sequence (3/5)
z = 2x − xy − x2
– where all data including products from multiplication are 8-bit unsigned numbers; and x, y, z are stored in registers r2, r3, and r4, respectively.
ldi r16, 2 mul r16, r2 mov r4, r0 mul r2, r3 sub r4, r0 mul r2, r2 sub r4, r0
; r1:r0  2x
; r1:r0xy
; r42x-xy
; r1:r0x2
; r42x-xy- x2
– 7 instructions and 10 cycles
COMP9032 Week1 50

Sequence (4/5)
• Find the value of the expression
z = 2x − xy − x2 = x(2−(x+ y))
– where all data including products from multiplications are 8-bit unsigned numbers; and x, y, z are stored in registers r2, r3, and r4, respectively.
COMP9032 Week1 51

What instructions do you need?
• sub • mul • ldi
• mov • add
COMP9032 Week1 52

Add without Carry
• Operands:
• Operation:
• Flags affected: H, S, V, N, Z, C
• Words: 1
• Cycles: 1
add Rd, Rr
Rd, Rr {r0, r1, …, r31} RdRd + Rr
COMP9032 Week1 53

• AVR code for
Sequence (5/5)
z = 2x − xy − x2
= x(2−(x+ y))
– where all data including products from multiplications are 8- bit unsigned numbers; and x, y, z are stored in registers r2, r3, and r4, respectively.
mov r4, r2 add r4, r3 ldi r16, 2 sub r16, r4 mul r2, r16 mov r4, r0
; r162-(x+y)
; r1:r0x(2-(x+y)) ; r4z
– 6 instructions and 7 cycles
COMP9032 Week1 54

Selection (1/2) – example
• IF-THEN-ELSE control structure
– Assume numbers a, b are 8-bit signed integers and stored in registers. You need to decide which registers to use.
• Instructions involved:
– Conditional branch – Unconditional jump
COMP9032 Week1 55

• Operands:
• Operation:
• Flags affected: H, S, V, N, Z, C
• Words: 1
• Cycles: 1
• Example:
cp r4, r5 brne noteq …
noteq: nop
; Compare r4 with r5 ; Branch if r4  r5
; Branch destination (do nothing)
Rd  {r0, r1, …, r31}
Rd – Rr (Rd is not changed)
COMP9032 Week1 56

Compare with Immediate
• Operands:
• Operation:
• Flags affected: H, S, V, N, Z, C
• Words: 1
• Cycles: 1
Rd {r16, r17, …, r31} and 0 k  255 Rd – k (Rd is not changed)
COMP9032 Week1 57

• Operands:
• Operation:
-64 ≤ k < 64 If RdRr (NV=0) then PCPC+k+1, Conditional Branch else PCPC+1 if condition is false • Flag affected: None • Words: 1 • Cycles: 1 if condition is false; 2 if condition is true S bit in SREG: S=0 COMP9032 Week1 58 • Operands: • Operation: • Flag affected: None • Words: 1 • Cycles: 2 Relative Jump -2K ≤ k < 2K PCPC+k+1 COMP9032 Week1 59 Selection (2/2) • IF-THEN-ELSE control structure – Numbers a, b are 8-bit signed integers and stored in registers. You need to decide which registers to use. .def a=r16 .def b=r17 cpi a,0 brge ELSE ldi b,1 rjmp END ;if a0, go to ELSE ;end of IF statement COMP9032 Week1 ELSE: ldi END: ... • WHILE loop Iteration (1/2) while (i<=n){ sum += i*i; – Numbers i, sum are 8-bit unsigned integers and stored in registers. You need to decide which registers to use. COMP9032 Week1 61 • WHILE loop Iteration (2/2) .def i = r16 .def n = r17 .def sum = r18 clr sum loop: add sum, r0 inc i ;initialization COMP9032 Week1 62 Reading Material • AVR Instruction Set online document about: – Instruction set nomenclature – I/O Registers (can skip for now) – The program and data memory Addressing – Arithmetic instructions, program execution flow control instructions COMP9032 Week1 63 1. Refer to the AVR Instruction Set document (available at http://www.cse.unsw.edu.au/~cs9032, under the link References → Documents → AVR- Instruction-Set.pdf). Study the following instructions: – Arithmetic and logic instructions • add, adc, adiw, sub, subi, sbc, sbci, sbiw, mul, muls, mulsu • and, andi, or, ori, eor • com, neg COMP9032 Week1 64 1. Studythefollowinginstructions(cont.) – Branch instructions • cp, cpc, cpi • breq, brne • brge, brlt • brsh, brlo – Data transfer instructions • ldi, ld, st COMP9032 Week1 65 2. Write assembly code for the following functions 1) 2-byte addition (i.e, addition on 16-bit numbers) 2) 2-byte signed subtraction 3) Sign-extension of one byte value to two bytes COMP9032 Week1 66