2017-10-12 48 views
0

我能夠獲得用戶輸入,如我的代碼所示,但我絕望地無法獲得最小的數字。 非常感謝...如何在不使用循環的情況下確定MIPS中三個整數的最小值

下面是對此的說明。

「編寫一個彙編程序,從用戶讀取三個32位有符號整數,確定這三個數中最小的一個並顯示此結果,不要使用循環,提示用戶輸入每個整數。

.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

你還允許分支嗎?如果是這樣,有條件地跳過移動指令以獲得前兩個的最大值,然後再重新執行它是相當簡單的。否則,查找無分支最小/最大序列。 (如果MIPS具有'cmov',或者如果必須將其構建出SUB/SRA/AND,則爲IDK)。 –

+2

你可以做兩個數字嗎?然後在你從兩個最小值開始,而不是打印結果再次從兩個最小值(從前一個結果到第三個值的最小值),而你從三個中的最小值。這只是幾行代碼。 – 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系統調用塊,只需分配「移動」指令並在最後出現一次'li' /'syscall'塊就會花費少得多的代碼'$ a0'中的右整數。 –

相關問題