I have the task of converting this code into assembly:
int prodArray (int A[], int n) {
if (!n)
return 1;
else if (n == 1)
return A[0];
int mid = n / 2;
int left = prodArray(A, mid);
int right = prodArray(A + mid, n - mid);
return left * right;
}
Here is what I have so far, and note that loop, loop2, and loop3 are pretty much the same:
main:
add $t0, $0, $0 # Initialize t0
add $t1, $0, $0 # Initialize t1
addi $t4, $0, 1 # Make t2 equal to 1
# Main Loop
loop:
beq $a1, $t2, exit # If the second argument is 0 branch to exit
beq $a1, $0, exit2 # If the second argument is 1 branch to exit2
srl $t2, $a1, 1 # Divide n by 2
sll $t3, $a1, 1 # Multiply n by 2
add $a1, $t2, $0 # make n half
b loop2 # branch to loop
loop2:
beq $a1, $t2, exit
beq $a1, $0, exit2
srl $t2, $a1, 1
sll $t3, $a1, 1
add $a0, $a0, $t2 # Make a0 point to A + mid
b loop3
loop3:
beq $a1, $t2, exit
beq $a1, $0, exit2
srl $t2, $a1, 1
sll $t3, $a1, 1
add $a1, $t2, $0
b loop
# exit
exit:
ja $ra, $t4
exit2:
ja $ra, 0(a1)
I'm not very good at assembly and need any assistance I can get. Note that a0 is meant to be the first argument and a1 is meant to be the second argument.
Thanks for any guidance you're able to give!