Example: passing parameters and return values using register
 

public class Prog {
   /* ----------------------------------------------
      sumSquares(a,b):  returns  (a*a + b*b)
      ---------------------------------------------- */         
   public static int sumSquares( int a, int b )           
   {
      return (a*a + b*b);
   }

   public static void main( )
   {
      int x, y, z;

      x = 4; y = 7;
      z = sumSquares(x, y);
   }
}
   

How we will pass the parameters and return values
 

How do we write the main( ) function

 main:                         sumSquares:

     // Pass x in r0                // Compute x*x+y*y 
                                    // where x = r0 and y = r1     


     // Pass y in r1

                                    // Return result in r0

     bl sumSquares                  mov pc, lr

     // Store return value
     // in z                         



Stop:                     
   

How do we write the main( ) function

 main:                         sumSquares:

     movw r0, #:lower16:x           // Compute x*x+y*y 
     movt r0, #:upper16:x           // where x = r0 and y = r1     
     ldr  r0, [r0]

     // Pass y in r1                     
                              
                                    // Return result in r0

     bl sumSquares                  mov pc, lr

     // Store return value
     // in z                         



Stop:                     
   

How do we write the main( ) function

 main:                         sumSquares:

     movw r0, #:lower16:x           // Compute x*x+y*y 
     movt r0, #:upper16:x           // where x = r0 and y = r1     
     ldr  r0, [r0]

     movw r1, #:lower16:y
     movt r1, #:lower16:y
     ldr  r1, [r1]                  // Return result in r0

     bl sumSquares                  mov pc, lr

     // Store return value
     // in z                         



Stop:                     
   

How do we write the main( ) function

 main:                         sumSquares:

     movw r0, #:lower16:x           // Compute x*x+y*y 
     movt r0, #:upper16:x           // where x = r0 and y = r1     
     ldr  r0, [r0]

     movw r1, #:lower16:y
     movt r1, #:lower16:y
     ldr  r1, [r1]                  // Return result in r0

     bl sumSquares                  mov pc, lr

     movw r1, #:lower16:z
     movt r1, #:upper16:z                          
     str  r0, [r1]


Stop:                     
   

How do we write the sumSquares( ) function

 main:                         sumSquares:

     movw r0, #:lower16:x           // Compute x*x+y*y 
     movt r0, #:upper16:x           // where x = r0 and y = r1     
     ldr  r0, [r0]

     movw r1, #:lower16:y
     movt r1, #:lower16:y
     ldr  r1, [r1]                  // Return result in r0

     bl sumSquares                  mov pc, lr

     movw r1, #:lower16:z
     movt r1, #:upper16:z                          
     str  r0, [r1]


Stop:                     
   

How do we write the sumSquares( ) function

 main:                         sumSquares:

     movw r0, #:lower16:x           // Compute x*x+y*y 
     movt r0, #:upper16:x           // where x = r0 and y = r1 
     ldr  r0, [r0]                  mul r2, r0, r0 // r2 = x*x
                                    mul r3, r1, r1 // r3 = y*y
     movw r1, #:lower16:y           add r2, r2, r3 // r2 = x*x+y*y 
     movt r1, #:lower16:y
     ldr  r1, [r1]                  // Return result in r0

     bl sumSquares                  mov pc, lr

     movw r1, #:lower16:z
     movt r1, #:upper16:z                          
     str  r0, [r1]


Stop:                     
   

How do we write the sumSquares( ) function

 main:                         sumSquares:

     movw r0, #:lower16:x           // Compute x*x+y*y 
     movt r0, #:upper16:x           // where x = r0 and y = r1 
     ldr  r0, [r0]                  mul r2, r0, r0 // r2 = x*x
                                    mul r3, r1, r1 // r3 = y*y
     movw r1, #:lower16:y           add r2, r2, r3 // r2 = x*x+y*y 
     movt r1, #:lower16:y
     ldr  r1, [r1]                  mov r0, r2

     bl sumSquares                  mov pc, lr

     movw r1, #:lower16:z
     movt r1, #:upper16:z                          
     str  r0, [r1]


Stop:                     
   

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