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



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

#read 2 integer numbers and print out the larger one
.data # data section
    mes1: .asciiz "nnEnter 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 "nnEnter 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 "nnEnter 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

最新更新