0

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
Shaheer
  • 50
  • 6
  • Have you tried debugging? Single step every instruction and find the first place where it goes different from what you expect. Then fix that and continue the process until every instruction is working as you expect -- then you'll get the right answers. – Erik Eidt Mar 30 '21 at 20:27

2 Answers2

0

try this and slightly change this for the other root you have to change something and how you do that is your test

.data
prompt: .asciiz "Enter number 1: "
prompt2: .asciiz "Enter number 2: "
prompt3: .asciiz "Enter number 3: "
newline: .asciiz "\n"
answer: .word 0
sqanswer:  .word 0
 
.text
.globl main
.ent main

main:
li $v0, 4
la $a0, prompt  
syscall

li $v0, 5
syscall
move $t1, $v0   

li $v0, 4
la $a0, prompt2   
syscall

li $v0, 5
syscall
move $t2, $v0     

li $v0, 4
la $a0, prompt3  
syscall

li $v0, 5
syscall
move $t3, $v0    

addi $a0, $t2, 0

#lw $a0, prompt
#lw $a1, prompt2
jal power

sw $v0, answer
lw $s0, answer 

li $t4, 4
mul $t5, $t1, $t3   
mul $t5, $t5, $t4   
li $t6, 2
mul $t7, $t6, $t1
sub $a1, $s0, $t5   

jal iSqrt

sw $v1, sqanswer
lw $s3, sqanswer 


li $s1, -1
mul $s1, $t2, $s1   
add $s2, $s1, $s3   
div $s3, $s2, $t7

move $a0, $s3
la $v0, 1
syscall 

li $v0, 10
syscall

.end main


.globl power
.ent power

power:

mul $v0, $a0, $a0

jr $ra

.end power

.globl iSqrt
.ent iSqrt

iSqrt:

move $v1, $a1   
li $t0, 0   

sqrLoop:

div $t8, $a1, $v1
add $v1, $t8, $v1
div $v1, $v1, 2 
add $t0, $t0, 1
blt $t0, 20, sqrLoop

jr $ra

.end iSqrt
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
-1

Try this code!

.data

prompt: .asciiz "Enter valie for a :"
prompt2: .asciiz "Enter value for b:"
prompt3: .asciiz "Enter value for c:"

.text .globl main

main: li $v0,4 la $a0,prompt syscall

li $v0,5
syscall
move $t1, $v0

li $v0,4
la $a0,prompt2
syscall

li $v0,5
syscall
move $t2, $v0

li $v0,4
la $a0,prompt3
syscall

li $v0,5
syscall
move $t3, $v0    

move $a0,$t2
jal power
move $t0, $v0

mul $t3,$t3,4
mul $t3, $t3,$t1
sub $t0, $t0, $t3

move $a0,$t0
jal sqrt
move $t0, $v0

sub $t0, $t0,$t2
mul $t1, $t1, 2
div $t0, $t0, $t1

li $v0,1
move $a0,$t0
syscall

li $v0, 10
syscall
.end main   

power: mul $v0, $a0, $a0 jr $ra

.end power sqrt: move $v0, $a0 li $t5,0

sqrtloop: div $t6, $a0, $v0 add $v0,$t6,$v0 div $v0 $v0,2 add $t5,$t5,1 blt $t5, 20, sqrtloop jr $ra .end sqrt

  • Please include a reproducible example of your issue as it will help others answer your question. Can you also describe the issue: what is not working? and what is the desired output – yuliaUU Apr 04 '21 at 18:23