int PerfectSquare(int n)
{
float root = sqrt(n);
if (n == ((int) root)*((int) root))
return root;
else
return 0;
}
and this is my attempt to write into MIPS:
PerfectSquare: addi $29,$29,-12 # Space on stack for 3 reg. sw $ra,0($29) # Save $ra on stack sw $s0,4($29) # Save $s0 on stack sw $s1,8($29) # Save $s1 on stack add $s0,$a0,$0 # move $a0 to $s0 addi $t1,$0,1 # set stack entry = 1 mult $a0,$s0,$s0 # multiply $s0 with $s0 beq $s0,$0,return0 # if $s0=0 branch to return0 add $s1,$0,$v0 # s1=root exitPerfectSquare: lw $ra,0($29) # Restore $ra lw $s0,4($29) # Restore $s0 lw $s1,8($29) # Restore $s1 addi $29,$29,12 # bring back stack pointer jr $ra # Return to calling routine returnRoot: li $v0,$s1 # store root in $v0 j exitPerfectSquare # unconditional jump to exit PerfectSquare function return0: li $v0,0 # store 0 in $v0 j exitPerfectSquare # unconditional jump to exit PerfectSquare function

New Topic/Question
Reply


MultiQuote



|