# Is this assembly code correct?

Page 1 of 1

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

### #1 hey92

Reputation: 0
• 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

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

returnRoot:

li 	\$v0,\$s1			# store root in \$v0

return0:

li	\$v0,0			# store 0 in \$v0

```

Is This A Good Question/Topic? 0

## Replies To: Is this assembly code correct?

### #2 turboscrew

• D.I.C Lover

Reputation: 164
• Posts: 1,057
• 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?