You are on page 1of 23

[1] RJMP (Relative Jump)

- Relative jump to an address within PC - 2K +1 and PC + 2K (words). For AVR


microcontrollers with Program memory not exceeding 4K words (8KB) this
instruction can address the entire memory from every address location.

- 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 ….

• kkkk kkkk kkkk (signed number)


– 1000 0000 0000 (-2048) ~ 0111 1111 1111 (2047)
• 1번지에서 rjmp 할 수 있는 최대값은 1 + 1 + 2047 = 2049번지까지 jump 가능

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.

- 상대적인 jump 가 아닌 절대주소로 jump


- SREG 영향 없음

5
[2] IJMP: 실습
• IJMP 명령 실행 후, mov r0, r16이 실행 됨

5번지: LDI r16, 30


6번지: mov r0, r16
7번지: LDI ZL, 6
8번지: LDI ZH, 0 ;Z=6
9번지: IJMP ; 6번지로 jump 함

• Z = 0x0006  6번지로 jump함


– 6 번지 앞으로 jump 하는 것이 아님

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의 값은?

ldi r16, 10 ldi r16, 10

ldi r17, 10 ldi r17, 20

ldi r18, 1 ldi r18, 1

sub r16, r17 sub r16, r17

breq abc breq abc

ldi r18, 2 ldi r18, 2

abc: nop abc: nop

11
[4] BREQ : r18의 값은?

ldi r16, 10 ldi r16, 10

ldi r17, 10 ldi r17, 20

ldi r18, 1 ldi r18, 1

sub r16, r17 sub r16, r17

breq abc breq abc

ldi r18, 2 ldi r18, 2

abc: nop abc: nop

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의 값은?

ldi r16, 10 ldi r16, 10

ldi r17, 10 ldi r17, 20

ldi r18, 1 ldi r18, 1

sub r16, r17 sub r16, r17

brne abc brne abc

ldi r18, 2 ldi r18, 2

abc: nop abc: nop

14
[5] BRNE : r18의 값은?

ldi r16, 10 ldi r16, 10

ldi r17, 10 ldi r17, 20

ldi r18, 1 ldi r18, 1

sub r16, r17 sub r16, r17

brne abc brne abc

ldi r18, 2 ldi r18, 2

abc: nop abc: nop

r18 = 2 r18 = 1

15
• 간단한 loop 의 실행

ldi r17, 0 r17 = 0, r16 = 3

ldi r16, 3 loop1: r17 = 1, r16 = 2, Z = 0, loop1로 jump

loop1: inc r17 loop1: r17 = 2, r16 = 1, Z = 0, loop1로 jump

dec r16 loop1: r17 = 3,. r16 = 0, Z = 1, loop2로 jump

brne loop1

loop2:

16
• .def 명령을 사용: r16 register가 loop counter로 사용

.def count=r18

ldi r17, 0 ldi r17, 0

ldi r16, 3 ldi count, 3

loop1: inc r17 loop1: inc r17

dec r16 dec count

brne loop1 brne loop1

loop2: loop2:

17
• 조심 : conditional branch는 바로 전에 명령에 영향을 받음

ldi r17, 0 ldi r17, 0

ldi r16, 3 ldi r16, 3

loop1: inc r17 loop1: dec r16

dec r16 inc r17 Z flag 변함

brne loop1 brne loop1

loop2: loop2:

loop 3번 실행 r16을 loop counter로


r16: loop counter 잘못 사용한 경우

18
• 연습문제: 0x100번지~0x110 번지에 50을 집어 넣음

ldi xl, 0

ldi xh, 1

ldi r16, 50

ldi r17, ______________ ; loop counter

loop1: ___________________

dec r17

brne loop1

loop2:

19
• 연습문제: 0x100번지~0x110 번지에 50을 집어 넣음

ldi xl, 0

ldi xh, 1

ldi r16, 50

ldi r17, 0x11 ; 17번 loop를 실행 (0x100 ~ 0x110 번지)

loop1: st x+, r16

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 {

sum= sum + count;

count = count – 1 }

while ( count != 0 )

21
• Exercise 2: 2 + 4 + 6 + 8 + 10 을 계산하는 프로그램을 작성하라. (합은 r16에 저장
할 것) (답: 30 = 0x1E)

• Exercise 3 : 0x100번지~0x110 번지에 0~0x10의 값을 순서대로 넣는 프로그램을 작


성하라. (0x100번지에는 0, 0x101번지에는 1, …. )

• Exercise 4: 0x100번지~0x110 번지에 들어 있는 값은 0x200번지~0x210번지로 복사


하는 프로그램을 작성하라.

• Exercise 5: 1에서 100까지 더한 값은 r17:r16에 집어 넣는 프로그램을 작성하라


– 답: 5050 = 0x13BA  r17에 0x13, r16에 0xba 가 들어가 있어야 됨

22
• AVR studio Debugging (Debug 메뉴 밑에 있음)

• 1명령씩 실행함: F11 (step into)

• Cursor 위치까지 실행함: Ctrl-F10 (Run-to-Cursor)

• Breakpoint 설정: F9 (toggle breakpoint)


– F5 (Continue) : 실행 (끝까지 또는 breakpoint 만날때까지)

23

You might also like