L08-Branch and Loop
L08-Branch and Loop
L08-Branch and Loop
AVR Microcontroller:
Jump (aka Branch)
2
Jump and Call (Continued)
• But sometimes we need the CPU to execute an instruction
other than the next instruction.
– For example:
• When we use a conditional instruction (if)
• When we make a loop
• When we call a function (subroutine)
3
Jump and Call (Continued)
• Example 1: Not executing the
next instruction, because of
condition.
– In the following example, the 1 void main ()
instruction of line 6 is not 2 {
executed. 3 int a = 2;
4 int c = 3;
5 if (a == 8)
6 c = 6;
7 else
8 c = 7;
9 c = a + 3;
}
4
Jump and Call (Continued)
• Example 2: In this example, the
next instruction will not be
executed because of loop.
– In the following example, the 1 void main ()
order of execution is as follows: 2 {
• Line 4 3 int a, c = 0;
4 for(a = 2; a < 4; a++)
• Line 5
5 c += a;
• Again, line 4 6 a = c + 2;
• Again line 5 7 }
• Line 6 8
9
5
Jump and Call (Continued)
• Example 3: Not executing the
next instruction, because of
calling a function. Code
– In the following example, the 1 void func1 ();
instruction of line 6 is not 2 void main ()
executed after line 5. 3 {
4 int a = 2, c = 3;
5 func1 ();
6 c = a + 3;
7 }
8 void func1 (){
9 int d = 5 / 2;
10 }
11
6
Jump and Call (Continued)
• In the assembly language, there are 2 groups of
instructions that make the CPU execute an instruction
other than the next instruction.
– These instructions are:
• Jump: used for making loop and condition
• Call: used for making function calls
7
Jump
• Jump changes the Program Counter (PC) and
causes the CPU to execute an instruction other
than the next instruction.
8
Jump
There are 2 kinds of Jump:
– Unconditional Jump:
• When CPU executes an unconditional jump, it jumps
unconditionally (without checking any condition) to the target
location.
– Example: RJMP and JMP instructions
– Conditional Jump:
• When CPU executes a conditional jump, it checks a condition, if
the condition is true then it jumps to the target location;
otherwise, it executes the next instruction.
9
Unconditional Jump
10
Unconditional Jump in AVR
• There are 3 unconditional jump
instructions in AVR: RJMP, JMP,
and IJMP
Code
• We label the location where we
1 LDI R16, 0
want to jump, using a unique
name or label, followed by ‘:’ 2 LDI R17, 2
3 HERE: ADD R16, R17
• Then, in front of the jump
instruction we mention the name 4 RJMP HERE
HERE
of the label. 5 SUB R10,R15
• This causes the CPU to jump to
the location we have labeled,
instead of executing the next
instruction.
11
Ways of specifying the Jump Target
• There are 3 ways to provide the jump address:
– PC = operand
– PC = PC + operand
– PC = Z register
12
JMP
• JMP PC = operand
– Long Jump
– 4-bytes instruction
• 10-bits for the op-code and rest 22-bits for the target address
• 22-bit = 4M memory locations
13
JMP
• JMP PC = operand
1001 010X XXXX 110X XXXX XXXX XXXX XXXX XXX
– Example:
• Operand = 0000000000000000000110
14
JMP
• Address Code
In JMP, the operand PC: 0007
0001
0000
0002
contains the 000 .ORG 0
address of the 0 LDI R16, 15
destination Machine 000
LDI R17, 5
• code: 0
When a JMP is 940C 0006
0006 JMP LBL_NAME
executed: opCod operan
000
1 LDI R18, 4
– PC is loaded e d
000 ADD R18, R17
with the 2 LBL_NAME: ADD R16,R17
operand value Machine 0006
000
code: JMP LBL_NAME
940C 0006
0006 4
LDI R18, 4
opCod operan 000
e d 5
000
6
000
7
000
9 15
RJMP (Relative Jump)
• RJMP:
– 2-bytes instruction
– Lower 12-bits for the relative address of the target
Range divided into forward and backward jumps.
16
RJMP (Relative jump)
• RJMP PC = PC + operand
• Operand = 000000000110
• PC = PC + 000000000110
17
RJMP
• When RJMP is
executed:
– The operand PC: 0003
0007
0006
0001
0000
0002 Addres Code
s
will be added +0
+F 0000 .ORG 0
18
IJMP (Indirect Jump)
• IJMP: PC = Z register
– 2-bytes instruction
– PC is loaded with the contents of Z-register
So jumps to the address pointed to by the Z-register
– For example, if Z points to location 100, by executing IJMP, the CPU
jumps to location 100
The instruction has no operand
19
Conditional Jump
20
Conditional Jump in AVR
SREG: I T H S V N Z C
• The conditional jump instructions in AVR are as follows:
Instruction Abbreviation of Comment
BREQ lbl Branch if Equal Jump to location lbl if Z = 1,
BRNE lbl Branch if Not Equal Jump if Z = 0, to location lbl
BRCS lbl Branch if Carry Set Jump to location lbl, if C = 1
BRLO lbl Branch if Lower
BRCC lbl Branch if Carry Cleared Jump to location lbl, if C = 0
BRSH lbl Branch if Same or Higher
BRMI lbl Branch if Minus Jump to location lbl, if N = 1
BRPL lbl Branch if Plus Jump if N = 0
BRGE lbl Branch if Greater or Equal Jump if S = 0
BRLTlbl Branch if Less Than Jump if S = 1
BRHS lbl Branch if Half Carry Set If H = 1 then jump to lbl
BRHC lbl Branch if Half Carry Cleared if H = 0 then jump to lbl
BRTS Branch if T flag Set If T = 1 then jump to lbl
BRTC Branch if T flag Cleared If T = 0 then jump to lbl
BRIS Branch if I flag set If I = 1 then jump to lbl
BRIC Branch if I flag cleared If I = 0 then jump to lbl
21 21
Usages of Conditional jump
• Conditions and
• Loop
22
Looping Instructions
23
Looping: Using BRNE
• BRNE [BRanch if Not Equal]:
– uses the ZERO flag [Z=0]
LDI R16, 0
LDI R17, 3
LDI R18, 5 ; R18 acts as the loop
counter
LOOP: ADD R16, R17
DEC R18
BRNE LOOP ; branch if
24
Looping: Example
• Write a program to:
a. Clear R20
b. Add 3 to R20 ten times
c. And then send the sum to PORTB
25
Looping: Example
26
Looping: Example Overall
• Write a program to:
a. Clear R20
b. Add 3 to R20 ten times
c. And then send the sum to PORTB
27
Reading
• The AVR Microcontroller and Embedded Systems: Using
Assembly and C by Mazidi et al., Prentice Hall
– Chapter-3: 3.1
28
THANK YOU