2016-10-22 7 views
-6

問題は、減算の結果が表示されるのを待つときに表示されますが、代わりに0が表示されます。x-aではありません。Substract in 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

'subst'は' substract is: 'の結果を出力した後に' sub'を実行することができます。 'move'命令を保存し、それを出力するsyscallのために減算結果を' $ a0'に入れます。 –

+0

デンカネイチップタ! –