汇编基本计算器运行时问题



好的,所以我想创建一个简单的程序来计算数值表达式。用户输入一个整数——运算符(+、-、*、/、%)——然后输入一个整数,以此类推,直到输入"=",此时我们输出结果。然后我们询问用户是否想要计算另一个表达式,根据答案,我们返回开始或退出程序。当不支持操作输入或选择/或%运算符输入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运行时,我进入第一个操作符后陷入无限循环…有结果的图片:https://i.stack.imgur.com/FOXuU.jpg

您存储了用户为操作符输入的字符串的地址,但是将其与操作符的字符代码进行比较。因此,没有一个测试成功,您的代码"通过"到addnum,它请求一个操作数,然后返回请求一个操作符。因此,它永远不会检测到输入=以结束循环。

最新更新