MIPS - 简单加法 - 使用Temp寄存器输入存储问题

时间:2016-09-03 19:42:27

标签: assembly mips

这是我第一次编写汇编程序。我应该使用$ t0,$ t1和$ t2编写一个基本的附加程序来存储前两个用户输入和总和。我无法理解为什么我的用户输入始终存储为5。

我的系统调用可能存在问题吗?

.data
prompt1: .asciiz "Give me an integer number: "
debug1: .asciiz "The first number inputted was: "
debug2: .asciiz "The second number inputted was: "
prompt2: .asciiz "\nGive me another integer number: "
result: .asciiz "\nThe sum of the two inputted numbers is: "

.text

li $v0, 4 #System call to print a string
la $a0, prompt1 #load prompt1 into address 0 - Give me an integer
syscall

#store first input in $t0
li $v0, 5 #System call code for Read Integer from input
move $t0, $v0 #Move value from $v0 to $t0
syscall 

#print the First user input for debugging --> Always prints 5

li $v0, 4  #System call code for Print String
la $a0, debug1 #Print debug1 string
syscall

li $v0, 1   #print int
move $a0, $t0   #move value in $t0 to $a0
syscall

#call prompt 2
li $v0, 4 #Print a string
la $a0, prompt2 # Here is the value to print - "Give me another integer"
syscall

#store second input in $t1
li $v0, 5 #System call code for Read Integer from input
move $t1, $v0 # $t1 = value from input
syscall

#print the second int input for debugging --> Always prints 5

li $v0, 4 #System call code for Print String
la $a0, debug2 #Print debug2 string
syscall

li $v0, 1   #System call code for Print Int
move $a0, $t1   #move value in $t1 to $a0
syscall

#add numbers
add $t2, $t1, $t0 # $t2 = $t1+$t0

li $v0, 4 #System call to Print String
la $a0, result #Print the string result 
syscall

li $v0, 1  #System call to Print Int
move $a0, $t2 #Move $t2, which = $t1 + $t0, into $a0
syscall

我提前感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

发现问题 -

#store second input in $t1
li $v0, 5 #System call code for Read Integer from input
syscall
move $t1, $v0 # $t1 = value from input

li 行之后应该有一个系统调用,在移动行之后应该不是。否则,$ v0的值将移动到$ t0,即5,而不等待用户输入更改$ v0的值。如果在syscall行之后有move,程序将根据您的用户输入执行另一个服务调用。请在此处的评论中查看更多内容:Service Calls Executing Based on User Input

答案 1 :(得分:1)

mips $t0$t1是临时寄存器。根据其调用约定,您不能在打电话后对临时寄存器内容做出任何假设。因此,除非系统调用明确指出,否则你应该假设它覆盖了临时寄存器。相反,您应该使用$s0 ... $s7寄存器来保存它们

编辑:哦,我看到你的说明顺序也有错误,如你所说。另请注意临时寄存器的限制