汇编基本计算器运行时问题
Assembly basic calculator runtime problems
好的,所以我想创建一个简单的程序来计算数值 expressions.User 给出一个整数运算符 (+, -, *, / ,%)-然后是一个整数等等,直到他输入“=”,在这种情况下,我们打印 result.Then 我们询问用户是否想要计算另一个表达式,并根据答案返回开始或退出 program.Program 当不支持操作输入或选择运算符 / 或 % 时输入 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, [=10=] #storing command
addi , [=10=], 0x79
beq $a1, , calc #checking if y
addi , [=10=], 0x6e
beq $a1, , 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
您存储了用户为操作员输入的字符串的地址,但正在将其与您的字符代码进行比较运营商。因此,none 的测试成功,并且您将 "falls through" 编码为 addnum
,它请求一个操作数,然后返回请求一个运算符。因此,它永远不会检测到输入 =
以结束循环。
好的,所以我想创建一个简单的程序来计算数值 expressions.User 给出一个整数运算符 (+, -, *, / ,%)-然后是一个整数等等,直到他输入“=”,在这种情况下,我们打印 result.Then 我们询问用户是否想要计算另一个表达式,并根据答案返回开始或退出 program.Program 当不支持操作输入或选择运算符 / 或 % 时输入 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, [=10=] #storing command
addi , [=10=], 0x79
beq $a1, , calc #checking if y
addi , [=10=], 0x6e
beq $a1, , 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
您存储了用户为操作员输入的字符串的地址,但正在将其与您的字符代码进行比较运营商。因此,none 的测试成功,并且您将 "falls through" 编码为 addnum
,它请求一个操作数,然后返回请求一个运算符。因此,它永远不会检测到输入 =
以结束循环。