2016-06-14 105 views
-1

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

#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 
+1

请更详细地什么问题,你有解释,它在做什么,而不是它应该做的事等 –

+1

但显然你正在做一些奇怪的算术(为什么加-100?),你会跳到一个字符串标签'mes3'而不是实际的代码标签等。 –

+0

Mips使用带有'$ zero'的'addi'来加载立即数。实际上'-100'不会被添加到读取的数字中。但是我不清楚将$ t0和$ t1设置为-100的意义。这需要[mcve]。另外,包括你目前使用调试器的想法。 –

回答

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

OP代码中的addi $ t0,$ zero,-100'是否实际上用'-100'覆盖了'$ t0'中的值?他没有添加任何东西,只是用一个常量替换两个输入。 –

+0

是的,你说得对。谢谢你的收获。虽然用一个常数取而代之似乎没有任何用处。 –

+0

没错,显然是一个巨大的bug(而不仅仅是一个无意义的偏移量,当你比较时会被取消,除非一个数字包装,另一个不包含)。我不知道OP的意图是用于这些说明。 –

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 
+0

它被认为是一个很好的礼仪,包括你的答案的简要说明。 – Neil

+0

唯一的区别是,较大的值存储在$ t1中,因此应该使用它而不是$ t0 –

相关问题