所以,我是 MIPS 的新手,正在为二次公式编写代码......我认为我做的一切都是正确的,但我的程序总是给出错误的答案,有时它会因 PC 0x004000f8 的异常而中止
请如果有人能告诉我我哪里出错了以及是否有任何其他差异 在我的代码中,请指导。谢谢!
.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
答案 0 :(得分:0)
试试这个,并为另一个根稍微改变这个你必须改变一些东西,你如何做这是你的测试
.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
答案 1 :(得分:-1)
试试这个代码!
.数据
prompt: .asciiz "Enter valie for a :"
prompt2: .asciiz "Enter value for b:"
prompt3: .asciiz "Enter value for c:"
.text .globl 主
主要: 里 $v0,4 la $a0, 提示 系统调用
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
功率: mul $v0, $a0, $a0 jr $ra
.结束电源 方格: 移动 $v0, $a0 li $t5,0
sqrtloop: div $t6, $a0, $v0 添加 $v0、$t6、$v0 div $v0 $v0,2 添加 $t5,$t5,1 blt $t5, 20, sqrtloop jr $ra .end sqrt