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

时间:2016-06-14 06:14:58

标签: assembly mips

我在尝试完成一个输入整数的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

2 个答案:

答案 0 :(得分:3)

您的方法存在一些问题。首先,我不知道你为什么用-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

答案 1 :(得分:0)

.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