装配基本计算器运行时问题

时间:2016-11-11 21:18:28

标签: assembly mips32

好的,所以我想创建一个简单的程序来计算数值表达式。用户给出一个整数 - 运算符(+, - ,*,/,%) - 然后是一个整数,依此类推,直到他输入“=”,在这种情况下我们打印结果。然后我们询问用户是否想要计算另一个表达式并根据答案返回开始或退出程序。程序在不支持操作输入时自动退出或0当选择运算符 /或%时,将作为输入给出(不需要检查数字)。这是我的代码:

.data

str0: .asciiz "nWelcome to Interactive Calculator 1.0"
str1: .asciiz " Enter operand: "
str2: .asciiz " Enter (+, -,*, /,&) or '=' to print result: "
str3: .asciiz " Enter second value: "
str4: .asciiz " Invalid Operator! Try again. "
str5: .asciiz " Result is: "
str6: .asciiz " Another Calculation? y, n: "
str7: .asciiz " Invalid input! Please enter y or n."
str8: .asciiz " Calculator Terminated or error in input(operation error or 0 as input in div or rem."
CRLF: .asciiz "\n"


.text
.globl main

main:
la $a0, str0
li $v0, 4
syscall #printing str0

la $a0, CRLF
li $v0, 4
syscall

calc:

la $a0, str1
li $v0, 4
syscall #printing str1


li $v0,5
syscall #read operand and store it
add $s0 ,$v0, $zero

la $a0, CRLF
li $v0, 4
syscall #change line

la $a0, str2
li $v0, 4
syscall #printing str2



getoperation:
li $v0,12
syscall # reading operation and saving it
la $s1,($v0)


la $a0, CRLF
li $v0, 4
syscall #change line

beq $s1,'=', printres #check if operation is =

beq $s1,'+', addnum #check if operation is +

beq $s1,'-', subnum #check if operation is -

beq $s1,'*', mulnum #check if operation is *

beq $s1,'/', divnum #check if operation is /

beq $s1,'%', remnum #check if operation is %

addnum:
la $a0, str1
li $v0, 4
syscall #printing str1
li $v0,5
syscall #read operand and perform add
add $s0 ,$s0, $v0
j getoperation

subnum:
la $a0, str1
li $v0, 4
syscall #printing str1
li $v0,5
syscall #read operand and perform sub
sub $s0 ,$s0, $v0
j getoperation

mulnum:
la $a0, str1
li $v0, 4
syscall #printing str1
li $v0,5
syscall #read operand and perform mul
mul $s0 ,$s0, $v0
j getoperation

divnum:
la $a0, str1
li $v0, 4
syscall #printing str1
li $v0,5
syscall #read operand and check its not 0
bgez $v0,exitApp
div $s0,$s0,$v0
j getoperation

remnum:
la $a0, str1
li $v0, 4
syscall #printing str1
li $v0,5
syscall #read operand and check its not 0
bgez $v0,exitApp
rem $s0,$s0,$v0
j getoperation

printres:
la $a0, str5
li $v0, 4
syscall #printing str5
move $a0,$s0
li $v0,1
syscall
j anothercalc

j exitApp

anothercalc:
la $a0, str0
li $v0, 4
syscall #printing str6
li $v0,12
syscall #reading input y/n
add $a1, $v0, $0    #storing command

addi $9, $0, 0x79
beq $a1, $9, calc   #checking if y

addi $9, $0, 0x6e
beq $a1, $9, exitApp    #checking if n

la $a0, str0
li $v0, 4 
syscall #printing str7
j anothercalc



exitApp:
la $a0, str8
li $v0, 4
syscall #printing str0


li $v0,10
syscall

但是当我尝试运行QTspim后,我进入第一个运算符后陷入无限循环...图片结果可用: http://imgur.com/a/6GYVd

1 个答案:

答案 0 :(得分:1)

您存储了用户为运营商输入的字符串的地址,但正在将其与运营商的字符代码进行比较。因此,没有一个测试成功,并且您将“直通”编码到addnum中,它要求操作数,然后返回询问操作符。因此,它永远不会检测到输入=来结束循环。