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

Definition of array variable:

   short A[10];   

Problem:

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

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

Address of array element A[5]:

 

Reading a short 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*2 bytes
       ---------------------------------------- */
    ldrsh   r0, [r1, #10]     // Load A[5] into r0


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

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

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

Definition of array variable:

   short A[10];   

Problem:

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

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

Address of array element A[5]:

 

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

ARM instructions to update A[5] = 99:

    .text

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

    movw    r0, #99           // r0 = 99

    /* ----------------------------------------
       Address of A[5] = base addr + 5*2 bytes
       ---------------------------------------- */
    strh   r0, [r1, #10]     // Write r0 (= 99) into A[5] 


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

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

Definition of array and index variables:

   short A[10]; 
   int   i;  

Problem:

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

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

Address of array element A[i]:

 

Reading a short 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
    add     r2, r2, r2        // r2 = i+i = 2*i

    /* ----------------------------------------
       Address of A[i] = base addr + 2*i bytes
       ---------------------------------------- */
    ldrsh   r0, [r1, r2]     // Load A[i] into r0

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

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

Definition of array and index variables:

   short A[10];  
   int   i; 

Problem:

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

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

Address of array element A[i]:

 

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

ARM instructions to update A[i] - 99:

    .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
    add     r2, r2, r2        // r2 = i+i = 2*i

    movw    r0, #99           // r0 = 99

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

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