Tuesday, June 9, 2009

mips - a first example

In the following sample, it shows how to send the input (val) and the output (ret) to assembly block.


/* foo returns (val | 3)
*/
int foo(int val)
{
int ret;
__asm__(
".set push\n" // save assembler option
".set noreorder\n" // suppress reordering
"ori $2, %1, %2\n"
"sw $2, %0\n" // save $2 to ret
".set pop\n" // pop assembler option
: "=m" (ret) // ret: memory
: "r" (val), "n" (3) // val: read-only register, n: number
);
return ret;
}



The real code by dump is:

08804554 :
8804554: 27bdffd0 addiu sp,sp,-48
8804558: afbe0020 sw s8,32(sp)
880455c: 03a0f021 move s8,sp
8804560: afc40010 sw a0,16(s8)
8804564: 8fc20010 lw v0,16(s8)
8804568: 34420003 ori v0,v0,0x3
880456c: afc20000 sw v0,0(s8)
8804570: 8fc20000 lw v0,0(s8)
8804574: 03c0e821 move sp,s8
8804578: 8fbe0020 lw s8,32(sp)
880457c: 27bd0030 addiu sp,sp,48
8804580: 03e00008 jr ra
8804584: 00000000 nop