Here is some stuff to think about.
Calls to printf: Those involve calling the system call write. Depending on what you want to do, you have 2 choices
- Call printf function
- Call write system call via syscall instruction.
compiler generated code and @pierr shows will call printf.
IF you want to work in mips in a "more real than spim or mars" environment, I recommend gxemul. You can simulate a netbsd running on mips.
Inside there, you have gcc, gdb, the works.
So, getting the assembly code of your main function (i.e not objdump) is really easy.
$gcc -S -mrnames main.c
The rest, is quite simple. As you are not asking for a mips to C translation, i'm not going to write the mips code, but will give you some tips.
int i=0;
Ok, this is a WORD, with the value 0x0. In mips, you have a special register ($0) called $zero,which is hardcoded to the 0 value. So to load a variable with 0x0, you have to alternatives.
mv $t1,$zero
or
li $t1,0
(there are more, such as subu $t1,t1,t1, but that's reinventing the wheel)
Then, you have your while loop.
Check your i variable ($t1). You'll have to use a branch instruction for that. Just load the 10 value in another register, and jump to a label according to the result.
Now, loading the array element. THis is the tricky part.
That array is NOT in mains stack. Its defined like this.
.rdata
.align 2
$array: .word 1
.word 2
.word 3
//you get the picture right?
main: //main function starts here.
The address of the first element of the array is $array (great isnt it?).
So to load the first (or the nth for that matter) element, just
la $t3,$array #this load the address of the array into $t3 register
lw $t2,0($t3) #this does $t2= *(array+0)
So now $t2 contains your array element.
The mod part is REALLY easy. As you are "modding" against 2, you can just take the lsb from your array element, something like
andi $t2,$t2,0x1
Now $t2 contains 1 if the number is odd (lsb is 1) or 0 if its even (lsb is 0). To compare, again, a branch instruction.
When you are done, increment your i variable (use one of the add instructions on t1) and branch back to the comparison.
The return is easy. Return values usually goes in $v0 register (this depends though)
li $v0,0
jr $ra