Professional Documents
Culture Documents
Untitled
Untitled
- SREG에는 영향 없음
- unconditional jump (항상 jump 함)
- 참고: conditional jump (어떤 조건을 만족할 때만 jump)
1
• RJMP k PC = PC + 1 + k
프로그램 명령
메모리 주소
0번지 LDI r16, 1 (1)
1번지 RJMP kkkk kkkk kkkk (2) PC = PC + 1 + kkkk …
2번지 LDI r17, 2
3번지 LDI r18, 2 (5)
4번지 LDI r19, 3 (3)
5번지 RJMP kkkk kkkk kkkk (4) PC = PC + 1 + kkkk ….
2
[1] RJMP: label을 사용한 rjmp (kkkk kkkk kkkk의 값)
START:
LDI R16, $42 ;PC = 0
CPI R16, $42 ;PC = 1
BRNE ERROR ;PC = 2
RJMP OK ;PC = 3 1100 kkkk kkkk kkkk?
ERROR:
ADD R16, R17 ;PC = 4
INC R16 ;PC = 5
RJMP START ;PC = 6 1100 kkkk kkkk kkkk?
OK:
NOP ;PC = 7
RJMP START
3
[1] RJMP: label을 사용한 rjmp (kkkk kkkk kkkk의 값)
START:
LDI R16, $42 ;PC = 0
CPI R16, $42 ;PC = 1
BRNE ERROR ;PC = 2
RJMP OK ;PC = 3 1100 0000 0000 0011 (+3)
ERROR:
ADD R16, R17 ;PC = 4
INC R16 ;PC = 5
RJMP START ;PC = 6 1100 1111 1111 1001 (-7)
OK:
NOP ;PC = 7
RJMP START
4
[2] IJMP (Indirect Jump)
- Indirect jump to the address pointed to by the Z (16 bits) Pointer Register in
the Register File.
5
[2] IJMP: 실습
• IJMP 명령 실행 후, mov r0, r16이 실행 됨
6
[3] JMP (Jump)
• Jump to an address within the entire 4M (words) Program memory
• SREG 변화 없음
• RJMP 는 2 byte 명령임으로 가능하면 RJMP 사용!
7
[3] JMP: 실습
START:
LDI R16, 6
MOV R30, R16
JMP LOOP
NOP
NOP
NOP
LOOP:
NOP
NOP
JMP START
8
[4] BREQ (Branch if Equal)
- Conditional relative branch.
- Tests the Zero Flag (Z) and branches relatively to PC if Z is set.
- If the instruction is executed immediately after any of the instructions CP, CPI, SUB,
or SUBI, the branch will occur if and only if the unsigned or signed binary number
represented in Rd was equal to the unsigned or signed binary number
represented in Rr.
-This instruction branches relatively to PC in either direction
(PC -63 ≤ destination ≤ PC + 64).
9
• 상태 레지스터(SREG, Status Register)
10
[4] BREQ : r18의 값은?
11
[4] BREQ : r18의 값은?
r18 = 1 r18 = 2
12
[5] BRNE (Branch if Not Equal)
- Conditional relative branch.
- Tests the Zero Flag (Z) and branches relatively to PC if Z is cleared.
- If the instruction is executed immediately after any of the instructions CP, CPI, SUB,
or SUBI, the branch will occur if and only if, the unsigned or signed binary number
represented in Rd was not equal to the unsigned or signed binary number
represented in Rr.
-This instruction branches relatively to PC in either direction (PC - 63 ≤ destination ≤
PC + 64).
13
[5] BRNE : r18의 값은?
14
[5] BRNE : r18의 값은?
r18 = 2 r18 = 1
15
• 간단한 loop 의 실행
brne loop1
loop2:
16
• .def 명령을 사용: r16 register가 loop counter로 사용
.def count=r18
loop2: loop2:
17
• 조심 : conditional branch는 바로 전에 명령에 영향을 받음
loop2: loop2:
18
• 연습문제: 0x100번지~0x110 번지에 50을 집어 넣음
ldi xl, 0
ldi xh, 1
ldi r16, 50
loop1: ___________________
dec r17
brne loop1
loop2:
19
• 연습문제: 0x100번지~0x110 번지에 50을 집어 넣음
ldi xl, 0
ldi xh, 1
ldi r16, 50
dec r17
brne loop1
loop2:
20
• Exercise 1: 다음 C 명령을 assembly로 구현하라 (sum = 5 + 4 + 3 + 2 + 1 = 15)
– r16: sum, r17: count 로 할 것
.def sum=r16
unsigned char sum, count;
.def count=r17
sum = 0;
ldi sum, 0
count = 5;
do {
count = count – 1 }
while ( count != 0 )
21
• Exercise 2: 2 + 4 + 6 + 8 + 10 을 계산하는 프로그램을 작성하라. (합은 r16에 저장
할 것) (답: 30 = 0x1E)
22
• AVR studio Debugging (Debug 메뉴 밑에 있음)
23