In: Computer Science
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored in memory with base addresses at 1000, 2000 and 3000 respectively.
int absoluteDifference(int x, int y) {
int r;
if (x > y)
r = x - y;
else
r = y - x;
return r;
}
int main() {
for (int i=0; i < 10; i++) {
int a = array1[i];
int b = array2[i];
int c = absoluteDifference(a, b);
array3[i] = c;
}
}
LEGV8 CODE:
=======================================================================================
absoluteDifference:
str fp, [sp, #-4]!
add fp, sp, #0
sub sp, sp, #20
str r0, [fp, #-16]
str r1, [fp, #-20]
ldr r2, [fp, #-16]
ldr r3, [fp, #-20]
cmp r2, r3
ble .L2
ldr r2, [fp, #-16]
ldr r3, [fp, #-20]
sub r3, r2, r3
str r3, [fp, #-8]
b .L3
.L2:
ldr r2, [fp, #-20]
ldr r3, [fp, #-16]
sub r3, r2, r3
str r3, [fp, #-8]
.L3:
ldr r3, [fp, #-8]
mov r0, r3
add sp, fp, #0
ldr fp, [sp], #4
bx lr
.LC0:
.word 1
.word 2
.word 3
.word 4
.word 5
.word 6
.word 7
.word 8
.word 9
.word 0
main:
push {fp, lr}
add fp, sp, #4
sub sp, sp, #136
ldr r3, .L9
sub ip, fp, #60
mov lr, r3
ldmia lr!, {r0, r1, r2, r3}
stmia ip!, {r0, r1, r2, r3}
ldmia lr!, {r0, r1, r2, r3}
stmia ip!, {r0, r1, r2, r3}
ldm lr, {r0, r1}
stm ip, {r0, r1}
ldr r3, .L9
sub ip, fp, #100
mov lr, r3
ldmia lr!, {r0, r1, r2, r3}
stmia ip!, {r0, r1, r2, r3}
ldmia lr!, {r0, r1, r2, r3}
stmia ip!, {r0, r1, r2, r3}
ldm lr, {r0, r1}
stm ip, {r0, r1}
ldr r3, .L9
sub ip, fp, #140
mov lr, r3
ldmia lr!, {r0, r1, r2, r3}
stmia ip!, {r0, r1, r2, r3}
ldmia lr!, {r0, r1, r2, r3}
stmia ip!, {r0, r1, r2, r3}
ldm lr, {r0, r1}
stm ip, {r0, r1}
mov r3, #0
str r3, [fp, #-8]
b .L6
.L7:
ldr r3, [fp, #-8]
lsl r3, r3, #2
sub r2, fp, #4
add r3, r2, r3
ldr r3, [r3, #-56]
str r3, [fp, #-12]
ldr r3, [fp, #-8]
lsl r3, r3, #2
sub r2, fp, #4
add r3, r2, r3
ldr r3, [r3, #-96]
str r3, [fp, #-16]
ldr r1, [fp, #-16]
ldr r0, [fp, #-12]
bl absoluteDifference
str r0, [fp, #-20]
ldr r3, [fp, #-8]
lsl r3, r3, #2
sub r2, fp, #4
add r3, r2, r3
ldr r2, [fp, #-20]
str r2, [r3, #-136]
ldr r3, [fp, #-8]
add r3, r3, #1
str r3, [fp, #-8]
.L6:
ldr r3, [fp, #-8]
cmp r3, #9
ble .L7
mov r3, #0
mov r0, r3
sub sp, fp, #4
pop {fp, pc}
.L9:
.word .LC0
=======================================================================================