4 输入 MIPS 计算器

时间:2021-04-28 04:38:54

标签: mips spim

我是 MIPS 新手,我的朋友练习程序是一个计算器,而不是允许用户有 2 个输入(例如 1 + 2 或 1 * 4)如何允许他们有 4 个输入(例如 1 + 3 + 3 + 2 或 1 * 4 * 5 * 2)。下面的代码是一个有效的 2 输入计算器,我如何使它变为 4?

#数据 .数据
prompt1: .asciiz "输入你的第一个数字:" prompt2: .asciiz "输入你的第二个数字:" prompt3: .asciiz "输入你的第三个数字:" prompt4: .asciiz "输入你的第四个数字:" 菜单:.asciiz“现在选择您想要执行的操作:1 表示加法,2 表示减法,3 表示乘法:” resultText: .asciiz "你的最终结果是:"

.text .globl 主 主要的: #下面的代码块是将代表各种指令的整数值预加载到寄存器中进行存储 li $t3, 1 #这是将1的立即数加载到临时寄存器$t3 li $t4, 2 #这是将2的立即数加载到临时寄存器$t4 li $t5, 3 #这是将3的立即数加载到临时寄存器$t5 li $t6, 4 #这是将4的立即数加载到临时寄存器$t6 li $t7, 5 #这是将5的立即数加载到临时寄存器$t7

 #asking the user to provide the first number
li $v0, 4     #command for printing a string
la $a0, prompt1 #loading the string to print into the argument to enable printing
syscall      #executing the command


#the next block of code is for reading the first number provided by the user
li $v0, 5    #command for reading an integer
syscall      #executing the command for reading an integer
move $t0, $v0     #moving the number read from the user input into the temporary register $t0

#asking the user to provide the second number
li $v0, 4    #command for printing a string
la $a0, prompt2    #loading the string into the argument to enable printing
syscall      #executing the command


#reading the second number to be provided to the user
li $v0, 5    #command to read the number  provided by the user
syscall      #executing the command for reading an integer
move $t1, $v0    #moving the number read from the user input into the temporary register $t1



li $v0, 4    #command for printing a string
la $a0, menu    #loading the string into the argument to enable printing
syscall      #executing the command

#the next block of code is to read the number provided by the user
li $v0, 5    #command for reading an integer
syscall      #executing the command
move $t2, $v0    #this command is to move the integer provided into the temporary register $t2

beq $t2,$t3,addProcess    #Branch to 'addProcess' if $t2 = $t3
beq $t2,$t4,subtractProcess #Branch to 'subtractProcess' if $t2 = $t4
beq $t2,$t5,multiplicationProcess #Branch to 'multiplyProcess' if $t2 = $t5

addProcess:
add $t6,$t0,$t1    #this adds the values stored in $t0 and $t1 and assigns them to the     temporary register $t6

#The following line of code is to print the results of the computation above
li $v0,4    #this is the command for printing a string
la $a0,resultText    #this loads the string to print into the argument $a0 for printing
syscall      #executes the command

#the following line of code prints out the result of the addition computation
li $v0,1
la $a0, ($t6)
syscall

li $v0,10 #terminate the program

subtractProcess:
sub $t6,$t0,$t1 #this adds the values stored in $t0 and $t1 and assigns them to the temporary register $t6
li $v0,4    #this is the command for printing a string
la $a0,resultText    #this loads the string to print into the argument $a0 for printing
syscall      #executes the command

#the following line of code prints out the result of the addition computation
li $v0,1
la $a0, ($t6)
syscall

li $v0,10 #terminate the program

multiplicationProcess:
mul $t6,$t0,$t1 #this adds the values stored in $t0 and $t1 and assigns them to the temporary register $t6
li $v0,4    #this is the command for printing a string
la $a0,resultText    #this loads the string to print into the argument $a0 for printing
syscall      #executes the command

#the following line of code prints out the result of the addition computation
li $v0,1
la $a0, ($t6)
syscall

li $v0,10 #terminate the program

1 个答案:

答案 0 :(得分:1)

几乎所有你想做的就是一个接一个地相加、相减或相乘

addProcess:
add $t6,$t0,$t1    
add $t6, $t6, $t8  
add $t6, $t6, $t9  

这将保存在 $t6 的两个值相加,然后将寄存器 $t6 的值与寄存器 $t8 的值相加,同时再次保存到 $t6,然后再一次。

subtractProcess:
sub $t6,$t0,$t1 
sub $t6,$t6, $t8
sub $t6, $t6, $t9 

multiplicatioProcess:
mul $t6,$t0,$t1 
mul $t6, $t6, $t8
mul $t6, $t6, $t9

只需将这些添加到您的单行所在的位置,然后在顶部添加一个点,要求用户输入更多值

#asking the user to provide the first number
li $v0, 4     
la $a0, prompt1
syscall      


#the next block of code is for reading the first number provided by the user
li $v0, 5    
syscall      
move $t0, $v0     

#asking the user to provide the second number
li $v0, 4    
la $a0, prompt2    
syscall     

#reading the second number to be provided to the user
li $v0, 5   
syscall      
move $t1, $v0    

#asking the user to provide the third number
li $v0, 4   
la $a0, prompt3    
syscall
  
#reading the second number to be provided to the user
li $v0, 5    
syscall      
move $t8, $v0    

#asking the user to provide the third number
li $v0, 4   
la $a0, prompt4  
syscall      

#reading the second number to be provided to the user
li $v0, 5    
syscall      
move $t9, $v0    

您也可以以程序的形式完成所有这些工作,但它更高级

相关问题