In: Computer Science
Convert the following C++ statements to an ARM assembly language program:
const int size = 10;
int x[size] = {8, 2, 9, 6, 7, 0, 1, 3, 5, 4};
int y[size] = {399, -87, 12, 0, 42, -367, 57, 92, -1000, 25};
for i = 0; i < size; i++)
if (x([ i ] > y[ i ])
z[ i ] = 0
else
z[ i ] = 1;
Answer: There are many errors in given c++ program this is a corrected one and now I given the assembly language coding for the c++ program below.
#include <iostream>
using namespace std;
int main ()
{
const int size = 10;
int x[size] = { 8, 2, 9, 6, 7, 0, 1, 3, 5, 4 };
int y[size] = { 399, -87, 12, 0, 42, -367, 57, 92,
-1000, 25 };
{
for (int i = 0; i < size; i++)
if (x [i] > y[i])
{
int z[i] = {0};
}
else
{
int z[i] = {1};
}
}
return 0;
}
Assembly language
str fp, [sp, #-4]!
add fp, sp, #0
sub sp, sp, #12
str r0, [fp, #-8]
mov r3, #0
mov r0, r3
add sp, fp, #0
ldr fp, [sp], #4
bx lr
std::__ioinit:
.LC0:
.word 8
.word 2
.word 9
.word 6
.word 7
.word 0
.word 1
.word 3
.word 5
.word 4
.LC1:
.word 399
.word -87
.word 12
.word 0
.word 42
.word -367
.word 57
.word 92
.word -1000
.word 25
main:
push {fp, lr}
add fp, sp, #4
sub sp, sp, #104
mov r3, #10
str r3, [fp, #-8]
ldr r3, .L13
sub ip, fp, #68
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, .L13+4
sub ip, fp, #108
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, #-12]
.L11:
ldr r3, [fp, #-12]
cmp r3, #9
bgt .L4
ldr r3, [fp, #-12]
lsl r3, r3, #2
sub r2, fp, #4
add r3, r2, r3
ldr r2, [r3, #-64]
ldr r3, [fp, #-12]
lsl r3, r3, #2
sub r1, fp, #4
add r3, r1, r3
ldr r3, [r3, #-104]
cmp r2, r3
ble .L5
mov r3, sp
mov lr, r3
ldr r3, [fp, #-12]
sub ip, r3, #1
str ip, [fp, #-24]
mov r3, ip
add r3, r3, #1
mov r0, r3
mov r1, #0
mov r2, #0
mov r3, #0
lsl r3, r1, #5
orr r3, r3, r0, lsr #27
lsl r2, r0, #5
mov r3, ip
add r3, r3, #1
mov r0, r3
mov r1, #0
mov r2, #0
mov r3, #0
lsl r3, r1, #5
orr r3, r3, r0, lsr #27
lsl r2, r0, #5
mov r3, ip
add r3, r3, #1
lsl r3, r3, #2
add r3, r3, #7
lsr r3, r3, #3
lsl r3, r3, #3
sub sp, sp, r3
mov r3, sp
add r3, r3, #3
lsr r3, r3, #2
lsl r3, r3, #2
str r3, [fp, #-28]
ldr r3, [fp, #-28]
mov r2, #0
str r2, [r3]
add r2, r3, #4
sub r3, ip, #1
.L7:
cmp r3, #0
blt .L6
mov r1, #0
str r1, [r2]
add r2, r2, #4
sub r3, r3, #1
b .L7
.L6:
mov sp, lr
b .L8
.L5:
mov r3, sp
mov lr, r3
ldr r3, [fp, #-12]
sub ip, r3, #1
str ip, [fp, #-16]
mov r3, ip
add r3, r3, #1
mov r0, r3
mov r1, #0
mov r2, #0
mov r3, #0
lsl r3, r1, #5
orr r3, r3, r0, lsr #27
lsl r2, r0, #5
mov r3, ip
add r3, r3, #1
mov r0, r3
mov r1, #0
mov r2, #0
mov r3, #0
lsl r3, r1, #5
orr r3, r3, r0, lsr #27
lsl r2, r0, #5
mov r3, ip
add r3, r3, #1
lsl r3, r3, #2
add r3, r3, #7
lsr r3, r3, #3
lsl r3, r3, #3
sub sp, sp, r3
mov r3, sp
add r3, r3, #3
lsr r3, r3, #2
lsl r3, r3, #2
str r3, [fp, #-20]
ldr r3, [fp, #-20]
mov r2, #1
str r2, [r3]
add r2, r3, #4
sub r3, ip, #1
.L10:
cmp r3, #0
blt .L9
mov r1, #0
str r1, [r2]
add r2, r2, #4
sub r3, r3, #1
b .L10
.L9:
mov sp, lr
.L8:
ldr r3, [fp, #-12]
add r3, r3, #1
str r3, [fp, #-12]
b .L11
.L4:
mov r3, #0
mov r0, r3
sub sp, fp, #4
pop {fp, lr}
bx lr
.L13:
.word .LC0
.word .LC1
__static_initialization_and_destruction_0(int, int):
push {fp, lr}
add fp, sp, #4
sub sp, sp, #8
str r0, [fp, #-8]
str r1, [fp, #-12]
ldr r3, [fp, #-8]
cmp r3, #1
bne .L17
ldr r3, [fp, #-12]
ldr r2, .L18
cmp r3, r2
bne .L17
ldr r0, .L18+4
bl std::ios_base::Init::Init() [complete object constructor]
ldr r2, .L18+8
ldr r1, .L18+12
ldr r0, .L18+4
bl __aeabi_atexit
.L17:
nop
sub sp, fp, #4
pop {fp, lr}
bx lr
.L18:
.word 65535
.word std::__ioinit
.word __dso_handle
.word _ZNSt8ios_base4InitD1Ev
_GLOBAL__sub_I_main:
push {fp, lr}
add fp, sp, #4
ldr r1, .L21
mov r0, #1
bl __static_initialization_and_destruction_0(int, int)
sub sp, fp, #4
pop {fp, lr}
bx lr
.L21:
.word 65535