Definition of array variable:
short A[10]; |
Problem:
|
Address of array element 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
Definition of array variable:
short A[10]; |
Problem:
|
Address of array element A[5]:
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 |
Definition of array and index variables:
short A[10]; int i; |
Problem:
|
Address of array element 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 |
Definition of array and index variables:
short A[10]; int i; |
Problem:
|
Address of array element A[i]:
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 |