Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help on converting C to Mipsr2000

Status
Not open for further replies.

rabbitonastick

Technical User
May 30, 2007
2
US
Hi my problem is I took C quite a while ago and forgot most of the syntax. FOr my hw i have to convert C code to assembly. The description of the code says it computes the maximum of an array of integers. But the program does some strange thing im not familiar with rather than a For loop.

Quite honestly, i have no idea how to convert this. I have some general idea. I know im suppose to use the stack frame and something about a calling function.


#include <stdio.h>
int max(int * arr, int l, int r);
// main calling program
int main(int argc, char *argv[]) {
// define stuff to pass
int a[10] = {3, 1, 5, 9, 2, 8, 0, 4, 7, 6};
int l = 0;
int r = 9;
int max_val;
max_val = max(a, l, r);
printf("max value = %d\n", max_val);
return 0;
}
// max procedure
int max(int * a, int l, int r) {
printf("call max(%d, %d)\n", l, r);
// define variables
int u = 0; // upper max
int v = 0; // lower max
int m = (l + r) / 2; // current division point
// check if pointers have run into eachother
if(l == r) {
printf("return a[%d] = %d\n", l, *(a + l));
return(*(a + l));
}
// divide and conquer!
u = max(a, l, m);
v = max(a, m+1, r);
// done when get here
if (u > v) {
printf("return u = %d\n", u);
return u;
}
else {
printf("return v = %d\n", v);
return v;
}
}
 
btw, if someone could point me in the right direct, that would be great!
 
If this is homework, you should do it yourself; you can get assembly code out of any C compiler. But you are thinking in the right lines looking at that for-loop. This must be the most dreadful method I have ever seen to find the maximum in an array, and I can only assume that it was set as an exercise in translation of dreadful algorithms. No one in their right mind would write in assembler like this. Assembler is usually written where you need either speed, or short, efficient code. Nearly always, recursion is dreadful on both counts. Stack frames cost time and space to set up.

There are good chapters in all assembly text-books on stack-frames and calling conventions (try any chapter on interfacing with C or other high-level languages). I'd try reading them before going any further. Good luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top