2017-10-12 3 views
0

私のコードに示されているようにユーザー入力を得ることができましたが、最小の数を得ることは必至です。 ありがとうございました...ループを使用せずにMIPSで3つの整数の最小値を決定する方法

ここにこの手順があります。

"ユーザーから3つの32ビット符号付き整数を読み取るためのアセンブリプログラムを作成するこれらの3つの数値の最小値を決定し、この結果を表示するループを使用しないでください。

.data 
Msg1: .asciiz "Enter the first integer: " 
Msg2: .asciiz "Enter the second integer: " 
Msg3: .asciiz "Enter the third integer: " 
Msg4: .asciiz "the the smallest numberis: " 

.text 
    # Print the first message 
li $v0, 4 
la $a0, Msg1 
syscall 

# Prompt the user to enter the first integer 
li $v0, 5 
syscall 

# Store the first integer in $t0 
move $t0, $v0 

# Print the second message 
li $v0, 4 
la $a0, Msg2 
syscall 

# Prompt the user to enter the second integer 
li $v0, 5 
syscall 

# Store the first integer in $t1 
move $t1, $v0 

# Print the third message 
li $v0, 4 
la $a0, Msg3 
syscall 

# Prompt the user to enter the third interger 
li $v0, 5 
syscall 

# Store the first integer in $t0 
move $t2, $v0 

# Determine the smallest Number 
slt $s0, $t1, $t0 
beq $s0, $zero, L1 
+0

まだブランチできますか?もしそうなら、最初の2つの最大値を得るために条件付きで移動命令を飛び越えて、それを再び実行するのはかなり簡単です。そうでなければ、ブランチレス最小/最大シーケンスを探します。 (MIPSに 'cmov 'がある場合、またはSUB/SRA/ANDからビルドする必要がある場合はIDK)。 –

+2

あなたは2つの数字のためにそれをすることができますか?次に、2つのうちの最小値を取った後、結果を印刷する代わりに、2つの値からもう少し小さい値(前の結果から3番目の値までの最小値)を計算します。それはほんの数行のコードです。 – Ped7g

+0

min(a、b、c)= min(min(a、b)、c) – Tommylee2k

答えて

0

ご回答いただきありがとうございました。最終的には最小の数値を決定することができました。このコードは、MARSで完全に機能します。

.data 
Msg1: .asciiz "Enter the first integer: " 
Msg2: .asciiz "Enter the second integer: " 
Msg3: .asciiz "Enter the third integer: " 

.text 
    # Print the first message 
li $v0, 4 
la $a0, Msg1 
syscall 

# Prompt the user to enter the first integer 
li $v0, 5 
syscall 

# Store the first integer in $t0 
move $t0, $v0 

# Print the second message 
li $v0, 4 
la $a0, Msg2 
syscall 

# Prompt the user to enter the second integer 
li $v0, 5 
syscall 

# Store the first integer in $t1 
move $t1, $v0 

# Print the third message 
li $v0, 4 
la $a0, Msg3 
syscall 

# Prompt the user to enter the third interger 
li $v0, 5 
syscall 

# Store the first integer in $t0 
move $t2, $v0 

# Determine the smallest Number 
blt $t0, $t1, L0 
blt $t1, $t0, L3 


li, $v0, 10 
syscall 

L0: 
    blt $t0, $t2, L2 
    blt $t2, $t0, L3 

L2: 
    li $v0, 1 
    move $a0, $t0 
    syscall 
    li, $v0, 10 
    syscall 

L3: 
    blt $t1, $t2, L4 
    blt $t2, $t1, L5 

L4: 
    li $v0, 1 
    move $a0, $t1 
    syscall 
    li, $v0, 10 
    syscall 

L5: 
    li $v0, 1 
    move $a0, $t2 
    syscall 
    li, $v0, 10 
    syscall 


li, $v0, 10 
syscall 
+0

ヒント:2つのsyscallブロック全体を複製するのではなく、 'move'命令のまわりを分岐し、最後に' li'/'syscall'のブロックを一度表示させるコードのほうがはるかに少なくなります。 '$ a0'の右の整数です。 –

関連する問題