int i ;
void set_array(int num){
int array[10];
for(i=0;i<10;i++){
array[i]=compare(num,i){
}
}
int compare(int a ,int b){
if(sub(a,b)>=0)
return 1;
else
return 0;
}开发者_如何学Python
int sub(int a,int b){
return a-b;
}
anybody know how to convert to mips code
First of all, you need a cheat sheet: MIPS Instruction Reference
A good reference sheet is the most useful thing possible when writing assembly for any architecture.
Secondly, start breaking the program down on the high level. You know the functions and their names, so start there. Start by defining the functions and creating a very basic definition for each function so that it is actually callable.
Next, write the code that will pull the function parameters from their respective places for each function. You are lucky here as each function just accepts a basic integer. You will need to move any function parameters into $a0
- $a3
before calling the function. You can then write the code that moves your return value into $v0
or $v1
before returning. Make sure in each function you write, you save off the registers you are using onto the stack and then restore them before your jal $ra
. Now you should have some functions that are callable and return values, but don't really do anything.
Lastly, actually write the junk that your functions do. Again, you are luck as you are only using int
... so each number will match to one register and you will only need to offset by 4 bytes when referencing the stack... making things easier. I'll give you a hint for int sub(int a, int b)
... it'll look something like this:
sub $t2, $t1, $t0
Haha, see? It's not so hard to write assembly :D
Of course you will then have to move your result into $v0' or
$v1` at the end of the function and jump back to the callee.
Anyways, hope that gets you started.
精彩评论