Definition of array variable:
short A[10]; |
Problem:
|
Address of array element A[5]:
ARM instructions to fetch A[5] into register r0:
.text
// Given that A is a short 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: .2byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // array
|
DEMO: /home/cs255001/demo/asm/3-array/demo.s
ARM instructions to fetch A[5] into register r0:
.text
// The load instruction needed is:
ldrsh r0, [r1, #n] // Loads A[5] into r0
// What offset n must we use ?
.data
A: .2byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // array
|
DEMO: /home/cs255001/demo/asm/3-array/demo.s
ARM instructions to fetch A[5] into register r0:
.text
// Preparation:
// r1 = base address of array A
ldrsh r0, [r1, #10] // Loads A[5] into r0
// The offset of A[5] is 10 bytes !
.data
A: .2byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // array
|
DEMO: /home/cs255001/demo/asm/3-array/demo.s
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
// Given that A is a short typed array
// Translate: A[5] = 99; into ARM assembler code
// Note: we can pre-compute the offset using the index 5 !
.data
A: .2byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // array
|
ARM instructions to update A[5] = 99:
.text
// The store instruction that you can use:
strh r0, [r1, #n] // Write r0 (= 99) into A[5]
// What offset must we use ?
.data
A: .2byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // array
|
ARM instructions to update A[5] = 99:
.text
// Preparations needed:
// r0 = 99
// r1 = base address of array A
strh r0, [r1, #10] // Write r0 (= 99) into A[5]
.data
A: .2byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // array
|
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*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
// Given:
// int i;
// a short 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: .2byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // array
.align 2
i: .4byte 4
|
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]: 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 |
ARM instructions to fetch A[i] into register r0:
.text // Preparations: // r1 = base address of array A // r2 = offset computed using index i 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 |
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*2 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
|
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*2 /* ---------------------------------------- Address of A[i] = base addr + i*2 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 |
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*2) /* ---------------------------------------- Address of A[i] = base addr + i*2 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 |
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*2) add r2, r2, r2 // r2 = i*2 /* ---------------------------------------- Address of A[i] = base addr + i*2 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 |
Alternate solution:
.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*2) mov r3, #2 mul r2, r3, r2 // r2 = i*2 // Address of A[i] = base addr + i*2 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
// Given:
// int i;
// a short 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: .2byte 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 // array
.align 2
i: .4byte 4
|
ARM instructions to update A[i] = 99:
.text // Preparations ? // The store instruction that must be use // to write the value to A[i]: 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 |
ARM instructions to update A[i] = 99:
.text // Preparations ? // r0 = 99 // r1 = base address of array A // r2 = offset computed using index i 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 |
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 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 |
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*2 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
|
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*2 (offset) /* ---------------------------------------- Address of A[i] = base addr + i*2 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 |
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 /* ---------------------------------------- Address of A[i] = base addr + i*2 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 |