MIPS - help converting code from C
I am a beginner in MIPS and I am trying to write a simple code that runs over a given array in memory that is smaller than 10 cells, lets say 9 cells, and prints on screen the biggest number.
I wrote a C code that solves this issue, but I don't know how to convert it (without mips gcc) to a working MIPS assembly code.
The code I wrote:
int N = 9 , i = 0 , biggest = 0 ;
int arr [N] = -2 , 3 , 9 , -1 , 5 , 6 , 10 , 52 , 9 ;
while ( i <= N )
{
if ( arr [i] > biggest )
biggest 开发者_如何学Go= arr [i] ;
i++ ;
}
printf ( "biggest number is: %d" , biggest ) ;
I will be more than happy if someone can write that code in MIPS assembly, and explain it to me.
Thank you !
Just focusing on the loop, try something like that:
.text
.set noreorder
.global get_max
get_max:
li $4, array // start pointer
li $5, array_end-array-4 // end pointer
li $2, 0 // 'biggest' as result
lw $6, 0($4) // load first table entry
1: slt $3, $2, $6 // boolean flag (biggest<arr[i])
movn $2, $6, $3 // update 'biggest' when flag is set
lw $6, 4($4) // load next table entry
bne $4, $5, 1b // continue until we hit end of array
addiu $4, 4 // advance to next cell (using bne delay slot)
jr $31 // return to the caller
nop // safely fill the delay slot
.data
array: .long -2 , 3 , 9 , -1 , 5 , 6 , 10 , 52 , 9
array_end: .long 0
Compile this into a separate assembly source file and link with your main C code.
Don't forget to call the function from your C code:
printf("biggest=%d\n",get_max());
You have a problem with your initialization...
精彩评论