繁体   English   中英

MIPS读取2个整数,然后打印出较大的一个

[英]MIPS reads 2 integer numbers and then print out the larger one

我在尝试完成一个MIPS程序时遇到麻烦,该程序需要输入整数并输出两个中较大的一个。 我的代码:

#read 2 integer numbers and print out the larger one
.data # data section
    mes1: .asciiz "\n\nEnter the first integer number: "
    mes2: .asciiz "Enter the second integer number: "
    mes3: .asciiz "The larger integer number is: "
.text # code section
    li $v0, 4 #print a string "mes1"
    la $a0, mes1
    syscall

    li $v0, 5 #read the first integer
    syscall
    move $t0, $v0

    li $v0, 4 #print a string "mes2"
    la $a0, mes2
    syscall

    li $v0, 5 #read the second integer
    syscall
    move $t1, $v0

    addi $t0, $zero, -100  #Get larger integer (the first or the second)
    addi $t1, $zero, -100

    slt $s0, $t0, $t1
    bne $s0, $zero, mes3 
    syscall

    li $v0, 4 #print a string "mes3"
    la $a0, mes3 
    syscall

    li $v1, 1 #print the larger int number
    move $a0, $v0
    syscall


    li $v0, 10 # system call for exit
    syscall

您的方法有两个问题。 首先,我不知道为什么要将两个数字都替换为-100。 其次,在条件之后有一个系统调用,该调用似乎对您的问题没有任何作用。 此代码应该起作用。

#read 2 integer numbers and print out the larger one

.data # data section
mes1: .asciiz "\n\nEnter the first integer number: "
mes2: .asciiz "Enter the second integer number: "
mes3: .asciiz "The larger integer number is: "

.text # code section
li $v0, 4 #print a string "mes1"
la $a0, mes1
syscall

li $v0, 5 #read the first integer
syscall
move $t0, $v0

li $v0, 4 #print a string "mes2"
la $a0, mes2
syscall

li $v0, 5 #read the second integer
syscall
move $t1, $v0

slt $s0, $t0, $t1
bne $s0, $zero, print_num  #jumps to print_num if $t0 is larger
move $t0, $t1  #else: $t1 is larger

print_num:
li $v0, 4 #print a string "mes3"
la $a0, mes3 
syscall

li $v0, 1 #print the larger int number
move $a0, $t0
syscall

li $v0, 10 # system call for exit
syscall
.data # data section
    mes1: .asciiz "\n\nEnter the first integer number: "
    mes2: .asciiz "Enter the second integer number: "
    mes3: .asciiz "The larger integer number is: "

.text # code section
    li $v0, 4 #print a string "mes1"
    la $a0, mes1
    syscall

    li $v0, 5 #read the first integer
    syscall
    move $t0, $v0

    li $v0, 4 #print a string "mes2"
    la $a0, mes2
    syscall

    li $v0, 5 #read the second integer
    syscall
    move $t1, $v0

    slt $s0, $t0, $t1
    bne $s0, $zero, print_num  #jumps to print_num if $t0 is larger
    move $t1, $t0  #else: $t1 is larger

print_num:
    li $v0, 4 #print a string "mes3"
    la $a0, mes3 
    syscall`enter code here`

    li $v0, 1 #print the larger int number
    move $a0, $t1
    syscall

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM