Returning from a subroutine
 

Returning from a subroutine is simply jump/branch to the return address:

        function f1( )               function f2( )  // Subroutine 
        {                   +------->{         
            ...             |            ...  |
            ...             |            ...  |
            ...       call  |            ...  |
            f2( ) ----------+            ...  |
 (Ret Addr) ...   <--------------+       ...  |
            ...                  |       ...  V 
            ...                  +------ return;   // Branch to the return address
        }                             } 

Translate the function call f2( ) in ARM assembler:

  • Use the bl instruction to:

    • Branch to function f2

    • Save the return address (Ret Addr) in the LR register

Returning from a subroutine
 

Quiz:   what instruction will cause function f2( ) to branch to the return address (Ret Addr):

        f1:                          f2:             // Subroutine 
            ...             +------->    ...         
            ...             |            ...  |      LR = (Ret Addr)
            ...             |            ...  |
            ...       call  |            ...  |
            bl f2 ----------+            ...  |
 (Ret Addr) ...   <--------------+       ...  |
            ...                  |       ...  V 
            ...                  +------ return;   // Branch to the return address
        }                             } 

Hints:

  • The return address (Ret Addr) is stored in register LR

  • To make a jump to some address:

    • Update the Program Counter (PC) to that address !

Returning from a subroutine
 

Answer:   mov pc, lr !!!

        f1:                          f2:             // Subroutine 
            ...             +------->    ...         
            ...             |            ...  |      LR = (Ret Addr)
            ...             |            ...  |
            ...       call  |            ...  |
            bl f2 ----------+            ...  |
 (Ret Addr) ...   <--------------+       ...  |
            ...                  |       ...  V 
            ...                  +------ mov pc,lr // Branch to the return address
        }                             } 

Hints:

  • The return address (Ret Addr) is stored in register LR

  • To make a jump to some address:

    • Update the Program Counter (PC) to that address !

Returning from a leaf subroutine - Example
 

Function call expressed in a high level programming language:

   main( )                   void myMethod( )
   {                         {
      ...                       ...  
      myMethod( );              ...  
      ...                       ...  
      ...                    }       
      ...	          	     
      myMethod( );            
      ...                
      ...                     
   }                          

Returning from a leaf subroutine - Example
 

How function calls are implemented in assembler:

   main:                     myMethod:
         ...                       ...
         ...                       ...         
         bl  myMethod              ...
         ...                       ...
         ...                       mov  pc, lr
         ...
         bl  myMethod
         ...            
         ...           
 

In the next few slides, I will show a concrete example (using concrete memory addresses how the bl instruction and the mov pc, lr instruction together implements the function call + return mechanis

Returning from a leaf subroutine - Example
 

Let's execute the program starting at main: using some specific return addresses:

   main:                     myMethod:
   PC--> ...                       ...
         ...                       ...         
         bl  myMethod              ...
   8000: ...                       ...
         ...                       mov  pc, lr
         ...
         bl  myMethod
   9000: ...            
         ...           
 

LR = ...

 

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

Returning from a leaf subroutine - Example
 

The main program will reach the first   bl myMethod instruction:

   main:                     myMethod:
         ...                       ...
         ...                       ...         
   PC--> bl  myMethod              ...
   8000: ...                       ...
         ...                       mov  pc, lr
         ...
         bl  myMethod
   9000: ...            
         ...           
 

LR = ...

 

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

Returning from a leaf subroutine - Example
 

The bl myMethod instruction will save 8000 in the LR register and branch to myMethod:

   main:                     myMethod:
         ...                 PC--> ...
         ...                       ...         
         bl  myMethod              ...
   8000: ...                       ...
         ...                       mov  pc, lr
         ...
         bl  myMethod
   9000: ...            
         ...           
 

LR = 8000  

 

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

Returning from a leaf subroutine - Example
 

The myMethod execution will reach the instruction mov pc, lr:

   main:                     myMethod:
         ...                       ...
         ...                       ...         
         bl  myMethod              ...
   8000: ...                       ...
         ...                 PC--> mov  pc, lr
         ...
         bl  myMethod
   9000: ...            
         ...           
 

LR = 8000     <---- mov pc, lr will branch to address 8000 !!!

 

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

Returning from a leaf subroutine - Example
 

Result of executing the mov pc, lr instruction:

   main:                     myMethod:
         ...                       ...
         ...                       ...         
         bl  myMethod              ...
   PC--> ...                       ...
         ...                       mov  pc, lr
         ...
         bl  myMethod
   9000: ...            
         ...           
 

LR = 8000  

 

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

Returning from a leaf subroutine - Example
 

The main program will reach the second   bl myMethod instruction:

   main:                     myMethod:
         ...                       ...
         ...                       ...         
         bl  myMethod              ...
   8000: ...                       ...
         ...                       mov  pc, lr
         ...
   PC--> bl  myMethod
   9000: ...            
         ...           
 

LR = 8000  

 

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

Returning from a leaf subroutine - Example
 

The bl myMethod instruction will now save 9000 in the LR register and branch to myMethod:

   main:                     myMethod:
         ...                 PC--> ...
         ...                       ...         
         bl  myMethod              ...
   8000: ...                       ...
         ...                       mov  pc, lr
         ...
         bl  myMethod
   9000: ...            
         ...           
 

LR = 9000  

 

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

Returning from a leaf subroutine - Example
 

The myMethod execution will reach the instruction mov pc, lr:

   main:                     myMethod:
         ...                       ...
         ...                       ...         
         bl  myMethod              ...
   8000: ...                       ...
         ...                 PC--> mov  pc, lr
         ...
         bl  myMethod
   9000: ...            
         ...           
 

LR = 9000     <---- mov pc, lr will branch to address 9000 !!!

 

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

Returning from a leaf subroutine - Example
 

Result of executing the mov pc, lr instruction:

   main:                     myMethod:
         ...                       ...
         ...                       ...         
         bl  myMethod              ...
   8000: ...                       ...
         ...                       mov  pc, lr
         ...
         bl  myMethod
   PC--> ...            
         ...           
 

LR = 9000        --- Demo time....

 

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