2017-05-31 91 views
0

這是我第一次編碼PCSPIM。我發現我的代碼有點麻煩。MIPS:如何將用戶輸入的整數應用到算術函數中?

.data 

user_input: .asciiz "\n\nEnter an Integer for the value of n: " 
result_display: .asciiz "\nThe sum from 0 to n is "    
Greeting: .asciiz "\n\nThank you!"  

.text 
main: 

#user input 
li $v0, 4 
la $a0, user_input 
syscall 

#allow user input 
li $v0, 5 
syscall 

#store the input value into t8 
move $t8, $v0 

#calculation 
addi $s0, $zero, $t8 

我希望用整數值($ T8),用戶輸入到#calculation部分,但它與錯誤結束。

addi $t0, $zero, 0 

loop1: 

add $t0, $t0, $s0 
addi $s0, $s0, -1 
bne $s0, $zero, loop1 
nop 
nop 

# Display the result 
li $v0, 4 
la $a0, result_display 
syscall 

# Print out the result 
li $v0, 1 
move $a0, $t0 
syscall 

# Greets the user 
li $v0, 4 
la $a0, Greeting 
syscall 

# Exit the program 
li $v0, 10 
syscall 

對不起,我蹩腳的英語。

回答

0

錯誤在於您使用「addi」指令的方式。該指令要求立即(數字)值作爲第三操作數傳遞,而不是架構寄存器。如果您將「addi」指令更新爲「addu」,則代碼應該起作用。

+0

非常感謝:D –

+0

它真的很有用。言語無法表達我的感激之情:'D –

相關問題