MIPS - 简单加法 - 使用临时寄存器的输入存储问题
MIPS - Simple Addition - Input Storage Issue using Temp registers
这是我第一次写汇编程序。我应该使用 $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
在此先感谢您的帮助。
找到问题 --
#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 行之后有一个系统调用,并且 not 在 move 行之后.否则,$v0 的值将移动到 $t0,即 5,而无需等待用户输入更改 $v0 的值。如果在 move
行之后有一个 syscall
,程序将根据您的用户输入执行另一个服务调用。在此处查看更多评论:Service Calls Executing Based on User Input
在 mips 中 $t0
和 $t1
是临时寄存器。根据它的调用约定,你不能在调用后对临时寄存器的内容做任何假设。因此,除非系统调用特别说明,否则您应该假设它会覆盖临时寄存器。相反,您应该在希望保留 $s0
...$s7
寄存器时使用它们
编辑:哦,我看到你的指令顺序也有错误,就像你说的那样。还要记住临时寄存器的限制
这是我第一次写汇编程序。我应该使用 $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
在此先感谢您的帮助。
找到问题 --
#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 行之后有一个系统调用,并且 not 在 move 行之后.否则,$v0 的值将移动到 $t0,即 5,而无需等待用户输入更改 $v0 的值。如果在 move
行之后有一个 syscall
,程序将根据您的用户输入执行另一个服务调用。在此处查看更多评论:Service Calls Executing Based on User Input
在 mips 中 $t0
和 $t1
是临时寄存器。根据它的调用约定,你不能在调用后对临时寄存器的内容做任何假设。因此,除非系统调用特别说明,否则您应该假设它会覆盖临时寄存器。相反,您应该在希望保留 $s0
...$s7
寄存器时使用它们
编辑:哦,我看到你的指令顺序也有错误,就像你说的那样。还要记住临时寄存器的限制