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

问题描述 投票:0回答:1

我需要从用户打印四个输入的最大整数。我想出了如何做到最多三个整数,但最后一个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 "

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

assembly mips
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.