使用MIPS汇编查找四个输入的最大整数

时间:2019-03-24 20:56:01

标签: assembly mips

我需要打印来自用户的四个输入的最大整数。我想出了最多三个整数的方法,但是我遇到了最后一个CMP。我需要最后一个CMP的帮助。

我假设我需要再进行一次CMP来解决此问题,但是我尝试执行的所有操作始终只会打印出前三个数字中的最大数字。

    # this program prints out the four of two numbers 
# The four numbers are read through the keyboard 
.text
.globl main

main:
# Display prompt1
li $v0, 4
la $a0, prompt1
syscall

# read keyboard into $v0 (number x is number to test)
li $v0, 5
syscall

# move the first number from $v0 in $t0
move $t0,$v0

# Display the prmopt2 (string)
li $v0, 4
la $a0, prompt2
syscall

# read keyboard into $v0 
li $v0, 5 
syscall

# move the second number from $v0 in $t1
move $t1,$v0 

# Display the prmopt3 (string)
li $v0, 4
la $a0, prompt3
syscall

# read keyboard into $v0 
li $v0, 5 
syscall

# move the third number from $v0 in $t2
move $t2,$v0

# Display the prmopt4 (string)
li $v0, 4
la $a0, prompt4
syscall

# read keyboard into $v0 
li $v0, 5 
syscall

# move the fourth number from $v0 in $t3
move $t3,$v0


# effectively these two lines do: $t1 = max($t0, $t1)
bge $t1, $t0, CMP2
move $t1, $t0

CMP2:
# effectively these two lines do: $t1 = max($t2, $t1)
bge $t1, $t2, L1 
move $t1, $t2

# largest number in $t1  
move $t2, $t0       

# print answer 
L1: 
li $v0, 4 
la $a0, answer
syscall

# print integer function call 1 
# put the answer into $a0
li $v0, 1 
move $a0, $t1 
syscall

#exit
end: li $v0, 10 
syscall 

.data
prompt1:
 .asciiz "Enter the first number "
prompt2:
 .asciiz "Enter the second number "
prompt3:
 .asciiz "Enter the third number "
prompt4:
 .asciiz "Enter the fourth number "
answer:
 .asciiz "The largest number is "

实际结果吐出了三个数字中最大的一个。我需要四个整数最大的实际结果。

1 个答案:

答案 0 :(得分:0)

我最终自己找到了解决方案。这是我需要的代码片段...

CMP1:
# the following lines perform this: $t1 = max($t0, $t1)
bge $t1, $t0, CMP2
move $t1, $t0

CMP2:
# the following lines perform this: $t1 = max($t2, $t1)
bge $t1, $t2, L1 
move $t1, $t2

# the following lines perform this: $t1 = max($t3, $t2)
CMP3:
bge $t1, $t3, CMP2
move $t1, $t3