Wednesday, June 10, 2009

mips - the next example

This example access an array which is a block of buffer.

/* bar initialize input (or 3 to each element)
*/
int bar(int* in, int n)
{
int ret;
__asm__(
".set push \n" // save assembler option
".set noreorder\n" // suppress reordering
"lw $9, %2 \n" // $9 = n (counter end)
"li $8, 0 \n" // $8 = 0
"li $12, 0 \n" // $12 = 0
"move $4, %1\n" // $4 = in
"loop: \n"
"lw $11, ($4) \n" // $11 = *in
"ori $11, $11, 3 \n" // $11 = $11 || 3
"sw $11, ($4) \n" // *in = $11
"addi $4, $4, 4 \n" // in++ (move to next addess)
"addi $8, $8, 1 \n" // move to next sample
"bne $8, $9, loop \n" // loop
"sw $11, %0 \n" // save $12 to ret
".set pop \n" // restore assembler option
: "=m" (ret) // to return
: "r" (in),"m" (n) // the input
);
return ret;
}


Here the use of $8 is not the best, because the move the array element is by $4, soe the $8 is not most necessary, because $4 can be used to check the range for the loop.