Reading a int typed array element with a constant index - example: A[5]
 

Definition of array variable:

   int A[10];   

Problem:

  • Fetch the value in A[5] into the register r0

Reading a int typed array element with a constant index - example: A[5]
 

Address of array element A[5]:

 

Reading a int typed array element with a constant index - example: A[5]
 

ARM instructions to fetch A[5] into register r0:

    .text

                               
   // Given that A is an int typed array
  
   // Write ARM instructions to fetch A[5] into register R0

   // Note: we can pre-compute the offset using the index 5 !



   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  // array
   

DEMO:   /home/cs255001/demo/asm/3-array/demo.s

Reading a int typed array element with a constant index - example: A[5]
 

ARM instructions to fetch A[5] into register r0:

    .text

   
   

  
    // The load instruction needed is:
       
    ldr     r0, [r1, #n]     // Loads A[5] into r0
                             // What offset n must we use ?

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  // array
   

DEMO:   /home/cs255001/demo/asm/3-array/demo.s

Reading a int typed array element with a constant index - example: A[5]
 

ARM instructions to fetch A[5] into register r0:

    .text

   

  
    // Preparation:
    //    r1 = base address of array A

    ldr     r0, [r1, #20]    // Loads A[5] into r0
                             // The offset of A[5] is 20 bytes !

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  // array
   

DEMO:   /home/cs255001/demo/asm/3-array/demo.s

Reading a int typed array element with a constant index - example: A[5]
 

ARM instructions to fetch A[5] into register r0:

    .text

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A  

    /* ----------------------------------------
       Address of A[5] = base addr + 5*4 bytes
       ---------------------------------------- */
    ldr     r0, [r1, #20]     // Load A[5] into r0


   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  // array
   

DEMO:   /home/cs255001/demo/asm/3-array/demo.s

Updating a int typed array element with a constant index - example: A[5] = 99
 

Definition of array variable:

   int A[10];   

Problem:

  • Update the value in A[5] to 99 (A[5] = 99)

Updating a int typed array element with a constant index - example: A[5] = 99
 

Address of array element A[5]:

 

Updating a int typed array element with a constant index - example: A[5] = 99
 

ARM instructions to update A[5] = 99:

    .text

  
   

    // Given that A is an int typed array

   
    // Translate:  A[5] = 99;  into ARM assembler code
      
    // Note: we can pre-compute the offset using the index 5 !


   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   

Updating a int typed array element with a constant index - example: A[5] = 99
 

ARM instructions to update A[5] = 99:

    .text

  
  

  

 
    // The store instruction that you can use:
       
    str    r0, [r1, #n]     // Write r0 (= 99) into A[5] 
                            // What offset must we use ?

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   

Updating a int typed array element with a constant index - example: A[5] = 99
 

ARM instructions to update A[5] = 99:

    .text

  
  

  
    // Preparations needed:
    //    r0 = 99
    //    r1 = base address of array A
       
    str    r0, [r1, #20]     // Write r0 (= 99) into A[5] 
                     

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   

Updating a int typed array element with a constant index - example: A[5] = 99
 

ARM instructions to update A[5] = 99:

    .text

    mov     r0, #99           // r0 = 99

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A

    /* ----------------------------------------
       Address of A[5] = base addr + 5*4 bytes
       ---------------------------------------- */
    str    r0, [r1, #20]     // Write r0 (= 99) into A[5] 


   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   

Reading a int typed array element with a variable index - example: A[i]
 

Definition of array and index variables:

   int A[10]; 
   int  i;  

Problem:

  • Fetch the value in A[i] into the register r0

Reading a int typed array element with a variable index - example: A[i]
 

Address of array element A[i]:

 

Reading a int typed array element with a variable index - example: A[i]

ARM instructions to fetch A[i] into register r0:

    .text


    // Given:
    //    int i;
    //    an int array A

    // Problem: fetch A[i] into register r0

    // Note: the offset depends on the value in i
    //       The offset can not be pre-calculated !!!
     
    

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4
   

Reading a int typed array element with a variable index - example: A[i]

ARM instructions to fetch A[i] into register r0:

    .text

    
   

    // Preparations ?
  
   
   
    // The load instruction that must be use
    // to fetch the value in A[i]:

    ldr     r0, [r1, r2]     // Load A[i] into r0

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4
   

Reading a int typed array element with a variable index - example: A[i]

ARM instructions to fetch A[i] into register r0:

    .text

    
   

    // Preparations:
    //    r1 = base address of array A
    //    r2 = offset computed using index i
   
 
   

    ldr     r0, [r1, r2]     // Load A[i] into r0

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4
   

Reading a int typed array element with a variable index - example: A[i]

ARM instructions to fetch A[i] into register r0:

    .text

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A

    // Preparation:
    //    r2 = offset computed using index i


    

    // Address of A[i] = base addr + i*4 bytes
    ldr     r0, [r1, r2]     // Load A[i] into r0

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4
   

Reading a int typed array element with a variable index - example: A[i]

ARM instructions to fetch A[i] into register r0:

    .text

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A

    // Preparation:
    //    r2 = i*4




    // Address of A[i] = base addr + i*4 bytes
    ldr     r0, [r1, r2]     // Load A[i] into r0

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4
   

Reading a int typed array element with a variable index - example: A[i]

ARM instructions to fetch A[i] into register r0:

    .text

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A

    movw    r2, #:lower16:i
    movt    r2, #:upper16:i   // r2 = address of i
    ldr     r2, [r2]          // r2 = i  (offset needed = i*4)



    // Address of A[i] = base addr + i*4 bytes
    ldr     r0, [r1, r2]     // Load A[i] into r0

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4
   

Reading a int typed array element with a variable index - example: A[i]

ARM instructions to fetch A[i] into register r0:

    .text

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A

    movw    r2, #:lower16:i
    movt    r2, #:upper16:i   // r2 = address of i
    ldr     r2, [r2]          // r2 = i  (offset needed = i*4)
    add     r2, r2, r2        // r2 = i*2


    // Address of A[i] = base addr + i*4 bytes
    ldr     r0, [r1, r2]     // Load A[i] into r0

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4
   

Reading a int typed array element with a variable index - example: A[i]

ARM instructions to fetch A[i] into register r0:

    .text

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A

    movw    r2, #:lower16:i
    movt    r2, #:upper16:i   // r2 = address of i
    ldr     r2, [r2]          // r2 = i  (offset needed = i*4)
    add     r2, r2, r2        // r2 = i*2
    add     r2, r2, r2        // r2 = i*4

    // Address of A[i] = base addr + i*4 bytes
    ldr     r0, [r1, r2]     // Load A[i] into r0

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4
   

Reading a int typed array element with a variable index - example: A[i]

Alternative solution: (no saving in # instructions)

    .text

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A

    movw    r2, #:lower16:i
    movt    r2, #:upper16:i   // r2 = address of i
    ldr     r2, [r2]          // r2 = i  (offset needed = i*4)
    mov     r3, #4    
    mul     r2, r3, r2        // r2 = i*4

    // Address of A[i] = base addr + i*4 bytes
    ldr     r0, [r1, r2]     // Load A[i] into r0

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4
   

Updating a int typed array element with a variable index - example: A[i] = 99
 

Definition of array and index variables:

   int A[10];  
   int  i; 

Problem:

  • Update the value in A[i] to 99 (A[i] = 99)

Updating a int typed array element with a variable index - example: A[i] = 99
 

Address of array element A[i]:

 

Updating a int typed array element with a variable index - example: A[i] = 99

ARM instructions to update A[i] = 99:

    .text


 
    // Given:
    //    int i;
    //    an int array A

    // Problem: translate A[i] = 99 into ARM assembler code

    // Note: the offset depends on the value in i
    //       The offset can not be pre-calculated !!!




   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4 

Updating a int typed array element with a variable index - example: A[i] = 99

ARM instructions to update A[i] = 99:

    .text


    



    // Preparations ?
    
    

    // The store instruction that must be use
    // to write the value to A[i]:
     
    str    r0, [r1, r2]     // Write r0 (= 99) into A[i] 

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4  

Updating a int typed array element with a variable index - example: A[i] = 99

ARM instructions to update A[i] = 99:

    .text






    // Preparations ?
    //    r0 = 99
    //    r1 = base address of array A
    //    r2 = offset computed using index i
    

     
    str    r0, [r1, r2]     // Write r0 (= 99) into A[i] 

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4  

Updating a int typed array element with a variable index - example: A[i] = 99

ARM instructions to update A[i] = 99:

    .text

    movw    r0, #99           // r0 = 99





    // Preparations ?
    //    r1 = base address of array A
    //    r2 = offset computed using index i
    

     
    str    r0, [r1, r2]     // Write r0 (= 99) into A[i] 

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4  

Updating a int typed array element with a variable index - example: A[i] = 99

ARM instructions to update A[i] = 99:

    .text

    movw    r0, #99           // r0 = 99

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A


    // Preparations ?
    //    r2 = offset computed using index i
    


    // Address of A[i] = base addr + i*4 bytes
    str    r0, [r1, r2]     // Write r0 (= 99) into A[i] 

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4  

Updating a int typed array element with a variable index - example: A[i] = 99

ARM instructions to update A[i] = 99:

    .text

    movw    r0, #99           // r0 = 99

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A


    // Preparations ?
    //    r2 = i*4 (offset)
    


    // Address of A[i] = base addr + i*4 bytes
    str    r0, [r1, r2]     // Write r0 (= 99) into A[i] 

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4  

Updating a int typed array element with a variable index - example: A[i] = 99

ARM instructions to update A[i] = 99:

    .text

    movw    r0, #99           // r0 = 99

    movw    r1, #:lower16:A
    movt    r1, #:upper16:A   // r1 = base address of array A

    movw    r2, #:lower16:i
    movt    r2, #:upper16:i   // r2 = address of i
    ldr     r2, [r2]          // r2 = i
    add     r2, r2, r2        // r2 = i*2
    add     r2, r2, r2        // r2 = i*4

    // Address of A[i] = base addr + i*4 bytes 
    str    r0, [r1, r2]     // Write r0 (= 99) into A[i] 

   .data
A: .4byte   1, 2, 3, 4, 5, 6, 7, 8, 9, 10  //  array
   .align  2
i: .4byte  4