rabbitonastick
Technical User
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;
}
}
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;
}
}