SlideShare a Scribd company logo
Chapter 4
Assembly Language
How to write a program
The ATmega324 Microcontroller
Nguyễn Trung Hiếu 1
Nguyễn Trung Hiếu
Ref: Giáo trình Vi Xử Lý, BMĐT
Instruction Format
[LABEL] Mnemonic [Op1][Op2] [;comment]
• […]: optional
• Op: operand
• Example:
➢LOOP: INC R1 ; R1 R1 + 1
➢ LDI R16,20 ; R16 20
➢ BREQ LOOP ; LOOP is name of label, Operand1
Nguyễn Trung Hiếu 2
Instruction Format
[LABEL] Mnemonic [Op1][Op2] [;comment]
• Operand can contain operators
• Example:
➢LDI R20,$0F&$75 ; R20  $05
➢LDI R22,255/7 ; R22  36
➢LDI R24,’A’<‘Z’ ; R24  1
➢LDI R26,HIGH($AB04) ; R26  $AB
Nguyễn Trung Hiếu 3
Assembler Directives
• Instructions for the compiler, not assembly
• Control the assembler, don't create any code
• The leading dot must be in column 1 of the line
• Segment:
➢ Everywhere
➢ Header
➢ Code
➢ EEPROM
➢ SRAM
Nguyễn Trung Hiếu 4
Assembler Directives: Everywhere
Nguyễn Trung Hiếu 5
.ORG
Defines the address within the respective segment, where
the assembler assembles to
syntax: .ORG 0x0000
.INCLUDE
Includes a file and assembles its content, just like its content
would be part of the calling file
syntax: .INCLUDE <M324PDEF.inc>
.EXIT
End of the assembler-source code
(stops the assembling process)
.LIST
Switches the listing to the .LST-file on (the assembled code
will be listed in a readable text file .LST)
.NOLIST Switches the output to the .LST-file off, suppresses listing
Assembler Directives: Header
Nguyễn Trung Hiếu 6
.DEF
Defines a synonym for a register
syntax: .DEF MyReg = R16
.SET
Defines a symbol and sets its value (later changes of this
value remain possible)
syntax: .SET test = 1234567
.EQU
Fixes the value of a symbol (later redefinition is not possible)
syntax: .EQU test = 1234567
Assembler Directives: Header
Nguyễn Trung Hiếu 7
.ORG 0
.EQU COUNTER = $A0
.EQU MYADDR1=$08
.SET MYADDR2 = 0x18
LDI R26,MYADDR1 ;R26=$08
LDI R27,MYADDR2 ;R27=$18
LDI R16, COUNTER ;R16 = 0xA0
.SET MYADDR2=0X0B ;redefine MYADDR2=$0B
OUT MYADDR1,R16 ;MYADDR1≡PORTC=0xA0
OUT MYADDR2,R16 ;MYADDR2≡PORTD=0xA0
Assembler Directives: Code
Nguyễn Trung Hiếu 8
.CSEG
Start of the code segment (all that follows is assembled to
the code segment and will go to the program space)
.DB
Inserts one or more constant bytes in the code segment
The number of inserted bytes must be even, otherwise an
additional zero byte will be inserted by the assembler
syntax: .DB 1,’a’,”bc”
.DW
Insert binary words in the code segment, not used string
syntax: .DW 5,’b’
.LISTMAC
Macros will be listed in the .LST-file.
(Default is that macros are not listed)
.MACRO
Beginning of a macro (no code will be produced, call of the
macro later produces code)
syntax: .MACRO macroname parameters
.ENDMACRO End of the macro
Assembler Directives: Code
Nguyễn Trung Hiếu 9
;Define Macro LOADIO
.MACRO LOADIO
LDI R20,@1
OUT @0,R20
.ENDMACRO
;Call Macro LOADIO
LOADIO PORTA, $20 ;PORTA=$20
.EQU VAL_1 = $FF
LOADIO DDRC, VAL_1 ;DDRC=$FF
LOADIO SPL, $55 ;SPL=$55
@0 and @1 is input parameters
@ can up to 9
Assembler Directives: EEPROM
Nguyễn Trung Hiếu 10
.ESEG
Assemble to the EEPROM-segment (the code produced will go
to the EEPROM section, the code produces an .EEP-file)
.DB
Inserts one or more constant bytes in the EEPROM segment
(could be numbers from 0..255, an ASCII-character like 'c', a
string like “abcde” or a combination like 1,2,3,'abc’)
syntax: .DB 1,’a’,”abcde”
.DW
Inserts a binary word to the EEPROM segment (the lower byte
goes to the next address, the higher byte follows on the
incremented address)
syntax: .DW 15,’d’
Assembler Directives: SRAM
Nguyễn Trung Hiếu 11
.DSEG
Assemble to the data segment (here only BYTE directives and
labels are valid, during assembly only the labels are used)
.BYTE
Reserves one or more bytes space in the data segment (only
used to produce correct labels, does not insert any values!)
Assembler Directives: SRAM
Nguyễn Trung Hiếu 12
.DSEG ; declare DATA segment
.ORG 0x120 ; SRAM, begin address at $120
VAR1: .BYTE 1 ; use 1 byte in SRAM for VAR1 at $120
.CSEG ; declare CODE segment
.ORG 0x10 ; FLASH, begin address = PC = $10
MOV R0,R1 ;
….. ;
….. ;
.ESEG ; declare EEPROM segment
VAR2: .BYTE 5 ; use 5 byte in EEPROM for VAR2
Instruction Types
• Data transfer
• Arithmetic
• Logical
• Boolean variable
• Program branching
• MCU control
Nguyễn Trung Hiếu 13
Used for creating loops,
branching and call subroutine
Program Branching Types
Nguyễn Trung Hiếu 14
• Call, Return
- Call subroutine: save information about where it currently is
executing and then jumps to the subroutine
- Return: when the subroutine is finished, it returns to where the
call was made
• Conditional Jump (Branch)
- If the Branch test is successful, the program jumps to the location
specified (no save information)
- If the Branch test is unsuccessful, the next line of code is executed
• Unconditional Jump (Jump)
- Always jump to the location specified (no save information)
Program Branching Types
Nguyễn Trung Hiếu 15
Call, return
Conditional and
Unconditional Jump
Subroutine Loops, branching
Call and Return
Nguyễn Trung Hiếu 16
MAIN: ... (1)
(RCALL,ICALL)CALL SUBLABEL (2)
... (3)
...
SUBLABEL: ... (4)
...
RET (5)
Structure:
Subroutine
Nguyễn Trung Hiếu 17
▪ Divide and Conquer – Allow you to focus on one small “chunk”
of the problem at a time
▪ Code Organization – Gives the code organization and structure.
A small step into the world of object-oriented programming
▪ Hierarchical Design – Moves information about the program at
the appropriate level of detail
▪ Code Readability – Allows others to read and understand the
program in digestible “bites” instead of all at once
▪ Encapsulation – Insulates the rest of the program from changes
made within a procedure
▪ Team Development – Helps multiple programmers to work on
the program in parallel; a first step to configuration control
Cho một số BCD nén nằm ở địa chỉ $220 của bộ nhớ SRAM. Viết
chương trình đọc số BCD đó và xuất giá trị ra 2 LED 7 đoạn kết nối
với PORTA (nibble cao) và PORTC (nibble thấp) của vi xử lý.
Nguyễn Trung Hiếu 18
Your Turn!
LDI R16,ENTRY_NUMBER
(LDI R17,$0)
LDI ZH,HIGH(TABLE<<1)
LDI ZL,LOW(TABLE<<1)
ADD ZL,R16
(ADC ZH,R17)
LPM R17,Z
TABLE: .DB data1, data2, data3, …
Nguyễn Trung Hiếu 19
(Remember) Look-up Table DATA: 8 bits (byte)
Program Memory
Address High Byte Low Byte
data2 data1
data4 data3
… …
… …
TABLE’S ADDRESS
If ENTRY_NUMBER = 0,
Z POINT TO
Nguyễn Trung Hiếu 20
Create the Table
D7 D6
g
D5
f
D4
e
D3
d
D2
c
D1
b
D0
a
0 0 1 0 0 0 0 0 0 40H
1 0 1 1 1 1 0 0 1 79H
2 0 0 1 0 0 1 0 0 24H
3 0 0 1 1 0 0 0 0 30H
4 0 0 0 1 1 0 0 1 19H
5 0 0 0 1 0 0 1 0 12H
6 0 0 0 0 0 0 1 0 02H
7 0 1 1 1 1 0 0 0 78H
8 0 0 0 0 0 0 0 0 00H
9 0 0 0 1 0 0 0 0 10H
TABLE: .DB $40,$79,$24,$30,$19,$12,$02,$78,$00,$10
.ORG 0
LDI R16,$FF
OUT DDRA,R16
OUT DDRC,R16
LDS R16,$220
ANDI R16,$0F
CALL LOOKUP
OUT PORTC,R17
LDS R16,$220
ANDI R16,$F0
SWAP R16
CALL LOOKUP
OUT PORTA,R17
LOOP: JMP LOOP
21
Solutions
PORTA,
PORTC
output
LOOKUP:
LDI ZL,LOW(TABLE<<1)
LDI ZH,HIGH(TABLE<<1)
ADD ZL,R16
LPM R17,Z
RET
TABLE: .DB $40,$79,$24,$30,$19,$12,$02,$78,$00,$10
Gọi CTC lần 1
Gọi CTC lần 2
Nguyễn Trung Hiếu
22
Analyze
LOOKUP:
LDI ZL,LOW(TABLE<<1)
LDI ZH,HIGH(TABLE<<1)
ADD ZL,R16
LPM R17,Z
RET
▪ Subroutine named LOOKUP
▪ The input parameter is stored in R16
▪ The return parameter is stored in R17
▪ The subroutine can change value in R16 – to keep it safe
LOOKUP:
PUSH R16
….
POP R16
RET Nguyễn Trung Hiếu
Unconditional Jump
Nguyễn Trung Hiếu 23
JMP: (Direct Program Addressing)
IJMP: (Indirect Program Addressing)
RJMP: (Relative Program Addressing)
Conditional Jumps
Nguyễn Trung Hiếu 24
Instruction Abbreviation of Comment
BREQ label Branch if Equal Jump to location label, if Z = 1
BRNE label Branch if Not Equal Jump to location label, if Z = 0
BRCS label
BRLO label
Branch if Carry Set
Branch if Lower
Jump to location label, if C = 1
BRCC label
BRSH label
Branch if Carry Cleared
Branch if Same or Higher
Jump to location label, if C = 0
BRMI label Branch if Minus Jump to location label, if N = 1
BRPL label Branch if Plus Jump to location label, if N = 0
BRGE label Branch if Greater or Equal Jump to location label, if S = 0
BRLT label Branch if Less Than Jump to location label, if S = 1
BRHS label Branch if Half Carry Set Jump to location label, if H = 1
BRHC label Branch if Half Carry Cleared Jump to location label, if H = 0
BRTS label Branch if T flag Set Jump to location label, if T = 1
BRTC label Branch if T flag Cleared Jump to location label, if T = 0
BRIS label Branch if I flag set Jump to location label, if I = 1
BRIC label Branch if I flag cleared Jump to location label, if I = 0
H S V N C
Z
T
I
SREG
Conditional Jumps
Nguyễn Trung Hiếu 25
Instruction Abbreviation of Comment
SBRS Rd,b Skip if Bit in Register is Set Skip next instruction if Rd(b) = 1
SBRC Rd,b Skip if Bit in Register is Cleared Skip next instruction if Rd(b) = 0
SBIS IO_Addr,b Skip if Bit in I/O Register is Set Skip next instruction if IO_Addr(b) = 1
SBIC IO_Addr,b Skip if Bit in I/O Register is Cleared Skip next instruction if IO_Addr(b) = 0
Branch with conditions of bits
Creating a Loop (1)
Viết chương trình thực hiện ghi giá trị $20 vào bộ nhớ SRAM từ ô
nhớ $200 đến $210
.ORG $0
LDI R16,$20
STS $200,R16
STS $201,R16
STS $202,R16
STS $203,R16
…
Nguyễn Trung Hiếu 26
17 lần
Viết chương trình thực hiện ghi giá trị $20
vào bộ nhớ SRAM từ ô nhớ $200 đến
$210
.ORG $0
LDI R16,$20
LDI R17,17
LDI ZH,HIGH($200)
LDI ZL,LOW($200)
Again: ST Z+,R16
DEC R17
BRNE Again
Nguyễn Trung Hiếu 27
Creating a Loop (2)
Ptr  $200
(Ptr)  R16
Ptr  Ptr+1
Loop=0?
N
Y
Loop  17
LoopLoop-1
R16  $20
Z = 0
Z = 1
SIMILAR TO FOR LOOPS
Viết chương trình thực hiện ghi giá trị $20
vào bộ nhớ SRAM từ ô nhớ $200 đến
$210
.ORG $0
LDI R16,$20
LDI ZH,HIGH($200)
LDI ZL,LOW($200)
Again: ST Z+,R16
CPI ZL,$11
BRCS Again
Nguyễn Trung Hiếu 28
Creating a Loop (3)
Ptr  $200
(Ptr)  R16
Ptr  Ptr+1
Ptr<=$210?
Y
N
R16  $20
C = 1
C = 0
ZL - $11
SIMILAR TO DO … WHILE …
Viết chương trình thực hiện ghi giá trị $20
vào bộ nhớ SRAM từ ô nhớ $200 đến
$210
.ORG $0
LDI R16,$20
LDI ZH,HIGH($200)
LDI ZL,LOW($200)
Again: ST Z+,R16
CPI ZL,$11
BRNE Again
Nguyễn Trung Hiếu 29
Creating a Loop (4)
Ptr  $200
(Ptr)  R16
Ptr  Ptr+1
Ptr=$211?
N
Y
R16  $20
Z = 0
Z = 1
ZL - $11
SIMILAR TO DO … WHILE …
Your Turn!
Cho một chuỗi số (có giá trị từ 0 đến 9) được lưu trong ô nhớ
$200 đến $210 ở bộ nhớ SRAM (mỗi ô lưu 1 số).
Viết chương trình tiến hành đọc giá trị từ các ô nhớ ra, sau đó
cộng giá trị đọc được thêm $30 và lưu vào vị trí cũ
Nguyễn Trung Hiếu 30
Solutions (1)
Nguyễn Trung Hiếu 31
(Ptr)  R16
.ORG $0
LDI R17,$30
LDI ZH,HIGH($200)
LDI ZL,LOW($200)
LDI R18,17
Again: LD R16,Z
ADD R16,R17
ST Z+,R16
DEC R18
BRNE Again
Ptr  $200
R16  (Ptr)
Ptr  Ptr+1
Loop=0?
N
Y
R16  R16+R17
R17  $30
Z = 0
Z = 1
Loop  17
Loop  Loop - 1
Solutions (2)
Nguyễn Trung Hiếu 32
(Ptr)  R16
.ORG $0
LDI R17,$30
LDI ZH,HIGH($200)
LDI ZL,LOW($200)
Again: LD R16,Z
ADD R16,R17
ST Z+,R16
CPI ZL,$11
BRNE Again
Ptr  $200
R16  (Ptr)
Ptr  Ptr+1
Ptr=$211?
N
Y
R16  R16+R17
R17  $30
Z = 0
Z = 1
For loop – Application
Nguyễn Trung Hiếu 33
To create a for loop control:
can use BRNE, BRCS (with CPI), …
Example: a 10-time loop
LDI R16,10
LOOP: (begin loop)
…
…
(end loop)
DEC R16
BRNE LOOP
(continue)
Nguyễn Trung Hiếu 34
Use BRNE to create a 1000-time loop?
Ex: 1000 = 250.4 = 200.5 = 100.10 = 10.10.10
LDI R17,4
LOOP1: LDI R16,250
LOOP: (begin loop)
…
…
(end loop)
DEC R16
BRNE LOOP
DEC R17
BRNE LOOP1
(continue)
For loop – Application
Viết chương trình thực hiện xóa thanh ghi R1, sau đó thực
hiện cộng cho R1 số 10 năm lần liên tiếp
Solution:
CLR R1
LDI R16,10
LDI R17,5
AGAIN: ADD R1,R16
DEC R17
BRNE AGAIN
Nguyễn Trung Hiếu 35
For loop – Application
CLR R1 ; 1 MC
LDI R16,10 ; 1 MC
LDI R17,5 ; 1 MC
AGAIN: ADD R1,R16 ; 1 MC
DEC R17 ; 1 MC
BRNE AGAIN ; True – 2 MCs
; False – 1 MC
Nguyễn Trung Hiếu 36
Timing Program
CLR R1 ; 1 MC
LDI R16,10 ; 1 MC
LDI R17,5 ; 1 MC
AGAIN: ADD R1,R16 ; 1 MC
DEC R17 ; 1 MC
BRNE AGAIN ; True – 2 MCs
; False – 1 MC
Nguyễn Trung Hiếu 37
Timing Program
1 time
5 times
4 times T
1 time F
Total time = (1 + 1 + 1).1 + (1 + 1).5 + 2.4 + 1 = 22 MCs
Or = (1 + 1 + 1).1 + (1 + 1 + 2).5 - 1 = 22 MCs
If fMC = 1 MHz → TMC = 1 µs → Total time = 22 µs
Nguyễn Trung Hiếu 38
Blinky Program
PC0  1
Delay
PC0  0
Delay
.ORG 0
SBI DDRC,0
LOOP: SBI PORTC,0
CALL DELAY
CBI PORTC,0
CALL DELAY
RJMP LOOP
DELAY:
LDI R21,100
L1: LDI R20,250
L2: NOP
DEC R20
BRNE L2
DEC R21
BRNE L1
RET
Nguyễn Trung Hiếu 39
Blinky Program
PC0  1
Delay
PC0  0
Delay
→ Waveform? Period? Frequency?
Nguyễn Trung Hiếu 40
Blinky Program
250x(1+1+2) – 1 = 999 MCs
.ORG 0
SBI DDRC,0
LOOP: SBI PORTC,0
CALL DELAY
CBI PORTC,0
CALL DELAY
RJMP LOOP
DELAY:
LDI R21,100
L1: LDI R20,250
L2: NOP ; 1 MC
DEC R20 ; 1 MC
BRNE L2 ; 2/1 MC
DEC R21
BRNE L1
RET
Nguyễn Trung Hiếu 41
Blinky Program
100x(999+1+1+2) – 1
= 100299 MCs
.ORG 0
SBI DDRC,0
LOOP: SBI PORTC,0
CALL DELAY
CBI PORTC,0
CALL DELAY
RJMP LOOP
DELAY:
LDI R21,100
L1: LDI R20,250 ; 1 MC
L2: NOP ; 1 MC
DEC R20 ; 1 MC
BRNE L2 ; 2/1 MC
DEC R21 ; 1 MC
BRNE L1 ; 2/1 MC
RET
Nguyễn Trung Hiếu 42
Blinky Program
100299 + 1 + 4
= 100304 MCs
.ORG 0
SBI DDRC,0
LOOP: SBI PORTC,0
CALL DELAY
CBI PORTC,0
CALL DELAY
RJMP LOOP
DELAY:
LDI R21,100 ; 1 MC
L1: LDI R20,250 ; 1 MC
L2: NOP ; 1 MC
DEC R20 ; 1 MC
BRNE L2 ; 2/1 MC
DEC R21 ; 1 MC
BRNE L1 ; 2/1 MC
RET ; 4 MC
Nguyễn Trung Hiếu 43
Blinky Program
100299 + 1 + 4
= 100304 MCs
.ORG 0
SBI DDRC,0
LOOP: SBI PORTC,0 ; 1 MC
CALL DELAY ; 4 + 100304 MCs
CBI PORTC,0 ; 1 MC
CALL DELAY ; 4 + 100304 MCs
RJMP LOOP ; 2 MC
DELAY:
LDI R21,100 ; 1 MC
L1: LDI R20,250 ; 1 MC
L2: NOP ; 1 MC
DEC R20 ; 1 MC
BRNE L2 ; 2/1 MC
DEC R21 ; 1 MC
BRNE L1 ; 2/1 MC
RET ; 4 MC
Nguyễn Trung Hiếu 44
Blinky Program
.ORG 0
SBI DDRC,0
LOOP: SBI PORTC,0 ; 1 MC
CALL DELAY ; 4 + 100304 MCs
CBI PORTC,0 ; 1 MC
CALL DELAY ; 4 + 100304 MCs
RJMP LOOP ; 2 MC
TH = 4 + 100304 + 1 = 100309 MCs
TL = 4 + 100304 + 2 + 1 = 100311 MCs
If fMC = 1MHz, 1 MC = 1 μs
→ tH = 100,309 MC = 100,309 μs
→tL = 100,311 MC = 100,311 μs
→ T = tH + tL = 200,620 μs
→ f = 1/T = 4.98 Hz
Nguyễn Trung Hiếu 45
Blinky Program
tH tL
T
PC0  1
Delay tH
PC0  0
Delay tL
→ tH = tL ≈ tDELAY ≈ 100 x 250 x 4 MC = 100,000 MC = 100,000 μs
→ T ≈ 200,000 μs
→ f ≈ 5 Hz
Nguyễn Trung Hiếu 46
Blinky Program - Estimating
PC0  1
Delay tH
PC0  0
Delay tL
DELAY:
LDI R21,100
L1: LDI R20,250 ; 1 MC
L2: NOP ; 1 MC
DEC R20 ; 1 MC
BRNE L2 ; 2/1 MC
DEC R21 ; 1 MC
BRNE L1 ; 2/1 MC
RET
4 MCs
Viết chương trình tạo 1 xung vuông có tần số 10 kHz ở
chân PC0. Giả sử rằng tần số fMC = 4MHz
Nguyễn Trung Hiếu 47
10-kHz square wave
Nguyễn Trung Hiếu 48
10-kHz square wave
tH tL
T
PC0  1
Delay tH
PC0  0
Delay tL
tH = tL
Viết chương trình tạo 1 xung vuông có tần số 10 kHz ở
chân PC0. Giả sử rằng tần số fMC = 4MHz
Nguyễn Trung Hiếu 49
10-kHz square wave
Viết chương trình tạo 1 xung vuông có tần số 10 kHz ở
chân PC0. Giả sử rằng tần số fMC = 4MHz
fMC = 4MHz → 1 MC = 0,25 µs
f = 10 kHz → TH = TL = 100 µs = 4.100.0,25 µs = 4.100 MCs
DELAY:
LDI R20,100
L1: NOP ; 1 MC
DEC R20 ; 1 MC
BRNE L1 ; 2/1 MC
RET
Nguyễn Trung Hiếu 50
10-kHz square wave, duty cycle 30%
tH tL
T
PC0  1
Delay tH
PC0  0
Delay tL
Viết chương trình tạo 1 xung vuông có tần số 10 kHz với
chu kỳ nhiệm vụ 30% ở chân PC0.
Giả sử rằng tần số fMC = 4MHz
Chu kỳ nhiệm vụ = D = TH/T
Nguyễn Trung Hiếu 51
10-kHz square wave, duty cycle 30%
PC0  1
Delay tH
PC0  0
Delay tL
Viết chương trình tạo 1 xung vuông có tần số 10 kHz với
chu kỳ nhiệm vụ 30% ở chân PC0.
Giả sử rằng tần số fMC = 4MHz
fMC = 4MHz → 1 MC = 0,25 µs
f = 10 kHz → T = 200 µs
TH = 0,3T = 60 µs = 4.60.0,25 µs = 4.60 MCs
TL = 0,7T = 140 µs
= 4.140.0,25 µs = 4.140 MCs
Nguyễn Trung Hiếu 52
400-kHz square wave
Viết chương trình tạo 1 xung vuông có tần số 400 kHz ở
chân PC0. Giả sử rằng tần số fMC = 4MHz
fMC = 4MHz → 1 MC = 0,25 µs
f = 400 kHz → TH = TL = 2.5 µs = 10 MCs
Nguyễn Trung Hiếu 53
400-kHz square wave, duty cycle 40%
Viết chương trình tạo 1 xung vuông có tần số 400 kHz và
chu kỳ nhiệm vụ 40% ở chân PC0.
Giả sử rằng tần số fMC = 4MHz
CPI R16,$05
BRNE Skip
(Statement 1)
Skip: (Continue)
Nguyễn Trung Hiếu 54
If … else … – Equal/Not Equal (1)
R16 = $05?
N
Y
Statement 1
Z = 0
Z = 1
CP R16,R17
BRNE Skip
(Statement 1)
Skip: (Continue)
R16 = R17?
N
Y
Statement 1
Z = 0
Z = 1
CPI R16,$05
BRNE Not_Eq
(Statement 1)
RJMP Next
Not_Eq: (Statement 2)
Next: (Continue)
Nguyễn Trung Hiếu 55
If … else … – Equal/Not Equal (2)
R16 = $05?
N
Y
Statement 1
Z = 0
Z = 1
Statement 2
CPI R16,$05
BRCC LessThan
(Statement 1)
LessThan: (Continue)
Nguyễn Trung Hiếu 56
If … else … –
Greater Than or Equal/Less Than (1)
R16 ≥ $05?
N
Y
Statement 1
C = 1
C = 0
CPI R16,$05
BRCS GrEqThan
(Statement 1)
GrEqThan: (Continue)
Nguyễn Trung Hiếu 57
If … else … –
Greater Than or Equal/Less Than (2)
R16 < $05?
N
Y
Statement 1
C = 0
C = 1
Viết chương trình kiểm tra nội dung trong thanh ghi R16.
Nếu giá trị 5 ≤ R16 ≤ 10, xuất giá trị bù 1 của R16 ra PORTA.
Ngược lại, xuất giá trị bù 2 của R16 ra PORTA.
58
Nguyễn Trung Hiếu
Ví dụ
.ORG $0
LDI R16,$FF
OUT DDRA,R16
CPI R16,5
BRCC LessThan
CPI R16,11
BRCS GrEqThan
LessThan:
NEQ R16
RJMP NEXT
GrEgThan:
COMP R16
NEXT: OUT PORTA,R16
NOP
CPI R16,$05
BRCC LessThan
(Statement 1)
RJMP Next
LessThan: (Statement 2)
Next: (Continue)
Nguyễn Trung Hiếu 59
If … else … –
Greater Than or Equal/Less Than (3)
R16 ≥ $05?
N
Y
Statement 1
C = 1
C = 0
Statement 2
CPI R16,$05
BRCS GrEqThan
(Statement 1)
RJMP Next
GrEqThan: (Statement 2)
Next: (Continue)
Nguyễn Trung Hiếu 60
If … else … –
Greater Than or Equal/Less Than (4)
R16 < $05?
N
Y
Statement 1
C = 0
C = 1
Statement 2
SBRS R16,0
(Statement 1)
(Continue)
Nguyễn Trung Hiếu 61
If … else … – Bit in Register is set/cleared (1)
R16(0)=1?
Y
N
Statement 1
SBRC R16,0
(Statement 1)
(Continue)
R16(0)=0?
Y
N
Statement 1
Note: Statement 1
- 1 instruction
- If > 1 instruction, call subroutine
SBRS R16,0
RJMP BitSet
(Statement 2)
RJMP Next
BitSet: (Statement 1)
Next: (Continue)
Nguyễn Trung Hiếu 62
If … else … – Bit in Register is set/cleared (2)
R16(0)=1?
Y
N
Statement 1
Statement 2
Cho 20 bytes nằm trong bộ nhớ SRAM bắt đầu tại địa chỉ $250.
Viết chương trình xuất các byte chẵn ra PORTA
63
Nguyễn Trung Hiếu
Bit Testing (1)
64
Nguyễn Trung Hiếu
Your Turn!
Cho 20 bytes nằm trong bộ nhớ SRAM bắt đầu tại địa chỉ $300.
Viết chương trình xuất các byte chẵn ra PORTA và các byte lẽ ra
PORTB
Cho 100 bytes số có dấu nằm trong bộ nhớ SRAM bắt đầu tại địa chỉ
$400. Viết chương trình xuất các số âm ra PORTA và các số dương ra
PORTB
Nguyễn Trung Hiếu
Bit Testing (2)
65
66
References
Nguyễn Trung Hiếu
• Giáo trình Vi xử lý, BMĐT
• Các tài liệu trên Internet không trích dẫn hoặc không ghi tác
giả

More Related Content

Similar to ATmega324-Chap4-Assembly-Programming.pdf

Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.ppt
steffydean
 
ADS Lab 5 Report
ADS Lab 5 ReportADS Lab 5 Report
ADS Lab 5 Report
Riddhi Shah
 
Example MVS Console Interface
Example MVS Console InterfaceExample MVS Console Interface
Example MVS Console Interface
David Young
 
Chapter3 presentation2
Chapter3 presentation2Chapter3 presentation2
Chapter3 presentation2
surendar88
 
Instruction types
Instruction typesInstruction types
Instruction types
JyotiprakashMishra18
 
MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdf
XxUnnathxX
 
Basic programming of 8085
Basic programming of 8085 Basic programming of 8085
Basic programming of 8085
vijaydeepakg
 
Arm Cortex material Arm Cortex material3222886.ppt
Arm Cortex material Arm Cortex material3222886.pptArm Cortex material Arm Cortex material3222886.ppt
Arm Cortex material Arm Cortex material3222886.ppt
Manju Badiger
 
131080111003 mci
131080111003 mci131080111003 mci
131080111003 mci
jokersclown57
 
LCD interfacing
LCD interfacingLCD interfacing
LCD interfacing
Kshitij Wagle
 
Lcd & keypad
Lcd & keypadLcd & keypad
Lcd & keypad
Izwanizam Yahaya
 
Ecet 330 final exam new 2016
Ecet 330 final exam new 2016Ecet 330 final exam new 2016
Ecet 330 final exam new 2016
Olly_March
 
Microprocessor Part 3
Microprocessor    Part  3Microprocessor    Part  3
Microprocessor Part 3
Sajan Agrawal
 
Joel Falcou, Boost.SIMD
Joel Falcou, Boost.SIMDJoel Falcou, Boost.SIMD
Joel Falcou, Boost.SIMD
Sergey Platonov
 
Sayeh basic computer
Sayeh basic computerSayeh basic computer
Sayeh basic computer
Farzan Dehbashi
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
Thirunavakkarasu kannusamy
 
08_lcd.pdf
08_lcd.pdf08_lcd.pdf
08_lcd.pdf
ssusera55490
 
Micro controller(pratheesh)
Micro controller(pratheesh)Micro controller(pratheesh)
Micro controller(pratheesh)
Pratheesh Pala
 
arduinoedit.pptx
arduinoedit.pptxarduinoedit.pptx
arduinoedit.pptx
rajalakshmi769433
 
Assembly Language and microprocessor
Assembly Language and microprocessorAssembly Language and microprocessor
Assembly Language and microprocessor
Khaled Sany
 

Similar to ATmega324-Chap4-Assembly-Programming.pdf (20)

Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.ppt
 
ADS Lab 5 Report
ADS Lab 5 ReportADS Lab 5 Report
ADS Lab 5 Report
 
Example MVS Console Interface
Example MVS Console InterfaceExample MVS Console Interface
Example MVS Console Interface
 
Chapter3 presentation2
Chapter3 presentation2Chapter3 presentation2
Chapter3 presentation2
 
Instruction types
Instruction typesInstruction types
Instruction types
 
MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdf
 
Basic programming of 8085
Basic programming of 8085 Basic programming of 8085
Basic programming of 8085
 
Arm Cortex material Arm Cortex material3222886.ppt
Arm Cortex material Arm Cortex material3222886.pptArm Cortex material Arm Cortex material3222886.ppt
Arm Cortex material Arm Cortex material3222886.ppt
 
131080111003 mci
131080111003 mci131080111003 mci
131080111003 mci
 
LCD interfacing
LCD interfacingLCD interfacing
LCD interfacing
 
Lcd & keypad
Lcd & keypadLcd & keypad
Lcd & keypad
 
Ecet 330 final exam new 2016
Ecet 330 final exam new 2016Ecet 330 final exam new 2016
Ecet 330 final exam new 2016
 
Microprocessor Part 3
Microprocessor    Part  3Microprocessor    Part  3
Microprocessor Part 3
 
Joel Falcou, Boost.SIMD
Joel Falcou, Boost.SIMDJoel Falcou, Boost.SIMD
Joel Falcou, Boost.SIMD
 
Sayeh basic computer
Sayeh basic computerSayeh basic computer
Sayeh basic computer
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
 
08_lcd.pdf
08_lcd.pdf08_lcd.pdf
08_lcd.pdf
 
Micro controller(pratheesh)
Micro controller(pratheesh)Micro controller(pratheesh)
Micro controller(pratheesh)
 
arduinoedit.pptx
arduinoedit.pptxarduinoedit.pptx
arduinoedit.pptx
 
Assembly Language and microprocessor
Assembly Language and microprocessorAssembly Language and microprocessor
Assembly Language and microprocessor
 

Recently uploaded

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 

Recently uploaded (20)

Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 

ATmega324-Chap4-Assembly-Programming.pdf

  • 1. Chapter 4 Assembly Language How to write a program The ATmega324 Microcontroller Nguyễn Trung Hiếu 1 Nguyễn Trung Hiếu Ref: Giáo trình Vi Xử Lý, BMĐT
  • 2. Instruction Format [LABEL] Mnemonic [Op1][Op2] [;comment] • […]: optional • Op: operand • Example: ➢LOOP: INC R1 ; R1 R1 + 1 ➢ LDI R16,20 ; R16 20 ➢ BREQ LOOP ; LOOP is name of label, Operand1 Nguyễn Trung Hiếu 2
  • 3. Instruction Format [LABEL] Mnemonic [Op1][Op2] [;comment] • Operand can contain operators • Example: ➢LDI R20,$0F&$75 ; R20  $05 ➢LDI R22,255/7 ; R22  36 ➢LDI R24,’A’<‘Z’ ; R24  1 ➢LDI R26,HIGH($AB04) ; R26  $AB Nguyễn Trung Hiếu 3
  • 4. Assembler Directives • Instructions for the compiler, not assembly • Control the assembler, don't create any code • The leading dot must be in column 1 of the line • Segment: ➢ Everywhere ➢ Header ➢ Code ➢ EEPROM ➢ SRAM Nguyễn Trung Hiếu 4
  • 5. Assembler Directives: Everywhere Nguyễn Trung Hiếu 5 .ORG Defines the address within the respective segment, where the assembler assembles to syntax: .ORG 0x0000 .INCLUDE Includes a file and assembles its content, just like its content would be part of the calling file syntax: .INCLUDE <M324PDEF.inc> .EXIT End of the assembler-source code (stops the assembling process) .LIST Switches the listing to the .LST-file on (the assembled code will be listed in a readable text file .LST) .NOLIST Switches the output to the .LST-file off, suppresses listing
  • 6. Assembler Directives: Header Nguyễn Trung Hiếu 6 .DEF Defines a synonym for a register syntax: .DEF MyReg = R16 .SET Defines a symbol and sets its value (later changes of this value remain possible) syntax: .SET test = 1234567 .EQU Fixes the value of a symbol (later redefinition is not possible) syntax: .EQU test = 1234567
  • 7. Assembler Directives: Header Nguyễn Trung Hiếu 7 .ORG 0 .EQU COUNTER = $A0 .EQU MYADDR1=$08 .SET MYADDR2 = 0x18 LDI R26,MYADDR1 ;R26=$08 LDI R27,MYADDR2 ;R27=$18 LDI R16, COUNTER ;R16 = 0xA0 .SET MYADDR2=0X0B ;redefine MYADDR2=$0B OUT MYADDR1,R16 ;MYADDR1≡PORTC=0xA0 OUT MYADDR2,R16 ;MYADDR2≡PORTD=0xA0
  • 8. Assembler Directives: Code Nguyễn Trung Hiếu 8 .CSEG Start of the code segment (all that follows is assembled to the code segment and will go to the program space) .DB Inserts one or more constant bytes in the code segment The number of inserted bytes must be even, otherwise an additional zero byte will be inserted by the assembler syntax: .DB 1,’a’,”bc” .DW Insert binary words in the code segment, not used string syntax: .DW 5,’b’ .LISTMAC Macros will be listed in the .LST-file. (Default is that macros are not listed) .MACRO Beginning of a macro (no code will be produced, call of the macro later produces code) syntax: .MACRO macroname parameters .ENDMACRO End of the macro
  • 9. Assembler Directives: Code Nguyễn Trung Hiếu 9 ;Define Macro LOADIO .MACRO LOADIO LDI R20,@1 OUT @0,R20 .ENDMACRO ;Call Macro LOADIO LOADIO PORTA, $20 ;PORTA=$20 .EQU VAL_1 = $FF LOADIO DDRC, VAL_1 ;DDRC=$FF LOADIO SPL, $55 ;SPL=$55 @0 and @1 is input parameters @ can up to 9
  • 10. Assembler Directives: EEPROM Nguyễn Trung Hiếu 10 .ESEG Assemble to the EEPROM-segment (the code produced will go to the EEPROM section, the code produces an .EEP-file) .DB Inserts one or more constant bytes in the EEPROM segment (could be numbers from 0..255, an ASCII-character like 'c', a string like “abcde” or a combination like 1,2,3,'abc’) syntax: .DB 1,’a’,”abcde” .DW Inserts a binary word to the EEPROM segment (the lower byte goes to the next address, the higher byte follows on the incremented address) syntax: .DW 15,’d’
  • 11. Assembler Directives: SRAM Nguyễn Trung Hiếu 11 .DSEG Assemble to the data segment (here only BYTE directives and labels are valid, during assembly only the labels are used) .BYTE Reserves one or more bytes space in the data segment (only used to produce correct labels, does not insert any values!)
  • 12. Assembler Directives: SRAM Nguyễn Trung Hiếu 12 .DSEG ; declare DATA segment .ORG 0x120 ; SRAM, begin address at $120 VAR1: .BYTE 1 ; use 1 byte in SRAM for VAR1 at $120 .CSEG ; declare CODE segment .ORG 0x10 ; FLASH, begin address = PC = $10 MOV R0,R1 ; ….. ; ….. ; .ESEG ; declare EEPROM segment VAR2: .BYTE 5 ; use 5 byte in EEPROM for VAR2
  • 13. Instruction Types • Data transfer • Arithmetic • Logical • Boolean variable • Program branching • MCU control Nguyễn Trung Hiếu 13 Used for creating loops, branching and call subroutine
  • 14. Program Branching Types Nguyễn Trung Hiếu 14 • Call, Return - Call subroutine: save information about where it currently is executing and then jumps to the subroutine - Return: when the subroutine is finished, it returns to where the call was made • Conditional Jump (Branch) - If the Branch test is successful, the program jumps to the location specified (no save information) - If the Branch test is unsuccessful, the next line of code is executed • Unconditional Jump (Jump) - Always jump to the location specified (no save information)
  • 15. Program Branching Types Nguyễn Trung Hiếu 15 Call, return Conditional and Unconditional Jump Subroutine Loops, branching
  • 16. Call and Return Nguyễn Trung Hiếu 16 MAIN: ... (1) (RCALL,ICALL)CALL SUBLABEL (2) ... (3) ... SUBLABEL: ... (4) ... RET (5) Structure:
  • 17. Subroutine Nguyễn Trung Hiếu 17 ▪ Divide and Conquer – Allow you to focus on one small “chunk” of the problem at a time ▪ Code Organization – Gives the code organization and structure. A small step into the world of object-oriented programming ▪ Hierarchical Design – Moves information about the program at the appropriate level of detail ▪ Code Readability – Allows others to read and understand the program in digestible “bites” instead of all at once ▪ Encapsulation – Insulates the rest of the program from changes made within a procedure ▪ Team Development – Helps multiple programmers to work on the program in parallel; a first step to configuration control
  • 18. Cho một số BCD nén nằm ở địa chỉ $220 của bộ nhớ SRAM. Viết chương trình đọc số BCD đó và xuất giá trị ra 2 LED 7 đoạn kết nối với PORTA (nibble cao) và PORTC (nibble thấp) của vi xử lý. Nguyễn Trung Hiếu 18 Your Turn!
  • 19. LDI R16,ENTRY_NUMBER (LDI R17,$0) LDI ZH,HIGH(TABLE<<1) LDI ZL,LOW(TABLE<<1) ADD ZL,R16 (ADC ZH,R17) LPM R17,Z TABLE: .DB data1, data2, data3, … Nguyễn Trung Hiếu 19 (Remember) Look-up Table DATA: 8 bits (byte) Program Memory Address High Byte Low Byte data2 data1 data4 data3 … … … … TABLE’S ADDRESS If ENTRY_NUMBER = 0, Z POINT TO
  • 20. Nguyễn Trung Hiếu 20 Create the Table D7 D6 g D5 f D4 e D3 d D2 c D1 b D0 a 0 0 1 0 0 0 0 0 0 40H 1 0 1 1 1 1 0 0 1 79H 2 0 0 1 0 0 1 0 0 24H 3 0 0 1 1 0 0 0 0 30H 4 0 0 0 1 1 0 0 1 19H 5 0 0 0 1 0 0 1 0 12H 6 0 0 0 0 0 0 1 0 02H 7 0 1 1 1 1 0 0 0 78H 8 0 0 0 0 0 0 0 0 00H 9 0 0 0 1 0 0 0 0 10H TABLE: .DB $40,$79,$24,$30,$19,$12,$02,$78,$00,$10
  • 21. .ORG 0 LDI R16,$FF OUT DDRA,R16 OUT DDRC,R16 LDS R16,$220 ANDI R16,$0F CALL LOOKUP OUT PORTC,R17 LDS R16,$220 ANDI R16,$F0 SWAP R16 CALL LOOKUP OUT PORTA,R17 LOOP: JMP LOOP 21 Solutions PORTA, PORTC output LOOKUP: LDI ZL,LOW(TABLE<<1) LDI ZH,HIGH(TABLE<<1) ADD ZL,R16 LPM R17,Z RET TABLE: .DB $40,$79,$24,$30,$19,$12,$02,$78,$00,$10 Gọi CTC lần 1 Gọi CTC lần 2 Nguyễn Trung Hiếu
  • 22. 22 Analyze LOOKUP: LDI ZL,LOW(TABLE<<1) LDI ZH,HIGH(TABLE<<1) ADD ZL,R16 LPM R17,Z RET ▪ Subroutine named LOOKUP ▪ The input parameter is stored in R16 ▪ The return parameter is stored in R17 ▪ The subroutine can change value in R16 – to keep it safe LOOKUP: PUSH R16 …. POP R16 RET Nguyễn Trung Hiếu
  • 23. Unconditional Jump Nguyễn Trung Hiếu 23 JMP: (Direct Program Addressing) IJMP: (Indirect Program Addressing) RJMP: (Relative Program Addressing)
  • 24. Conditional Jumps Nguyễn Trung Hiếu 24 Instruction Abbreviation of Comment BREQ label Branch if Equal Jump to location label, if Z = 1 BRNE label Branch if Not Equal Jump to location label, if Z = 0 BRCS label BRLO label Branch if Carry Set Branch if Lower Jump to location label, if C = 1 BRCC label BRSH label Branch if Carry Cleared Branch if Same or Higher Jump to location label, if C = 0 BRMI label Branch if Minus Jump to location label, if N = 1 BRPL label Branch if Plus Jump to location label, if N = 0 BRGE label Branch if Greater or Equal Jump to location label, if S = 0 BRLT label Branch if Less Than Jump to location label, if S = 1 BRHS label Branch if Half Carry Set Jump to location label, if H = 1 BRHC label Branch if Half Carry Cleared Jump to location label, if H = 0 BRTS label Branch if T flag Set Jump to location label, if T = 1 BRTC label Branch if T flag Cleared Jump to location label, if T = 0 BRIS label Branch if I flag set Jump to location label, if I = 1 BRIC label Branch if I flag cleared Jump to location label, if I = 0 H S V N C Z T I SREG
  • 25. Conditional Jumps Nguyễn Trung Hiếu 25 Instruction Abbreviation of Comment SBRS Rd,b Skip if Bit in Register is Set Skip next instruction if Rd(b) = 1 SBRC Rd,b Skip if Bit in Register is Cleared Skip next instruction if Rd(b) = 0 SBIS IO_Addr,b Skip if Bit in I/O Register is Set Skip next instruction if IO_Addr(b) = 1 SBIC IO_Addr,b Skip if Bit in I/O Register is Cleared Skip next instruction if IO_Addr(b) = 0 Branch with conditions of bits
  • 26. Creating a Loop (1) Viết chương trình thực hiện ghi giá trị $20 vào bộ nhớ SRAM từ ô nhớ $200 đến $210 .ORG $0 LDI R16,$20 STS $200,R16 STS $201,R16 STS $202,R16 STS $203,R16 … Nguyễn Trung Hiếu 26 17 lần
  • 27. Viết chương trình thực hiện ghi giá trị $20 vào bộ nhớ SRAM từ ô nhớ $200 đến $210 .ORG $0 LDI R16,$20 LDI R17,17 LDI ZH,HIGH($200) LDI ZL,LOW($200) Again: ST Z+,R16 DEC R17 BRNE Again Nguyễn Trung Hiếu 27 Creating a Loop (2) Ptr  $200 (Ptr)  R16 Ptr  Ptr+1 Loop=0? N Y Loop  17 LoopLoop-1 R16  $20 Z = 0 Z = 1 SIMILAR TO FOR LOOPS
  • 28. Viết chương trình thực hiện ghi giá trị $20 vào bộ nhớ SRAM từ ô nhớ $200 đến $210 .ORG $0 LDI R16,$20 LDI ZH,HIGH($200) LDI ZL,LOW($200) Again: ST Z+,R16 CPI ZL,$11 BRCS Again Nguyễn Trung Hiếu 28 Creating a Loop (3) Ptr  $200 (Ptr)  R16 Ptr  Ptr+1 Ptr<=$210? Y N R16  $20 C = 1 C = 0 ZL - $11 SIMILAR TO DO … WHILE …
  • 29. Viết chương trình thực hiện ghi giá trị $20 vào bộ nhớ SRAM từ ô nhớ $200 đến $210 .ORG $0 LDI R16,$20 LDI ZH,HIGH($200) LDI ZL,LOW($200) Again: ST Z+,R16 CPI ZL,$11 BRNE Again Nguyễn Trung Hiếu 29 Creating a Loop (4) Ptr  $200 (Ptr)  R16 Ptr  Ptr+1 Ptr=$211? N Y R16  $20 Z = 0 Z = 1 ZL - $11 SIMILAR TO DO … WHILE …
  • 30. Your Turn! Cho một chuỗi số (có giá trị từ 0 đến 9) được lưu trong ô nhớ $200 đến $210 ở bộ nhớ SRAM (mỗi ô lưu 1 số). Viết chương trình tiến hành đọc giá trị từ các ô nhớ ra, sau đó cộng giá trị đọc được thêm $30 và lưu vào vị trí cũ Nguyễn Trung Hiếu 30
  • 31. Solutions (1) Nguyễn Trung Hiếu 31 (Ptr)  R16 .ORG $0 LDI R17,$30 LDI ZH,HIGH($200) LDI ZL,LOW($200) LDI R18,17 Again: LD R16,Z ADD R16,R17 ST Z+,R16 DEC R18 BRNE Again Ptr  $200 R16  (Ptr) Ptr  Ptr+1 Loop=0? N Y R16  R16+R17 R17  $30 Z = 0 Z = 1 Loop  17 Loop  Loop - 1
  • 32. Solutions (2) Nguyễn Trung Hiếu 32 (Ptr)  R16 .ORG $0 LDI R17,$30 LDI ZH,HIGH($200) LDI ZL,LOW($200) Again: LD R16,Z ADD R16,R17 ST Z+,R16 CPI ZL,$11 BRNE Again Ptr  $200 R16  (Ptr) Ptr  Ptr+1 Ptr=$211? N Y R16  R16+R17 R17  $30 Z = 0 Z = 1
  • 33. For loop – Application Nguyễn Trung Hiếu 33 To create a for loop control: can use BRNE, BRCS (with CPI), … Example: a 10-time loop LDI R16,10 LOOP: (begin loop) … … (end loop) DEC R16 BRNE LOOP (continue)
  • 34. Nguyễn Trung Hiếu 34 Use BRNE to create a 1000-time loop? Ex: 1000 = 250.4 = 200.5 = 100.10 = 10.10.10 LDI R17,4 LOOP1: LDI R16,250 LOOP: (begin loop) … … (end loop) DEC R16 BRNE LOOP DEC R17 BRNE LOOP1 (continue) For loop – Application
  • 35. Viết chương trình thực hiện xóa thanh ghi R1, sau đó thực hiện cộng cho R1 số 10 năm lần liên tiếp Solution: CLR R1 LDI R16,10 LDI R17,5 AGAIN: ADD R1,R16 DEC R17 BRNE AGAIN Nguyễn Trung Hiếu 35 For loop – Application
  • 36. CLR R1 ; 1 MC LDI R16,10 ; 1 MC LDI R17,5 ; 1 MC AGAIN: ADD R1,R16 ; 1 MC DEC R17 ; 1 MC BRNE AGAIN ; True – 2 MCs ; False – 1 MC Nguyễn Trung Hiếu 36 Timing Program
  • 37. CLR R1 ; 1 MC LDI R16,10 ; 1 MC LDI R17,5 ; 1 MC AGAIN: ADD R1,R16 ; 1 MC DEC R17 ; 1 MC BRNE AGAIN ; True – 2 MCs ; False – 1 MC Nguyễn Trung Hiếu 37 Timing Program 1 time 5 times 4 times T 1 time F Total time = (1 + 1 + 1).1 + (1 + 1).5 + 2.4 + 1 = 22 MCs Or = (1 + 1 + 1).1 + (1 + 1 + 2).5 - 1 = 22 MCs If fMC = 1 MHz → TMC = 1 µs → Total time = 22 µs
  • 38. Nguyễn Trung Hiếu 38 Blinky Program PC0  1 Delay PC0  0 Delay
  • 39. .ORG 0 SBI DDRC,0 LOOP: SBI PORTC,0 CALL DELAY CBI PORTC,0 CALL DELAY RJMP LOOP DELAY: LDI R21,100 L1: LDI R20,250 L2: NOP DEC R20 BRNE L2 DEC R21 BRNE L1 RET Nguyễn Trung Hiếu 39 Blinky Program PC0  1 Delay PC0  0 Delay → Waveform? Period? Frequency?
  • 40. Nguyễn Trung Hiếu 40 Blinky Program 250x(1+1+2) – 1 = 999 MCs .ORG 0 SBI DDRC,0 LOOP: SBI PORTC,0 CALL DELAY CBI PORTC,0 CALL DELAY RJMP LOOP DELAY: LDI R21,100 L1: LDI R20,250 L2: NOP ; 1 MC DEC R20 ; 1 MC BRNE L2 ; 2/1 MC DEC R21 BRNE L1 RET
  • 41. Nguyễn Trung Hiếu 41 Blinky Program 100x(999+1+1+2) – 1 = 100299 MCs .ORG 0 SBI DDRC,0 LOOP: SBI PORTC,0 CALL DELAY CBI PORTC,0 CALL DELAY RJMP LOOP DELAY: LDI R21,100 L1: LDI R20,250 ; 1 MC L2: NOP ; 1 MC DEC R20 ; 1 MC BRNE L2 ; 2/1 MC DEC R21 ; 1 MC BRNE L1 ; 2/1 MC RET
  • 42. Nguyễn Trung Hiếu 42 Blinky Program 100299 + 1 + 4 = 100304 MCs .ORG 0 SBI DDRC,0 LOOP: SBI PORTC,0 CALL DELAY CBI PORTC,0 CALL DELAY RJMP LOOP DELAY: LDI R21,100 ; 1 MC L1: LDI R20,250 ; 1 MC L2: NOP ; 1 MC DEC R20 ; 1 MC BRNE L2 ; 2/1 MC DEC R21 ; 1 MC BRNE L1 ; 2/1 MC RET ; 4 MC
  • 43. Nguyễn Trung Hiếu 43 Blinky Program 100299 + 1 + 4 = 100304 MCs .ORG 0 SBI DDRC,0 LOOP: SBI PORTC,0 ; 1 MC CALL DELAY ; 4 + 100304 MCs CBI PORTC,0 ; 1 MC CALL DELAY ; 4 + 100304 MCs RJMP LOOP ; 2 MC DELAY: LDI R21,100 ; 1 MC L1: LDI R20,250 ; 1 MC L2: NOP ; 1 MC DEC R20 ; 1 MC BRNE L2 ; 2/1 MC DEC R21 ; 1 MC BRNE L1 ; 2/1 MC RET ; 4 MC
  • 44. Nguyễn Trung Hiếu 44 Blinky Program .ORG 0 SBI DDRC,0 LOOP: SBI PORTC,0 ; 1 MC CALL DELAY ; 4 + 100304 MCs CBI PORTC,0 ; 1 MC CALL DELAY ; 4 + 100304 MCs RJMP LOOP ; 2 MC TH = 4 + 100304 + 1 = 100309 MCs TL = 4 + 100304 + 2 + 1 = 100311 MCs
  • 45. If fMC = 1MHz, 1 MC = 1 μs → tH = 100,309 MC = 100,309 μs →tL = 100,311 MC = 100,311 μs → T = tH + tL = 200,620 μs → f = 1/T = 4.98 Hz Nguyễn Trung Hiếu 45 Blinky Program tH tL T PC0  1 Delay tH PC0  0 Delay tL
  • 46. → tH = tL ≈ tDELAY ≈ 100 x 250 x 4 MC = 100,000 MC = 100,000 μs → T ≈ 200,000 μs → f ≈ 5 Hz Nguyễn Trung Hiếu 46 Blinky Program - Estimating PC0  1 Delay tH PC0  0 Delay tL DELAY: LDI R21,100 L1: LDI R20,250 ; 1 MC L2: NOP ; 1 MC DEC R20 ; 1 MC BRNE L2 ; 2/1 MC DEC R21 ; 1 MC BRNE L1 ; 2/1 MC RET 4 MCs
  • 47. Viết chương trình tạo 1 xung vuông có tần số 10 kHz ở chân PC0. Giả sử rằng tần số fMC = 4MHz Nguyễn Trung Hiếu 47 10-kHz square wave
  • 48. Nguyễn Trung Hiếu 48 10-kHz square wave tH tL T PC0  1 Delay tH PC0  0 Delay tL tH = tL Viết chương trình tạo 1 xung vuông có tần số 10 kHz ở chân PC0. Giả sử rằng tần số fMC = 4MHz
  • 49. Nguyễn Trung Hiếu 49 10-kHz square wave Viết chương trình tạo 1 xung vuông có tần số 10 kHz ở chân PC0. Giả sử rằng tần số fMC = 4MHz fMC = 4MHz → 1 MC = 0,25 µs f = 10 kHz → TH = TL = 100 µs = 4.100.0,25 µs = 4.100 MCs DELAY: LDI R20,100 L1: NOP ; 1 MC DEC R20 ; 1 MC BRNE L1 ; 2/1 MC RET
  • 50. Nguyễn Trung Hiếu 50 10-kHz square wave, duty cycle 30% tH tL T PC0  1 Delay tH PC0  0 Delay tL Viết chương trình tạo 1 xung vuông có tần số 10 kHz với chu kỳ nhiệm vụ 30% ở chân PC0. Giả sử rằng tần số fMC = 4MHz Chu kỳ nhiệm vụ = D = TH/T
  • 51. Nguyễn Trung Hiếu 51 10-kHz square wave, duty cycle 30% PC0  1 Delay tH PC0  0 Delay tL Viết chương trình tạo 1 xung vuông có tần số 10 kHz với chu kỳ nhiệm vụ 30% ở chân PC0. Giả sử rằng tần số fMC = 4MHz fMC = 4MHz → 1 MC = 0,25 µs f = 10 kHz → T = 200 µs TH = 0,3T = 60 µs = 4.60.0,25 µs = 4.60 MCs TL = 0,7T = 140 µs = 4.140.0,25 µs = 4.140 MCs
  • 52. Nguyễn Trung Hiếu 52 400-kHz square wave Viết chương trình tạo 1 xung vuông có tần số 400 kHz ở chân PC0. Giả sử rằng tần số fMC = 4MHz fMC = 4MHz → 1 MC = 0,25 µs f = 400 kHz → TH = TL = 2.5 µs = 10 MCs
  • 53. Nguyễn Trung Hiếu 53 400-kHz square wave, duty cycle 40% Viết chương trình tạo 1 xung vuông có tần số 400 kHz và chu kỳ nhiệm vụ 40% ở chân PC0. Giả sử rằng tần số fMC = 4MHz
  • 54. CPI R16,$05 BRNE Skip (Statement 1) Skip: (Continue) Nguyễn Trung Hiếu 54 If … else … – Equal/Not Equal (1) R16 = $05? N Y Statement 1 Z = 0 Z = 1 CP R16,R17 BRNE Skip (Statement 1) Skip: (Continue) R16 = R17? N Y Statement 1 Z = 0 Z = 1
  • 55. CPI R16,$05 BRNE Not_Eq (Statement 1) RJMP Next Not_Eq: (Statement 2) Next: (Continue) Nguyễn Trung Hiếu 55 If … else … – Equal/Not Equal (2) R16 = $05? N Y Statement 1 Z = 0 Z = 1 Statement 2
  • 56. CPI R16,$05 BRCC LessThan (Statement 1) LessThan: (Continue) Nguyễn Trung Hiếu 56 If … else … – Greater Than or Equal/Less Than (1) R16 ≥ $05? N Y Statement 1 C = 1 C = 0
  • 57. CPI R16,$05 BRCS GrEqThan (Statement 1) GrEqThan: (Continue) Nguyễn Trung Hiếu 57 If … else … – Greater Than or Equal/Less Than (2) R16 < $05? N Y Statement 1 C = 0 C = 1
  • 58. Viết chương trình kiểm tra nội dung trong thanh ghi R16. Nếu giá trị 5 ≤ R16 ≤ 10, xuất giá trị bù 1 của R16 ra PORTA. Ngược lại, xuất giá trị bù 2 của R16 ra PORTA. 58 Nguyễn Trung Hiếu Ví dụ .ORG $0 LDI R16,$FF OUT DDRA,R16 CPI R16,5 BRCC LessThan CPI R16,11 BRCS GrEqThan LessThan: NEQ R16 RJMP NEXT GrEgThan: COMP R16 NEXT: OUT PORTA,R16 NOP
  • 59. CPI R16,$05 BRCC LessThan (Statement 1) RJMP Next LessThan: (Statement 2) Next: (Continue) Nguyễn Trung Hiếu 59 If … else … – Greater Than or Equal/Less Than (3) R16 ≥ $05? N Y Statement 1 C = 1 C = 0 Statement 2
  • 60. CPI R16,$05 BRCS GrEqThan (Statement 1) RJMP Next GrEqThan: (Statement 2) Next: (Continue) Nguyễn Trung Hiếu 60 If … else … – Greater Than or Equal/Less Than (4) R16 < $05? N Y Statement 1 C = 0 C = 1 Statement 2
  • 61. SBRS R16,0 (Statement 1) (Continue) Nguyễn Trung Hiếu 61 If … else … – Bit in Register is set/cleared (1) R16(0)=1? Y N Statement 1 SBRC R16,0 (Statement 1) (Continue) R16(0)=0? Y N Statement 1 Note: Statement 1 - 1 instruction - If > 1 instruction, call subroutine
  • 62. SBRS R16,0 RJMP BitSet (Statement 2) RJMP Next BitSet: (Statement 1) Next: (Continue) Nguyễn Trung Hiếu 62 If … else … – Bit in Register is set/cleared (2) R16(0)=1? Y N Statement 1 Statement 2
  • 63. Cho 20 bytes nằm trong bộ nhớ SRAM bắt đầu tại địa chỉ $250. Viết chương trình xuất các byte chẵn ra PORTA 63 Nguyễn Trung Hiếu Bit Testing (1)
  • 64. 64 Nguyễn Trung Hiếu Your Turn! Cho 20 bytes nằm trong bộ nhớ SRAM bắt đầu tại địa chỉ $300. Viết chương trình xuất các byte chẵn ra PORTA và các byte lẽ ra PORTB
  • 65. Cho 100 bytes số có dấu nằm trong bộ nhớ SRAM bắt đầu tại địa chỉ $400. Viết chương trình xuất các số âm ra PORTA và các số dương ra PORTB Nguyễn Trung Hiếu Bit Testing (2) 65
  • 66. 66 References Nguyễn Trung Hiếu • Giáo trình Vi xử lý, BMĐT • Các tài liệu trên Internet không trích dẫn hoặc không ghi tác giả