I need to write an assembly program finding the maximum of x^3 -14x^2 +56x -64 in the range -2<=x<=8 and the maximum value should be in one of the registers at the end of the program.
I wrote the code in C, but I'm not sure how to convert it myself into Assembly. I'm really confused at this point. BTW I have to convert it myself. I can't use gcc to convert to assembly. This is for SPARC
#include<stdio.h>
int main()
{
int i;
int ans;
for (i = -2; i < 9; i++){
ans = (i * i * i) - (14 * i * i) + (56 * i) - 64;
}
}
I have attempted to write the assembly. Can someone critique it and tell me if I'm in the right direction. Also, how would I go about testing for the max number?
main:
save %sp, -96, %sp
ba test
mov -2, %a_r
loop:
mov %a_r, %o0 !a_r moved into o0
mov %a_r, %o1 !a_r moved into o1
call .mul !they are multiplied and stored in o0
call .mul !they are multiplied again and stored in o0
mov %o0, r0 !results stored in r0
mov %a_r, %o0 !a_r moved into o0 and o1
mov %a_r, %o1
call .mul !they are multiplied and stored in o0
mov 14, %o1
call .mul !o0 result is multiplied by 14 and stored in r1
mov %o0, r1
mov 56, %o0 !56 moved into o0
mov %a_r, %01 !a_r moved into o1
call .mul !they are multiplied and stored in r2
mov %o0, r2
Sub r0,r1,r0 !r0-r1 stored in r0
Add r0,r2,r0 !r0+r2 stored in r0
Sub r0,64,r0 !r0-64 stored in r0
add %a_r, 1, %a_r !a_r + 1
test:
cmp %a_r %b_r ! a_r<=8?
ble loop