Calling a subroutine: the branch-and-link (bl) assembler instruction

Syntax and meaning (= effect) of the branch-and-link instruction:

  bl  label       

  Effect:
        (1) Save the return address (= location) into 
            the Link Register (LR)

	(2) Branch to memory location marked by 
            the label "label"  
   

Situation before the execution of the   bl label instruction:

Calling a subroutine: the branch-and-link (bl) assembler instruction

Syntax and meaning (= effect) of the branch-and-link instruction:

  bl  label       

  Effect:
        (1) Save the return address (= location) into 
            the Link Register (LR)

	(2) Branch to memory location marked by 
            the label "label"  
   

Situation after   the execution of the   bl label instruction:

How the bl instruction is executed

Initial state:   the CPU fetches the bl 4000 (= label) instruction at address 44:

Note:   the return address = 48 because each ARM instruction is 4 bytes long.

How the bl instruction is executed

The CPU sends the READ command on the system bus:

 

How the bl instruction is executed

The memory returns the bl 4000 instruction to the CPU:

 

How the bl instruction is executed

The bl 4000 instruction is stored in the Instruction Register:

 

How the bl instruction is executed

The Program Counter is incremented:

Notice that the Program Counter contains the return address !!!

How the bl instruction is executed

State of the CPU when it executes the   bl 4000 instruction:

 

How the bl instruction is executed

The bl instruction simulataneously updates: LR = PC and PC = 4000:

 

How the bl instruction is executed

Result of executing   bl 4000 (= label):

  1. The execution of the function at 4000 (= label) begins (because PC = 4000)
  2. The return address to the caller has been saved in the LR register

Illustrating the effect of the bl (branch and link) instruction

Illsutrating the effects of the bl instruction:

main:
        mov     r0, #1111
        mov     r1, #1111
        bl      myMethod                         
        mov     r2, #2222
        mov     r3, #2222


myMethod:
        mov     r8, #4444
        mov     r9, #4444   

DEMO:   /home/cs255001/demo/asm/8-sub/bl.s

(Next slide give you hints on what to look out for)

Illustrating the effect of the bl (branch and link) instruction

Illsutrating the effects of the bl instruction:

main:
        mov     r0, #1111
        mov     r1, #1111
        bl      myMethod                         
        mov     r2, #2222
        mov     r3, #2222


myMethod:
        mov     r8, #4444
        mov     r9, #4444 

When the CPU executes bl myMethod, look for:

  1. PC = address of the myMethod label

             I.e.: the program made a jump to the myMethod function

  2. LR register contains: the return address

             (= the address of the mov r2, #2222 instruction)