In: Computer Science
Translate following C# code into MIPS assembly language
int recursive(int x) {
return (x >= 0xFF) ? (recursive(x - 3) + recursive(x - 2))
: ((x >= 0xF) ? recursive(x - 1) + 1 : 1);
Given C# code as follows:
int recursive(int x) {
return (x >= 0xFF) ? (recursive(x - 3) + recursive(x - 2)): ((x >= 0xF) ? recursive(x - 1) + 1 : 1);
}
Converted MIPS assembly language code:
CODE COMMENT
recursive: //function started
addiu $sp,$sp,-40 //intializing stack pointer
sw $31,36($sp) //save word from stack pointer at $31
sw $fp,32($sp) //save word from stack pointer at file pointer sw $16,28($sp) //save word from stack pointer at $16
move $fp,$sp //move content of stack pointer to file pointer
sw $4,40($fp) //store word at $4 from file pointer
lw $2,40($fp) //load word from $2 to file pointer
nop //do nothing
slt $2,$2,255 //set $2 if $2<0xF
bne $2,$0,$L2 //branch to L2 if $2 not equal to $0
nop //do nothing
lw $2,40($fp) // load $2 with file pointer value
nop //do nothing
addiu $2,$2,-3 //x=x-3 implemented and result stored at $2
move $4,$2 //move the content of $2 to $4
jal recursive //call recurisve function as recurisve(x-3)
nop //do nothing
move $16,$2 //move the content of $2 to $16
lw $2,40($fp) //load the content of file pointer to $2
nop //do nothing
addiu $2,$2,-2 //set x=x-2 store result at $2
move $4,$2 //move content of $2 to $4
jal recursive //call recursive function recursive(x-2)
nop //do nothing
addu $2,$16,$2 //Add content of $16 and $2 and store the result in $2
b $L6 //goto branch L6
nop //do nothing
$L2:
lw $2,40($fp) //load the content of $2 with file pointer
nop //do nothing
slt $2,$2,15 //set $2 if $2<0xF
bne $2,$0,$L4 //if $2 not equal to 0 then goto branch L4
nop //do nothing
lw $2,40($fp) //load the content of $2 with file pointer
nop //do nothing
addiu $2,$2,-1 //set $2 as x-1
move $4,$2 //move content of $2 to $4
jal recursive //call recursive function as recursive(x-1)
nop //do nothing
addiu $2,$2,1 //Add 1 to $2 content
b $L6 //branch to L6
nop // do nothing
$L4:
li $2,1 # 0x1 //set $2 as 1
$L6:
move $sp,$fp //move file pointer content to stack pointer
lw $31,36($sp) //load stack pointer to $31
lw $fp,32($sp) //load file pointer with stack pointer
lw $16,28($sp) //load $16 with stack pointer
addiu $sp,$sp,40 //Add 40 to stack pointer
j $31 //jump with $31
nop //do nothing