So, i am new to MIPS and was writing a code for quadratic formula... I think i did everything right, but my program always give wrong answers and sometimes it aborts with Exception at PC 0x004000f8
please if anyone can tell me where I am going wrong and if there are any other discrepancy in my code, please guide. Thank you!
.data
promptfora: .asciiz "Enter Value of a: "
promptforb: .asciiz "Enter Value of b: "
promptforc: .asciiz "Enter Value of c: "
finalansdisplay: .asciiz "root is:"
answer: .word 0
.text
.globl main
main:
#b in t1
li $v0, 4
la $a0, promptforb
syscall
li $v0, 5
syscall
move $t1, $v0
#a in t2
li $v0, 4
la $a0, promptfora
syscall
li $v0, 5
syscall
move $t2, $v0
#value of c in t3
li $v0, 4
la $a0, promptforc
syscall
li $v0, 5
syscall
move $t3, $v0
move $a0, $t1 # pass arg's to function
jal power
move $t4, $v0 #b^2
mul $t5,$t2,$t3 # ac in t5
mul $t5,$t5,4
sub $a0,$t4,$t5 # b^2 - 4ac
jal iSqrt
mul $t1,$t1,-1 #- * b
add $t6,$t1,$v0 # - b + sqrt(b^2 - 4ac)
mul $t2,$t2,2 #2a
div $t7, $t6,$t2 #final root
li $v0, 4
la $a0, finalansdisplay
syscall
move $a0, $t6
li $v0, 1 #display final answer
syscall
li $v0, 10
syscall
.end main
#Function to square b
.globl power
.ent power
power:
li $v0, 1
mul $v0, $a0, $a0
jr $ra
.end power
#Function for root
.globl iSqrt
.ent iSqrt
iSqrt:
move $v0, $a0 # $v0 = x = N
li $t0, 0 # counter
sqrLoop:
div $t1, $a0, $v0 # N/x
add $v0, $t1, $v0 # x + N/x
div $v0, $v0, 2 # (x + N/x)/2
add $t0, $t0, 1
blt $t0, 20, sqrLoop
jr $ra
.end iSqrt