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:
        /* -------------------------------------------------
           Pass parameter x by copying its value in reg r0
           ------------------------------------------------- */
        movw    r0, #:lower16:x
        movt    r0, #:upper16:x
        ldr     r0, [r0]

        /* -------------------------------------------------
           Pass parameter y by copying its value in reg r1
           ------------------------------------------------- */
        movw    r1, #:lower16:y
        movt    r1, #:upper16:y
        ldr     r1, [r1]

        /* ---------------------------------------------------------
           call z = sumSquares(x,y):
                agreed inputs:  r0 = x, r1 = y

           
           --------------------------------------------------------- */ 
        bl      sumSquares          // Call sumSquares 

        /* ----------------------------------------------------------
           Agreed return location:  r0 = return value

           

           Save return value (in r0) to z (z = sumSquares(..))
           --------------------------------------------------- */
        movw    r1, #:lower16:z    // Do NOT use r0 !!!
        movt    r1, #:upper16:z    // (r0 contains the return value)

        str     r0, [r1]           // This will store return value in z
   

How do we write the sumSquares( ) function

/* ----------------------------------------------------------------
   Function sumSquares(a,b):
        agreed inputs:  r0 = a, r1 = b
        agreed return:  r0 = return value
   ---------------------------------------------------------------- */

sumSquares:

        // When  sumSquares begin, we will have: r0 = a, r1 = b

        
        mul     r2, r0, r0      // r2 = a*a (use r0 as var a)
        mul     r3, r1, r1      // r3 = b*b (use r1 as var b)


        
        add     r0, r2, r3      // r0 = a*a + b*b
                                // The return value is now in r0

        mov     pc, lr          // Return to the caller
   

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