1 Replies - 1833 Views - Last Post: 20 November 2012 - 05:31 AM

#1 hey92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-November 11

Is this assembly code correct?

Posted 18 November 2012 - 06:04 PM

This is the code in C:
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



Is This A Good Question/Topic? 0
  • +

Replies To: Is this assembly code correct?

#2 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 626
  • Joined: 03-April 12

Re: Is this assembly code correct?

Posted 20 November 2012 - 05:31 AM

Where is your "float root = sqrt(n);" equivalent in assembly?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1