Microcontroller Chapter 02
Microcontroller Chapter 02
Microcontroller Chapter 02
CHAPTER 2
AVR Architecture and Assembly
Language Programing
Department of Electrical Engineering
Khusnulhidayat[at]yahoo.com 1
Khusnulhidayat[at]yahoo.com 2
1
22/10/2018
Khusnulhidayat[at]yahoo.com 3
LDI Instruction
The LDI instruction copies 8-bit data into the
GPR. It has the following format:
LDI Rd,K
Note:
Load Rd (destination) with immediate value K, d
must between 16 and 31.
Example:
LDI R20, 0x25 ;load R20 with 0x25 (R20=0x25)
Khusnulhidayat[at]yahoo.com 4
2
22/10/2018
ADD Instruction
It has the following format:
ADD Rd,Rr
Note:
ADD Rr to Rd and store the result in Rd.
Example:
LDI R16,0x25
LDI R17,0x34
ADD R16,R17
Khusnulhidayat[at]yahoo.com 5
Khusnulhidayat[at]yahoo.com 6
3
22/10/2018
Khusnulhidayat[at]yahoo.com 7
Khusnulhidayat[at]yahoo.com 8
4
22/10/2018
Khusnulhidayat[at]yahoo.com 9
LDS Instruction
(LoaD direct from data Space)
It has the following format:
LDS Rd,K
Note:
Load Rd (0≤n≤31) with the contents of the
location K, K is an address between $0000 to
$FFFF.
Example:
LDS R5,0x200 ;0x200 is address on SRAM
Khusnulhidayat[at]yahoo.com 10
5
22/10/2018
Khusnulhidayat[at]yahoo.com 11
STS Instruction
(STore direct to data Space)
It has the following format:
STS K,Rr
Note:
Store register into location K, K is an address
between $0000 to $ FFFF.
Example:
STS 0x230,R25
Khusnulhidayat[at]yahoo.com 12
6
22/10/2018
Khusnulhidayat[at]yahoo.com 13
I/O Register
Khusnulhidayat[at]yahoo.com 14
7
22/10/2018
Khusnulhidayat[at]yahoo.com 15
MOV Instruction
The MOV instruction is used to copy data among
the GPR registers of R0-R31.
MOV Rd,Rr ;Rd=Rr (copy Rr to Rd)
Example:
MOV R10,R20
Khusnulhidayat[at]yahoo.com 16
8
22/10/2018
INC Instruction
INC Rd
Note:
Increment the contents of Rd by one (0≤n≤31).
Example:
INC R2 ;R2 = R2 + 1
Khusnulhidayat[at]yahoo.com 17
SUB Instruction
SUB Rd,Rr ;Rd = Rd – Rr
Example:
LDI R20,0x34
LDI R21,0x25
SUB R20,R21
STS 0x300,R20
Khusnulhidayat[at]yahoo.com 18
9
22/10/2018
DEC Instruction
DEC Rd ; Rd = Rd -1
Example:
LDI R30,3 ;R30 = 3
DEC R30 ;R30 has 2
DEC R30 ;R30 has 1
DEC R30 ;R30 has 0
Khusnulhidayat[at]yahoo.com 19
COM Instruction
COM Rd
Note:
Inverts (complements) the content of Rd and
places the result back into the Rd register.
Example:
LDI R16,0x55
COM R16 ;R16 = 0xAA
Khusnulhidayat[at]yahoo.com 20
10
22/10/2018
Khusnulhidayat[at]yahoo.com 21
Khusnulhidayat[at]yahoo.com 22
11
22/10/2018
Khusnulhidayat[at]yahoo.com 23
Flags Example
Khusnulhidayat[at]yahoo.com 24
12
22/10/2018
Flags Example
Khusnulhidayat[at]yahoo.com 25
Flags Example
Khusnulhidayat[at]yahoo.com 26
13
22/10/2018
Khusnulhidayat[at]yahoo.com 27
Assembler Directives
.EQU(equate)
This used to define a constant value or fixed
address.
Example:
.EQU COUNT = 0x25
..........................
LDI R21,COUNT ;R21 = 0x25
Khusnulhidayat[at]yahoo.com 28
14
22/10/2018
Assembler Directives
.INCLUDE
The .include directive tells the AVR assembler to
add the contents of a file to our program.
Example:
.INCLUDE “M32DEF.INC”
Khusnulhidayat[at]yahoo.com 29
Khusnulhidayat[at]yahoo.com 30
15
22/10/2018
Khusnulhidayat[at]yahoo.com 31
Add Bus
Data Bus
Khusnulhidayat[at]yahoo.com 32
16
22/10/2018
Khusnulhidayat[at]yahoo.com 33
Khusnulhidayat[at]yahoo.com 34
17
22/10/2018
Khusnulhidayat[at]yahoo.com 35
Khusnulhidayat[at]yahoo.com 36
18
22/10/2018
OUT Instruction:
Khusnulhidayat[at]yahoo.com 37
LDS Instruction:
Khusnulhidayat[at]yahoo.com 38
19
22/10/2018
Khusnulhidayat[at]yahoo.com 39
Khusnulhidayat[at]yahoo.com 40
20
22/10/2018
Futures of RISC
Future 1:
RISC processors have a fixed instruction size
(2-byte instructions with very few 4-byte).
In a CISC microcontroller such as the 8051,
instructions cab be 1, 2, or even 3 bytes.
This is like bricklayer working with bricks of
the same size as opposed to using bricks of
variable sizes. Of course, it is much more
efficient tu use bricks of the same sizes.
Khusnulhidayat[at]yahoo.com 41
Futures of RISC
Future 2:
RISC architecture is a large number of
registers, all RISC architecture have at least 32
register.
One advantage is that it avoids the need for
large stack to store parameters.
Khusnulhidayat[at]yahoo.com 42
21
22/10/2018
Futures of RISC
Future 3:
RISC processors have a small instruction set,
hardwiring of RISC instruction take no more
than 10% of transistors (CISC 40-60%).
Future 4:
More than 95% of instructions are executed
with only one clock cycle.
Khusnulhidayat[at]yahoo.com 43
Futures of RISC
Future 5:
In CISC processor, there is one set of buses for
the address and another set of buses for data.
In RISC processor, there are four sets of buses:
(1) a set of data buses for carrying data in and
out of the CPU, (2) a set of address buses for
accessing data, (3) a set of buses to carry the
opcodes, (4) a set of address buses to access
the opcodes.
Khusnulhidayat[at]yahoo.com 44
22
22/10/2018
Futures of RISC
Future 6:
RISC “load-store architecture”
– Only load/store access memory
– Benefit: simpler hardware easier to pipeline,
higher performace
CISC “register-memory architecture”
– All operations can have an operand in memory
– Benefit: fewer instructions smaller code
Khusnulhidayat[at]yahoo.com 45
References
Mazidi, M.A., NAIMI, S., NAIMI., S. (2011). The
AVR Microcontroller and Embedded System.
New York: Prentice-Hall.
Khusnulhidayat[at]yahoo.com 46
23
22/10/2018
Thank You
Khusnulhidayat[at]yahoo.com
Electrical Engineering Department
University of Muhammadiyah Malang (UMM)
Khusnulhidayat[at]yahoo.com 47
24