使用 MIPS 汇编查找四个输入的最大整数 [英] Finding largest integer of four inputs using MIPS assembly

查看:53
本文介绍了使用 MIPS 汇编查找四个输入的最大整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打印来自用户的四个输入的最大整数.我想出了如何做到最多三个整数,但最后一个 CMP 我遇到了困难.我需要上次 CMP 的帮助.

I need to print the largest integer of four inputs from the user. I figured out how to do it up to three integers, but the last CMP I am having difficulties with. I need assistance with the last CMP.

我假设我还需要一个 CMP 来解决这个问题,但我尝试的每件事总是只打印出前三个数字中最大的一个.

I am assuming I need one more CMP to solve this issue, but everything I try always just prints out the largest of the first three numbers.

    # 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 "

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

The actual results spit out the largest of the three numbers. I need the actual results of the largest of four integers.

推荐答案

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

I ended up finding the solution on mine own. This was the snippet of code that I was in need of...

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

这篇关于使用 MIPS 汇编查找四个输入的最大整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆