0

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
Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
user2012751
  • 153
  • 2
  • 2
  • 9

1 Answers1

1

Your answer is a lacking a little detail because we have no information about which machine you are targeting. This also sounds like homework, so I will not give you a complete answer.

What I will tell you, though, is that to implement loops in assembly (any kind) you need to use labels and branch instructions.

Aside from that it is a simple sums and multiplications. Just take care of storing i and ans in two different registers (they are different variables, after all).

EDIT

It seems you are in the right direction (I have never written for SPARC, but a quick glance at the docs tells me you have the right idea). About the maximum number: how would you go about it in C/C++? Think about it and do the same assembly! :)

NeXuS
  • 1,797
  • 1
  • 13
  • 13