2016-10-22 92 views
-6

當我等待減法的結果出現時,會出現這個問題,但是它會顯示出0.不是x-a,因爲我希望它會。Mips中的摘要

.data 


strin: .asciiz "type two integers\n\n\n" 

strout: .asciiz "the result of the substract is:" 

a: .word 0 

x: .word 1 



.text 

main: 

li $v0, 4                   
la $a0,strin                 
syscall 

li $v0, 5 
syscall 
sw $t0,a 

li $v1,5 
syscall 
sw $t1,x 


sub $t1,$t1,$t0          


li $v0, 4                   
la $a0,strout                     
syscall 

move $a0, $t1     
li $v0, 1       
syscall 

li $v0,10            
syscall 
+0

您可能要考慮*爲什麼*您將輸入存儲到內存中。 (提示:'syscall'可能會破壞寄存器) – EOF

+0

請不要破壞你的帖子。 – Glorfindel

+0

請不要破壞你自己的問題。這對那些努力回答你的問題的人們是不敬的! –

回答

1

更好的方法是讀取整數作爲輸入,然後進行計算。 這是不是一個實際的方式來存儲在內存中的值,然後讀取它們到 進行計算。如果你不想讀取數字作爲輸入,我建議你使用li指令來加載一個特定的數值你的登記冊,然後進行你可能想要的計算。

.data 
strin: .asciiz "type two integers\n" 
strout: .asciiz "the result of the substract is:" 

.text 
.globl main 
main: 

li $v0, 4 
la $a0,strin                 
syscall 

#changed 
li $v0, 5  #Read the first integer 
syscall 
move $t0,$v0  #move the value you read to $t0 register 

#changed 
li $v0, 5  #Read the second integer 
syscall 
move $t1,$v0 #move the value you read to $t1 register 

sub $s0,$t0,$t1 #make substraction between $t0 and $t1 and store the   
        #result in $s0 register  
li $v0, 4 
la $a0,strout                     
syscall 

li $v0, 1 
move $a0, $s0 
syscall 

li $v0,10 
syscall 
+0

如果您至少在代碼中發表評論來描述您所做的更改以及原因,那麼這將是更好的答案。 –

+0

你可以在打印完'sub'後得到'sub'的結果是'',然後你可以保存一個'move'指令,並將相減結果寫入到'$ a0'中,以便打印它的系統調用。 –

+0

Den kanei tipota! –