0% found this document useful (0 votes)
16 views

ATmega Chap4 Assembly Programming

Uploaded by

vyhoa118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

ATmega Chap4 Assembly Programming

Uploaded by

vyhoa118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

The ATmega324 Microcontroller

Chapter 4
Assembly Language
How to write a program

Nguyễn Trung Hiếu


Ref: Giáo trình Vi Xử Lý, BMĐT
Nguyễn Trung Hiếu 1
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
Defines the address within the respective segment, where
.ORG the assembler assembles to
syntax: .ORG 0x0000
Includes a file and assembles its content, just like its content
.INCLUDE would be part of the calling file
syntax: .INCLUDE <M324PDEF.inc>
End of the assembler-source code
.EXIT
(stops the assembling process)
Switches the listing to the .LST-file on (the assembled code
.LIST
will be listed in a readable text file .LST)
.NOLIST Switches the output to the .LST-file off, suppresses listing

Nguyễn Trung Hiếu 5


Assembler Directives: Header
Defines a synonym for a register
.DEF
syntax: .DEF MyReg = R16
Defines a symbol and sets its value (later changes of this
.SET value remain possible)
syntax: .SET test = 1234567
Fixes the value of a symbol (later redefinition is not possible)
.EQU
syntax: .EQU test = 1234567

Nguyễn Trung Hiếu 6


Assembler Directives: Header

.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

Nguyễn Trung Hiếu 7


Assembler Directives: Code
Start of the code segment (all that follows is assembled to
.CSEG
the code segment and will go to the program space)
Inserts one or more constant bytes in the code segment
The number of inserted bytes must be even, otherwise an
.DB
additional zero byte will be inserted by the assembler
syntax: .DB 1,’a’,”bc”
Insert binary words in the code segment, not used string
.DW
syntax: .DW 5,’b’
Macros will be listed in the .LST-file.
.LISTMAC
(Default is that macros are not listed)
Beginning of a macro (no code will be produced, call of the
.MACRO macro later produces code)
syntax: .MACRO macroname parameters
.ENDMACRO End of the macro Nguyễn Trung Hiếu 8
Assembler Directives: Code
;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
Nguyễn Trung Hiếu 9
Assembler Directives: EEPROM

Assemble to the EEPROM-segment (the code produced will go


.ESEG
to the EEPROM section, the code produces an .EEP-file)
Inserts one or more constant bytes in the EEPROM segment
(could be numbers from 0..255, an ASCII-character like 'c', a
.DB
string like “abcde” or a combination like 1,2,3,'abc’)
syntax: .DB 1,’a’,”abcde”
Inserts a binary word to the EEPROM segment (the lower byte
goes to the next address, the higher byte follows on the
.DW
incremented address)
syntax: .DW 15,’d’

Nguyễn Trung Hiếu 10


Assembler Directives: SRAM

Assemble to the data segment (here only BYTE directives and


.DSEG
labels are valid, during assembly only the labels are used)

Reserves one or more bytes space in the data segment (only


.BYTE
used to produce correct labels, does not insert any values!)

Nguyễn Trung Hiếu 11


Assembler Directives: SRAM
.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

Nguyễn Trung Hiếu 12


Instruction Types
• Data transfer

• Arithmetic

• Logical

• Boolean variable

• Program branching Used for creating loops,


branching and call subroutine
• MCU control

Nguyễn Trung Hiếu 13


Program Branching Types
• 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)
Nguyễn Trung Hiếu 14
Program Branching Types

Conditional and
Call, return
Unconditional Jump

Subroutine Loops, branching

Nguyễn Trung Hiếu 15


Call and Return
Structure:
MAIN: ... (1)
(RCALL,ICALL)CALL SUBLABEL (2)
... (3)
...
SUBLABEL: ... (4)
...
RET (5)

Nguyễn Trung Hiếu 16


Subroutine
▪ 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
Nguyễn Trung Hiếu 17
Your Turn!
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


(Remember) Look-up Table DATA: 8 bits (byte)
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, …
If ENTRY_NUMBER = 0,
Address High Byte Low Byte Z POINT TO
TABLE’S ADDRESS data2 data1
data4 data3
… …
Program Memory …
Nguyễn Trung Hiếu … 19
Create the Table
D7 D6 D5 D4 D3 D2 D1 D0
g f e d c b 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

Nguyễn Trung Hiếu 20


Solutions
.ORG 0
PORTA, LDI R16,$FF LOOKUP:
PORTC OUT DDRA,R16 LDI ZL,LOW(TABLE<<1)
output OUT DDRC,R16 LDI ZH,HIGH(TABLE<<1)
ADD ZL,R16
LDS R16,$220 LPM R17,Z
ANDI R16,$0F RET
CALL LOOKUP Gọi CTC lần 1
OUT PORTC,R17
LDS R16,$220
ANDI R16,$F0
SWAP R16 Gọi CTC lần 2
CALL LOOKUP
OUT PORTA,R17

LOOP: JMP LOOP


Nguyễn Trung Hiếu 21
TABLE: .DB $40,$79,$24,$30,$19,$12,$02,$78,$00,$10
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 22
Unconditional Jump

JMP: (Direct Program Addressing)

IJMP: (Indirect Program Addressing)

RJMP: (Relative Program Addressing)

Nguyễn Trung Hiếu 23


Conditional Jumps SREG I T H S V N Z C

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 Branch if Carry Set Jump to location label, if C = 1
BRLO label Branch if Lower
BRCC label Branch if Carry Cleared Jump to location label, if C = 0
BRSH label Branch if Same or Higher
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
Nguyễn Trung Hiếu 24
Conditional Jumps
Branch with conditions of bits

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

Nguyễn Trung Hiếu 25


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 17 lần
STS $203,R16

Nguyễn Trung Hiếu 26


R16  $20
Creating a Loop (2)
Viết chương trình thực hiện ghi giá trị $20 Loop  17
vào bộ nhớ SRAM từ ô nhớ $200 đến
$210 Ptr  $200

.ORG $0
(Ptr)  R16
LDI R16,$20
LDI R17,17
LDI ZH,HIGH($200) Ptr  Ptr+1
LDI ZL,LOW($200)
Again: ST Z+,R16 LoopLoop-1
DEC R17
BRNE Again N
Loop=0?
Z=0
SIMILAR TO FOR LOOPS Z=1
Y
Nguyễn Trung Hiếu 27
Creating a Loop (3)
Viết chương trình thực hiện ghi giá trị $20
vào bộ nhớ SRAM từ ô nhớ $200 đến
$210 R16  $20

.ORG $0 Ptr  $200


LDI R16,$20
LDI ZH,HIGH($200)
(Ptr)  R16
LDI ZL,LOW($200)
Again: ST Z+,R16
Ptr  Ptr+1
CPI ZL,$11
BRCS Again
Y
ZL - $11 Ptr<=$210?
C=1
SIMILAR TO DO … WHILE … C=0
N
Nguyễn Trung Hiếu 28
Creating a Loop (4)
Viết chương trình thực hiện ghi giá trị $20
vào bộ nhớ SRAM từ ô nhớ $200 đến
$210 R16  $20

.ORG $0 Ptr  $200


LDI R16,$20
LDI ZH,HIGH($200)
(Ptr)  R16
LDI ZL,LOW($200)
Again: ST Z+,R16
Ptr  Ptr+1
CPI ZL,$11
BRNE Again
N
ZL - $11 Ptr=$211?
Z=0
SIMILAR TO DO … WHILE … Z=1
Y
Nguyễn Trung Hiếu 29
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


R17  $30
Solutions (1)
Ptr  $200

Loop  17
.ORG $0
LDI R17,$30 R16  (Ptr)
LDI ZH,HIGH($200)
LDI ZL,LOW($200) R16  R16+R17
LDI R18,17
Again: LD R16,Z (Ptr)  R16
ADD R16,R17
ST Z+,R16 Ptr  Ptr+1
DEC R18
Loop  Loop - 1
BRNE Again
N

Loop=0?
Nguyễn Trung Hiếu
Z=0 31
Y Z=1
R17  $30
Solutions (2)
Ptr  $200

.ORG $0 R16  (Ptr)


LDI R17,$30
LDI ZH,HIGH($200) R16  R16+R17
LDI ZL,LOW($200)
Again: LD R16,Z (Ptr)  R16
ADD R16,R17
ST Z+,R16 Ptr  Ptr+1
CPI ZL,$11
BRNE Again
N
Ptr=$211?
Z=0
Z=1
Y
Nguyễn Trung Hiếu 32
For loop – Application
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 33
For loop – Application
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) Nguyễn Trung Hiếu 34
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
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 36


Timing Program

CLR R1 ; 1 MC
1 time LDI R16,10 ; 1 MC
LDI R17,5 ; 1 MC
AGAIN: ADD R1,R16 ; 1 MC
5 times DEC R17 ; 1 MC
4 times T BRNE AGAIN ; True – 2 MCs
1 time F
; False – 1 MC
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 37
Blinky Program

PC0  1

Delay

PC0  0

Delay

Nguyễn Trung Hiếu 38


Blinky Program
.ORG 0
SBI DDRC,0

LOOP: SBI PORTC,0 PC0  1


CALL DELAY
CBI PORTC,0
CALL DELAY Delay
RJMP LOOP

DELAY: PC0  0
LDI R21,100
L1: LDI R20,250
L2: NOP Delay
DEC R20
BRNE L2
DEC R21
BRNE L1 → Waveform? Period? Frequency?
RET
Nguyễn Trung Hiếu 39
Blinky Program
.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 250x(1+1+2) – 1 = 999 MCs
BRNE L2 ; 2/1 MC
DEC R21
BRNE L1
RET
Nguyễn Trung Hiếu 40
Blinky Program
.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 100x(999+1+1+2) – 1
BRNE L2 ; 2/1 MC = 100299 MCs
DEC R21 ; 1 MC
BRNE L1 ; 2/1 MC
RET
Nguyễn Trung Hiếu 41
Blinky Program
.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 100299 + 1 + 4
BRNE L2 ; 2/1 MC = 100304 MCs
DEC R21 ; 1 MC
BRNE L1 ; 2/1 MC
RET ; 4 MC
Nguyễn Trung Hiếu 42
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

DELAY:
LDI R21,100 ; 1 MC
L1: LDI R20,250 ; 1 MC
L2: NOP ; 1 MC
DEC R20 ; 1 MC 100299 + 1 + 4
BRNE L2 ; 2/1 MC = 100304 MCs
DEC R21 ; 1 MC
BRNE L1 ; 2/1 MC
RET ; 4 MC
Nguyễn Trung Hiếu 43
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

Nguyễn Trung Hiếu 44


Blinky Program
If fMC = 1MHz, 1 MC = 1 μs
→ tH = 100,309 MC = 100,309 μs PC0  1
→tL = 100,311 MC = 100,311 μs
Delay tH
→ T = tH + tL = 200,620 μs
→ f = 1/T = 4.98 Hz PC0  0

Delay tL

tH tL

Nguyễn Trung Hiếu 45


Blinky Program - Estimating

DELAY: 4 MCs
LDI R21,100 PC0  1
L1: LDI R20,250 ; 1 MC
L2: NOP ; 1 MC
DEC R20 ; 1 MC Delay tH
BRNE L2 ; 2/1 MC
DEC R21 ; 1 MC
BRNE L1 ; 2/1 MC PC0  0
RET
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
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

Nguyễn Trung Hiếu 47


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

PC0  1
tH tL

T
Delay tH
tH = tL
PC0  0

Delay tL

Nguyễn Trung Hiếu 48


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 49


10-kHz square wave, duty cycle 30%
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
PC0  1

tH tL
Delay tH
T

Chu kỳ nhiệm vụ = D = TH/T PC0  0

Delay tL

Nguyễn Trung Hiếu 50


10-kHz square wave, duty cycle 30%
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
PC0  1
fMC = 4MHz → 1 MC = 0,25 µs
f = 10 kHz → T = 200 µs Delay tH
TH = 0,3T = 60 µs = 4.60.0,25 µs = 4.60 MCs
TL = 0,7T = 140 µs
PC0  0
= 4.140.0,25 µs = 4.140 MCs
Delay tL

Nguyễn Trung Hiếu 51


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 52


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

Nguyễn Trung Hiếu 53


If … else … – Equal/Not Equal (1)
N
R16 = $05? CPI R16,$05
Z=0 BRNE Skip
Z=1
Y (Statement 1)
Statement 1 Skip: (Continue)

N CP R16,R17
R16 = R17?
Z=0 BRNE Skip
Z=1 (Statement 1)
Y
Statement 1 Skip: (Continue)

Nguyễn Trung Hiếu 54


If … else … – Equal/Not Equal (2)

N
CPI R16,$05
R16 = $05?
Z=0 BRNE Not_Eq
Z=1 (Statement 1)
Y
Statement 2 Statement 1 RJMP Next
Not_Eq: (Statement 2)
Next: (Continue)

Nguyễn Trung Hiếu 55


If … else … –
Greater Than or Equal/Less Than (1)

CPI R16,$05
N
R16 ≥ $05? BRCC LessThan
C=1
(Statement 1)
C=0
Y LessThan: (Continue)
Statement 1

Nguyễn Trung Hiếu 56


If … else … –
Greater Than or Equal/Less Than (2)

CPI R16,$05
N
R16 < $05? BRCS GrEqThan
C=0
(Statement 1)
C=1
Y GrEqThan: (Continue)
Statement 1

Nguyễn Trung Hiếu 57


Ví dụ
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.

.ORG $0 LessThan:
LDI R16,$FF NEQ R16
OUT DDRA,R16 RJMP NEXT
CPI R16,5 GrEgThan:
BRCC LessThan COMP R16
CPI R16,11 NEXT: OUT PORTA,R16
BRCS GrEqThan NOP

Nguyễn Trung Hiếu 58


If … else … –
Greater Than or Equal/Less Than (3)

N
CPI R16,$05
R16 ≥ $05? BRCC LessThan
C=1 (Statement 1)
C=0
Y RJMP Next
Statement 2 Statement 1 LessThan: (Statement 2)
Next: (Continue)

Nguyễn Trung Hiếu 59


If … else … –
Greater Than or Equal/Less Than (4)

N
CPI R16,$05
R16 < $05? BRCS GrEqThan
C=0 (Statement 1)
C=1
Y RJMP Next
Statement 2 Statement 1 GrEqThan: (Statement 2)
Next: (Continue)

Nguyễn Trung Hiếu 60


If … else … – Bit in Register is set/cleared (1)
Y
R16(0)=1? SBRS R16,0
(Statement 1)
N (Continue)
Statement 1
Note: Statement 1
- 1 instruction
- If > 1 instruction, call subroutine

Y SBRC R16,0
R16(0)=0?
(Statement 1)
(Continue)
N
Statement 1

Nguyễn Trung Hiếu 61


If … else … – Bit in Register is set/cleared (2)

Y
SBRS R16,0
R16(0)=1?
RJMP BitSet
N (Statement 2)
Statement 2 Statement 1 RJMP Next
BitSet: (Statement 1)
Next: (Continue)

Nguyễn Trung Hiếu 62


Bit Testing (1)
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

Nguyễn Trung Hiếu 63


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

Nguyễn Trung Hiếu 64


Bit Testing (2)
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 65


References
• 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ả

Nguyễn Trung Hiếu 66

You might also like