0% found this document useful (0 votes)
51 views48 pages

4 JumpAndCall v22

This document discusses branch and call instructions in CPUs. It explains that branch instructions change the program counter to allow the CPU to execute instructions out of order, such as for conditional statements, loops, and function calls. The ARM architecture uses B and BX branch instructions, where B adds an offset to the program counter and BX loads the program counter from a register. Conditional branch instructions in ARM check status flags in the condition program status register.

Uploaded by

vokhacnam2k1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views48 pages

4 JumpAndCall v22

This document discusses branch and call instructions in CPUs. It explains that branch instructions change the program counter to allow the CPU to execute instructions out of order, such as for conditional statements, loops, and function calls. The ARM architecture uses B and BX branch instructions, where B adds an offset to the program counter and BX loads the program counter from a register. Conditional branch instructions in ARM check status flags in the condition program status register.

Uploaded by

vokhacnam2k1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 48

Branch & Call

Chapter 4

Sepehr Naimi

www.NicerLand.com
Topics
 Introduction to branch and call
 Branch
 Call
 LR
 Calling a function
 Time Delay

2
Branch and Call
 CPU executes instructions one
after another.
1 void main ()
 For example in the following C 2 {
program, CPU first executes the 3 a = b + c;

instruction of line 3 (adds b and 4 c -= 2;

c), then executes the instruction 5 d = a + c;


6 }
of line 4.

3
Branch 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

4
Branch and Call (Continued)
 Example 1: Not executing the
next instruction, because of
condition. 1 void main ()
 In the following example, the 2 {

instruction of line 6 is not


3 int a = 2;
4 int c = 3;
executed. 5 if (a == 8)
6 c = 6;
7 else
8 c = 7;
9 c = a + 3;
}

5
Branch and Call (Continued)
 Example 2: In this example
the next instruction will not
be executed because of 1 void main ()
loop. 2 {
3
In the following example,
int a, c = 0;

4 for(a = 2; a < 4; a++)

the order of execution is as 5 c += a;

follows: 6
7 }
a = c + 2;

 Line 4 8
9
 Line 5
 Again, line 4
 Again line 5
 Line 6
6
Branch and Call (Continued)
 Example 3: Not executing
the next instruction, because
of calling a function. 1
Code

void func1 ();


 In the following example, 2 void main ()

the instruction of line 6 is


3 {
4 int a = 2, c = 3;
not executed after line 5. 5 func1 ();
6 c = a + 3;
7 }
8 void func1 (){
9 int d = 5 / 2;
10 }
11

7
Branch 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. The
instructions are:
 Branch: used for making loop and condition
 Call: used for making function calls

8
Branch
 Branch changes the Program Counter (PC)
and causes the CPU to execute an instruction
other than the next instruction.

9
Branch in ARM
 There are 2 branch instructions
in ARM: B and BX.
 We label the location where we Code

want to jump, using a unique 1 MOV R1, #0


2 MOV R2, #2
name 3 L1
L1 ADD R1, R1, R2

 Then, in front of the branch 4 B L1


5 SUB R10,R1,R5
instruction we mention the
name of the label.
 This causes the CPU to jump to
the location we have labeled,
instead of executing the next
instruction.

10
Ways of specifying the branch target
 B and BX provide the branch address using 2
different ways:
 B operand
 PC = PC + (operand*2)

 BX Rn
 PC = Rn register

11
B (Branch)
 B PC = PC + (operand×2)
 To execute B: The operand shifts left and then it is
added to the current value of PC.
1110 0xxx XXXX XXXX

 Example: 1110 0000 0000 0000 0000 0000 0000 0111

 Operand = 000000000111
 PC = PC + 000000001110

12
B instruction
 To execute B the operand shifts left and then it is added to the current value of PC.
 Cortex-M has a 3-stage pipeline. So, when an instruction is executed, PC points to 4 bytes below

PC: 0800001C
0800000C
0800001E
08000010
08000014 Address Code
+ 8000008 EXPORT __main
00000018 8000008 __main MOV R1,#0x15
Machine code:
opCode operand 800000C MOV R7, #5

E002 8000010 B LBL_NAME


Shift left 8000012 MOV R8, #4
00000004
8000016 ADD R8, R8, R7
8000018 LBL_NAME
Machine code:
opCode operand 8000018 ADD R6, R6, R7
E7FD 800001A B LBL_NAME
FFFFFFFA 800001C

13
BX
 BX Rn PC = Rn 0100 0111 0nnn n000

BX R3 0100 0111 0001 1000

 The Program counter is loaded with


the contents of Rn register.
 For example, if R5 points to location

100, by executing BX R5, the CPU


jumps to location 100.

14
BX instruction
 To execute BX:
 The value of a register is loaded to the PC.

Address Code
PC: 0800000C
08000010
08000012 8000008 AREA PROG,CODE,READONLY
8000008 __main MOV R1,#0x15
R7: 00000000
80000016 800000C LDR R7, =LBL1
800000E BX R7
8000010 MOV R8, #4
8000014 ADD R8, R8, R7
8000016 LBL1
8000016 ADD R6, R6, R7
8000018 B LBL1
800001A

15
Conditional Jump in ARM
D31 D30 D29 D28 ………. D7 D6 D5 D4 D3 D2 D1 D0
CPSR: N Z C V Reserved I F T M4 M3 M2 M1 M0

Negative oVerflow
Zero carry

 The conditional jump instructions in ARM are as follows:


Instruction Abbreviation of Action
BCS lbl branch if carry set Branch to location lbl if C = 1
BHS lbl branch if higher or same
BCC lbl branch if carry clear Branch if C = 0
BLO lbl branch if lower
BEQ lbl branch if equal Branch if Z = 1
BNE lbl branch if not equal Branch if Z = 0
BLS lbl branch if lower or same Branch if Z = 1 or C = 0
BHI lbl branch if higher Branch if Z = 0 and C = 1
 The N and V flags are related to signed numbers and
their branch instructions are discussed in Chapter 5.
16
Conditional vs. Unconditional
There are 2 kinds of Branch
 Unconditional Branch: When CPU executes an
unconditional branch, it jumps unconditionally (without
checking any condition) to the target location.
 Example: BX and B instructions
 Conditional Branch: When CPU executes a conditional
branch, it checks a condition, if the condition is true then it
jumps to the target location; otherwise, it executes the next
instruction.

17
Usages of Conditional jump
 Conditions
 Loop

18
Conditions
 When b is subtracted from a:
 The result is zero, when a is equal to b a
 Carry will be set when a >= b -b

19
Example 1
 Write a program that if R0 is equal to R1 then R2
increases.

 Solution: R0 == R1
No

SUBS R0,R0,R1 ;Z will be set if R0 == R1


Yes
BNE NEXT ;if Not Equal jump to next
ADD R2,R2,#1
increment R2
NEXT

20
Example 2
 Write a program that if R6 < R4 then R2
increases.

 Solution: R6 < R4
No

SUBS R6,R6,R4 ;C will be set when R6 >= R4


Yes
BCS L1 ;if Carry set jump to L1
ADD R2,R2,#1
increment R2
L1

21
Example 3
 Write a program that if R6 >= R4 then R2
increases.

Solution:
No
 R6 >= R4

SUBS R6,R6,R4 ;C will be set when R6 >= R4 Yes


BCC L1 ;jump to L1 if Carry cleared
ADD R2,R2,#1 increment R2
L1

22
Example 4: IF and ELSE
int main ( )
R7 = 5
{
R7 = 5;
if (R0 > R1)
R2++; No
R0 > R1
else
R2--;
R7++; Yes
}

increment R2

MOV R7,#5
SUBS R1,R1,R0 ;C is set when R1 >= R0 decrement R2

BCS ELSE_LABEL ;jump to else if set


ADD R2,R2,#1
JMP NEXT
ELSE_LABEL
SUB R2,R2,#1 increment R7

NEXT
ADD R7,R7,#1

23
Loop
 Write a program that executes the instruction
“ADD R3,R3,R1” 9 times.
R6 = 9

 Solution:
MOV R6,#9 ;R6 = 9 R3 = R3 + R1
L1 ADD R3,R3,R1
SUBS R6,R6,#1 ;R6 = R6 - 1
BNE L1 ;if Z = 0 R6 = R6 - 1

L2 B L2 ;Wait here forever

Yes
R6 > 0

No

END

24
Loop
 Write a program that calculates the result of
9+8+7+…+1
R6 = 9
R7 = 0

 Solution:
R7 = R7 + R6
MOV R6, #9 @R6 = 9
MOV R7, #0 @R7 = 0
R6 = R6 - 1
L1 ADD R7,R7,R6 @R7 = R7 + R6
SUBS R6,R6,#1 @R6 = R6 - 1
BNE L1 @if Z = 0
L2 B L2 @Wait here forever Yes
R6 > 0

No

END

25
Loop
 Write a program that calculates the result of
20+19+18+17+…+1
R6 = 20
R7 = 0
 Solution:
R7 = R7 + R6
MOV R6, #20 @R6 = 20
MOV R7, #0 @R7 = 0
L1 ADD R7,R6,R6 @R7 = R7 + R6 R6 = R6 - 1

SUBS R6,R6,#1 @R6 = R6 - 1


BNE L1 @if Z = 0
L2 B L2 @Wait here forever
Yes
R6 > 0

No

END

26
Loop
for (init; condition; calculation) init

{
do something
Do something

} calculation

Yes
Condition

No

END

27
Loop
 Write a program that calculates 1+3+5+…+27
 Solution:
R0 = 0
R6 = 1
MOV R0,#0
MOV R6,#1 R0 = R0 + R6

L1 ADD R0,R0,R6
ADD R6,R6,#2 ;R16 = R16 + 2 R6 = R6 + 2

SUBS R7,R6,#27
BCC L1 ;if R6 <= 27 jump L1
Yes
R6 <= 27

No

END

28
CMP instruction
 CMP sets the flags the same way as SUBS. But
does not store the result of subtraction. So, it
suits for comparing.

MOV R0,#0 MOV R0,#0


MOV R6,#1 MOV R6,#1
L1 ADD R0,R0,R6 L1 ADD R0,R0,R6
ADD R6,R6,#2 ADD R6,R6,#2
SUBS R7,R6,#27 CMP R6,#27
BCC L1 BCC L1

29
Conditional Execution in ARM (Case Study)
31 28 27 26 0

Cond Instruction

Bits Mnemonic Extension Meaning Flag


0000 EQ Equal Z=1
0001 NE Not equal Z=0
0010 CS/HS Carry Set/Higher or Same C=1
0011 CC/LO Carry Clear/Lower C=0
0100 MI Minus/Negative N=1
0101 PL Plus N=0
0110 VS V Set (Overflow) V=1
0111 VC V Clear (No Overflow) V=0
1000 HI Higher C = 1 and Z = 0
mov r1, #10 ; r1 = 10
1001 HS Lower or Same C = 1 and Z = 1
cmp r1, #0 ; compare r1 with 0
1010 GE Greater than or Equal N=V
addne r1, r1, #10 ; this line is executed if z = 0
1011 LT Less than N≠V
; (if in the last cmp operands were not equal)
1100 GT Greater than Z = 0 and N = V
1101 LE Less than or Equal Z = 0 or N ≠ V
1110 AL Always (unconditional)
1111 --- Not Valid
30
Calling a Function
 To execute a call:
 Address of the next instruction is saved in LR (R14).
 PC is loaded with the subroutine address

Address Code
08000010 AREA PROG,CODE,READONLY
08000010 __main MOV R1, #5
Machine code: 08000014 MOV R2, #6
F000F803 08000018 BL FUNC_NAME
Shiftoperand
opCode left 0800001C
0800001C MOV R2, #6
0006 08000020 L1 B L1

LR: 0800001C
00000000 08000022 FUNC_NAME
08000022 ADD R2,R2,R1
08000024 SUB R2,R2,#3
PC: 0800001C
08000014
08000028
08000024
0800002C
08000018
08000028 BX LR
+ 0800002A
08000022

31
PROC and ENDP
 The directives are used to mark the beginning
and end of a subroutine:

FUNC_NAME PROC
...
BX LR
ENDP

32
Time delay

33
Time delay

Microcontroller
XTAL1 1
XTAL2
TMachine cycle =
FXTAL

For F = 1GHz:
1
TMachine cycle = = 1 ns
1GHz

34
Time delay
machine cycle
MOV R6, #19 1
MOV R0, #95 1
MOV R1, #5 1
ADD R6, R6, R0 1
ADD R6, R6, R1 1
5

Delay = 5 x T = 5 x 1 ns = 5 ns
machine cycle

35
Time delay

machine cycle
MOV R6, #100
1
AGAIN: ADD R7,R7,R6 *100
1
*100
SUBS R6,R6,#1 1
BNE AGAIN 1/3 *100

Branch penalty

36
Time delay

machine cycle
MOV R6, #50
1
AGAIN: NOP 1 *50
NOP 1 *50

SUBS R6,R6,#1 1 *50

BNE AGAIN 1/3 *50

37
Time delay

machine cycle
MOV R7, #20
1
L1: MOV R6, #50
1 *20
L2: NOP
1 *20 * 50
NOP 1 *20 * 50
SUBS R6,R6,#1 1 *20 * 50
BNE L2 1/3 *20 * 50
SUBS R7,R7,#1 1 *20
BNE L1 1/3 *20

38
Stack

39
Push and Pop

Push Pop

40
Stack Pointer
 R13 is the stack pointer

 PUSH {Rn} 0x2000050


0x2000054
 SP = SP – 4 0x2000058
0x200005C
 [SP] = Rn 0x2000060
SP 0x2000064

 POP {Rn}
 Rn = [SP]
 SP = SP + 4

41
Push and Pop (Cont.)

PUSH {register list} POP {register list}


 PUSH {R10}  POP {R9}
 PUSH {R2,R3,R5}  POP {R4-R11}
 PUSH {R5-R9}  POP {R1-R4,R7}

42
Stack

Address Code

...
R0: 00000000
00000010 LDR R0,=0x10
LDR R1,=0x20
R1: 00000000
00000020 LDR R2,=0x30
R2: 00000030
00000000 PUSH0x00000010
{R0}
PUSH0x00000020
{R1}
0x00000030
PUSH {R2}
MOV R0,#0
MOV R1,#0
MOV R2,#0
POP {R2}

20005F0 POP {R1}


20005F4 POP {R0}
20005F8 L1 B L1
20005FC
2000600
SP Memory

43
Stack Initialization
CPU Memory
20002000 00000000
00001001 00000004
SP (R13): 00000008

PC (R15): 00001000
Flash Boot
memory program

20000000
Stack
SRAM 20001FFF
20002000
3FFFFFFF

44
Startup File
; memory allocation for stack Stack
Stack_Size EQU 0x00000400

AREA STACK, NOINIT, READWRITE, ALIGN=3


Stack_Mem SPACE Stack_Size
__initial_sp

; Heap Configuration Heap


Heap_Size EQU 0x00000200

AREA HEAP, NOINIT, READWRITE, ALIGN=3


__heap_base
Heap_Mem SPACE Heap_Size
__heap_limit

PRESERVE8 Vector Table


THUMB
; Vector Table Mapped to Address 0 at Reset
AREA RESET, DATA, READONLY
EXPORT __Vectors
EXPORT __Vectors_End
EXPORT __Vectors_Size 45
Memory Allocation

Memory
__Vectors 20000600 00000000
080000ED 00000004
00000008

Flash
Reset_Handler Reset 080000ED memory
program
SystemInit System init

__main main

Heap_Mem 20000000
Heap
200001FF
Stack_Mem 20000200
Stack SRAM
200005FF
__initial_sp 20000600

46
Some stack usages
Nested calls
EXPORT __main
AREA MY_CODE, CODE, READONLY
__main
BL FUNC1
HERE B HERE

; subroutine FUNC1
FUNC1 PROC
PUSH {LR} ;store the LR on the stack
BL FUNC2 ;the func. call changes the LR
POP {LR} ;restore the LR value from the stack
BX LR
ENDP

; subroutine FUNC2
FUNC2 PROC
;do something
BX LR ; return
ENDP
END
47
Some stack usages
Preserving registers values
EXPORT __main
AREA MY_CODE, CODE, READONLY
__main PROC
BL DELAY
HERE B HERE
ENDP

DELAY PROC
PUSH {R7} ;store R7 onto the stack
LDR R7,=120000
D_1 SUBS R7,R7,#1
BNE D_1
POP {R7} ;restore R7
BX LR
ENDP
END
48

You might also like