Lec_04 AVR Architecture and Assembly Language Programming
Lec_04 AVR Architecture and Assembly Language Programming
Course
ME4162: Microprocessor AVR Microcontrollers
Semester 1, 2023 Introduction to Assembly
Lecturer: Dr. Duong Van Lac Dr. Duong Van Lac
Department of Mechatronics, HUST
Email: [email protected]
…
RAM EEPROM Timers
PROGRAM
SREG: I T H S V N Z C
Flash ROM R15
Program
Bus
CPU
Data
Bus CPU R16
R17
…
PC
Interrupt Other
OSC Ports R30
Unit Peripherals
Instruction decoder
R31
I/O
PINS Instruction Register
registers
1
10/9/2023
LDI (Load Immediate) ◼ There are some instructions for doing Arithmetic
◼ LDI Rd, k and logic operations; such as:
◼ Its equivalent in high level languages: ADD, SUB, MUL, AND, etc.
Rd = k ◼ ADD Rd,Rs
◼ Example: R0 ◼ Rd = Rd + Rs R0
R1 R1
◼ LDI R16,53
ALU
R2 ◼ Example: ALU
R2
ADD R25, R9
…
◼ R16 = 53 SREG: I T H S V N Z C ◼ SREG: I T H S V N Z C
R25 = R25 + R9
R15 R15
◼ LDI R19,$27 CPU R16 ◼
CPU R16
◼ LDI R23,0x27
R17
◼ ADD R17,R30 R17
…
PC PC
◼ R23 = 0x27 R30
◼ R17 = R17 + R30 R30
Instruction decoder Instruction decoder
LDI R23,0b11101100
R31 R31
◼ Instruction Register Instruction Register
registers registers
2
10/9/2023
…
◼ SREG: I T H S V N Z C ◼ SREG: I T H S V N Z C
R15 R15
…
PC PC
Instruction decoder
R30 ◼ DEC R23 Instruction decoder
R30
R31 R31
Instruction Register
registers
◼ R23 = R23 - 1 Instruction Register
registers
...
$0000 R2 $0000
$30 $10
General - $46 $26 TCNT0 $5C
General $3C - Other Purpose Purpose Other
Interrupt Interrupt
...
...
...
R31
$31 $11
Purpose - Purpose OSC Ports Example:
$001F Write
Registers a program
OSCthat adds the contents Portsof the PINC I/O
...
...
...
- $4A $2A$00 GPIOR1 $01
Registers Registers I/O (SFRs) of the SRAM (SFRs) I/O
...
...
$35 $15
(SFRs) TIFR0 $4A
Example: $005F
$2A$01
Add contents of location
GPIOR2
Example:0x90
(SFRs)
Storeto0x53
contents of location
into the 0x95 and
SPH register.
PINS
The store
$005F $005F
PINS
...
$005F
IN Rd,IOaddress ;Rd = [addr]
...
$0060 $0060
$0060 Example: What
the result$0060 does the
in location following
0x313. LDS
STS instruction
(Load
(Storedirect
direct do?
from data0x80
to data space)
space)
$3E
Solution: Example:
$3E
Example: Write
Example: Write programaddress
aa program
Extended that
that of
stores
copiesSPH
55
the is 0x5E
into location
contents of 0x80
of location RAM.of RAM Extended SREG $3F Extended
OUT IOAddr,Rd ;[addr]=Rd
...
Extended SREG $3F
I/O Memory I/O Memory
...
...
LDS R20, SRAM SRAM
...
...
SRAM
Solution:
Answer: $21FF
SRAM
IN R21,PIND
$21FF
IN
;R21 =R15,SREG
PIND
Example: ;IN R15,0x3F
$2200
LDS External
R21, 0x95 LDI ;R21R20,= 0x53
[0x95] ;R20 = 0x53
55 Example:
$2200
External Solution:
LDI R20, ;R20 = 55 External
SRAM
ADDSRAM
External
R20,R21 ;R20
IN R1, 0x3F
= R20 + R21 ;SREG = R12
;R1 = SREG
$FFFF
SRAM
It copies
ADD the
$FFFF
contents
SRAM
R20, R21 of STS
R2 into R20;= asR20
;R20
0x5E, 2 is+the
R20 R21address
;SPH of
= R2.
R20 $FFFF $FFFF OUT 0x3F,R12
STS 0x80,
LDS ATmega640/V
R20, 0x80 R20 LDS
;[0x80]
STS R1, = R20
0x60
;R20
0x60,R15 = 55
= [0x80] ; [0x60] = R15 ATmega328
ATmega640/V
IN R17,0x3E ;R17 = SPH
ATmega328 ATmega1280/V
STS ATmega1281/V
0x313, R20 ;[0x313] = R20
STS 0x90,R20
ATmega1280/V
;[0x90] = R20
ATmega64
ATmega128 STS ATmega2560/V
0x81, R20 ;[0x81] = R20 = [0x80]
ATmega64
ATmega128
ATmega1281/V
ATmega2560/V
OUT 0x3E,R15 ;SPH = R15
ATmega2561/V ATmega2561/V
3
10/9/2023
...
◼ LDI R16, 2 LDI
LDI R20,
R20,
LDILDI R20, 0x9C
0xA5
0x52
R16, 0x9C
0x38 ;R16 = 0x38
R0
Registers
$001F IO Address
LDI
LDI R21,
R21, 0x9C
0x23
0x73R1
LDI R17, 3
$00
ALU
LDILDI R21,
R17, 0x64
0x2F ;R17 = $0020
0x2F $01
R2 Standard IO
SUB
SUB R20,
R20, R21
R21 ;subtract
;subtract R21
R21 from R20
from R20
...
ADD
ADD R20,
R16, R21
R17 ;add
;add R21
R17 toto R20
R16
...
Registers
…
ADD R16, R17 SREG: I T H S V N Z C
Solution:
Solution:
$005F
SPH
SREG
$3E
$3F
Solution:
Solution: R15 11
CPU
$0060
$52
$9C
$A5 0101 0010
10011100
1010 1100
0101R16
$38
$9C 0011
1001 1000
- $23
$73 0111
R17 0011
...
+-- +$64
$9C
$2F 10010100
0010
0110 1100
0011
1111
$DF 1101 1111 R20
R20 === $DF
…
1110 0000 0000 0010 PC
$00
$82 0000
1000
$67 0000
00100110 0111R20 $00
$82
R16is==a00
0x67
C = 1 because R21 is bigger0000
$100 1 0000 R20
than R20 and there borrow from D8 bit.
C===100because
C becausethere
because R21 is
R21 is not
isnot bigger
bigger than R20
than R20 andbit.
and there is
there is no
no borrow
borrow from
from D8
D8 bit.
bit.
1110 0000 0001 0011 Z
C
C
== 00 decoder
because
because the
thereR20
is has
no
a carry
R30
a value
carry
beyond the D7
otherthe
beyond than
D7zero after the subtraction.
Instruction
Z=
Z
H == 1
01 because
because there
because the R20
the R20 iscarry
is ahaszero after
a value
from the D3
other
the than 0 bit.
subtraction.
to theafter the subtraction.
D4 bit.
0000 1111 0000 0001 H = 1 because there isR31 a borrow
carry from
from D4D3
the toto
D3.
the D4 bit.
H===100because
ZH
Instruction
becausethe
because there
there
R20 is(the
is no borrow
no borrow fromaD4
result) from
has D4 to D3.
to
value D3.
0 in it after the addition.
Z = 0Register
because the R16 (the result) has a value other than 0 after the addition.
registers
$FFFF
◼ Example:
myfile.eep myfile.hex myfile.map myfile.lst myfile.obj
.SET COUNT = 0x25
LDI R21, COUNT ;R21 = 0x25
DOWNLOAD TO DOWNLOAD TO LDI R22, COUNT + 3 ;R22 = 0x28
AVR’s EEPROM AVR ’s FLASH
.SET COUNT = 0x19
LDI R21, COUNT ;R21 = 0x19
4
10/9/2023
M328def.inc
.equ SREG = 0x3f
00 E205
.equ SPL = 0x3d
01 0000 .equ SPH = 0x3e
Program.asm 02 0000 ....
.ORG 0 03 0000
LDI R16, 0x25 04 0000
assembler
.ORG 0x7 05 0000
LDI R17, 0x34 06 0000
LDI R18, 0x31 Program.asm
07 E314
08 E321
LDI R20, 10
09 0000
OUT SPL, R20
0A 0000
I/O I/O
PINS PINS
5
10/9/2023
Bus
Interrupt Other
OSC Ports
Unit Peripherals
I/O
PINS
6
10/9/2023
PROGRAM
ALU
Flash ROM Execute
PC: Data
CPU Bus
Instruction dec.
Program
Bus
Control bus Control bus
Code Data
Interrupt
Ports
Other Memory Data bus CPU Data bus Memory
OSC Unit Peripherals
Address bus Address bus
I/O
PINS
7
10/9/2023