In: Computer Science
if I need to get the sum of 6 numbers, here is a C programming solution
int getSum(int number1, int number2, int number3, int number4, int number5, int number6);
and the function is:
int SUM;
sum = number1+number2+number3+number4+number5+number6;
return SUM;
this function is called in main, and the main looks like:
int totalSum;
totalSum=getSum(1,2,3,4,5,6);
while(1);
so how can I write this C programming code in ARM assembly code, can you please write the comment of each line so I can understand the code better, we only need to implement the function getSum(), which is called from the c code in main(). Please add some c code that is needed to call the function getSum().
The given C program is:
int getSum(int number1, int number2, int number3, int number4, int number5, int number6) {
int SUM;
SUM=number1+number2+number3+number4+number5+number6;
return SUM;
}
main()
{
int totalSum;
totalSum=getSum(1,2,3,4,5,6);
}
The corresponding code in arm (arm64 gcc 6.4) assembly language is given below:
getSum:
sub sp, sp, #48 // stack space allocated
str w0, [sp, 28] // stores number 1
str w1, [sp, 24] // stores number 2
str w2, [sp, 20] // stores number 3
str w3, [sp, 16] // stores number 4
str w4, [sp, 12] // stores number 5
str w5, [sp, 8] // stores number 6
ldr w1, [sp, 28] // load number 1
ldr w0, [sp, 24] // load number 2
add w1, w1, w0 // add numbers
ldr w0, [sp, 20] // load number 3
add w1, w1, w0 // add numbers
ldr w0, [sp, 16] // load number 4
add w1, w1, w0 // add numbers
ldr w0, [sp, 12] // load number 4
add w1, w1, w0 // add numbers
ldr w0, [sp, 8] // load number 5
add w0, w1, w0 // add numbers
str w0, [sp, 44]
ldr w0, [sp, 44] // load number 6
add sp, sp, 48 //add numbers
ret
main:
stp x29, x30, [sp, -32]!
add x29, sp, 0
mov w5, 6 //load 6
mov w4, 5 //load 5
mov w3, 4 //load 4
mov w2, 3 //load 3
mov w1, 2 //load 2
mov w0, 1 //load 1
bl getSum //call getSum()
str w0, [x29, 28] // final result
mov w0, 0
ldp x29, x30, [sp], 32
ret