如何在MIPS中比较寄存器中的值?

时间:2016-09-18 22:06:37

标签: if-statement assembly mips

我正在尝试创建一个功能,提示用户输入3到30之间的数字,如果数字小于3,它将显示“您的数字小于3”,如果数字大于30它会显示“你的号码大于30”。这可能是愚蠢的,但我是MIPS的新手,我不太明白我做错了什么。

谢谢

.data
prompt: .asciiz "Enter your number (3-30):"
message: .asciiz "\n Your number is "
message2: .asciiz "\n Your number is less than 3"
message3: .asciiz "\n Your number is more than 30"
.text 
# Prompt the user to enter the number
li $v0, 4
la $a0, prompt
syscall

# Get the number 
li $v0, 5 #get an integer from the user
syscall

# Store the result in t0
move $t0, $v0

main: #syscall to end the program
    addi $t1, $zero, 0
    addi $t2, $zero, 3
    addi $t3, $zero, 30

    ble $t0, $t1, numberSmaller
    bge $t0, $t2, numberLarger

    li $v0, 10
    syscall

numberSmaller:
    li $v0, 4
    la $a0, message2
    syscall
numberLarger:
    li $v0, 4
    la $a0, message3
    syscall

1 个答案:

答案 0 :(得分:1)

你实际上非常接近。

main:标签位置错误,因此在某些模拟器下,可能无法执行前几条指令。

此外,该程序[有些]不完整,因为在采取条件分支后,该程序“脱离了世界末日”。

但问题的核心是两个分支需要不同的分支条件,每个分支中的第二个寄存器需要更改:

变化:

ble $t0, $t1, numberSmaller

分为:

blt $t0, $t2, numberSmaller

变化:

bge $t0, $t2, numberLarger

分为:

bgt $t0, $t3, numberLarger

所以,这是完全纠正的程序:

    .data
prompt:     .asciiz     "Enter your number (3-30):"
message:    .asciiz     "\n Your number is "
message2:   .asciiz     "\n Your number is less than 3"
message3:   .asciiz     "\n Your number is more than 30"

    .text
    .globl  main
main:
    # Prompt the user to enter the number
    li      $v0,4
    la      $a0,prompt
    syscall

    # Get the number
    li      $v0,5                   # get an integer from the user
    syscall

    # Store the result in t0
    move    $t0,$v0

    addi    $t1,$zero,0
    addi    $t2,$zero,3
    addi    $t3,$zero,30

    blt     $t0,$t2,numberSmaller
    bgt     $t0,$t3,numberLarger

    # output message to show the number
    li      $v0,4
    la      $a0,message
    syscall

    # show the number
    li      $v0,1
    move    $a0,$t0
    syscall

exit:
    li      $v0,10                  # syscall to end the program
    syscall

numberSmaller:
    li      $v0,4
    la      $a0,message2
    syscall
    j       exit

numberLarger:
    li      $v0,4
    la      $a0,message3
    syscall
    j       exit

这是一个稍微紧张的版本:

    .data
prompt:     .asciiz     "Enter your number (3-30):"
message:    .asciiz     "\n Your number is "
message2:   .asciiz     "\n Your number is less than 3"
message3:   .asciiz     "\n Your number is more than 30"

    .text
    .globl  main
main:
    # Prompt the user to enter the number
    li      $v0,4
    la      $a0,prompt
    syscall

    # Get the number
    li      $v0,5                   # get an integer from the user
    syscall

    # Store the result in t0
    move    $t0,$v0

    li      $t1,3
    blt     $t0,$t1,numberSmaller

    li      $t1,30
    bgt     $t0,$t1,numberLarger

    # output message to show the number
    li      $v0,4
    la      $a0,message
    syscall

    # show the number
    li      $v0,1
    move    $a0,$t0
    syscall

exit:
    li      $v0,10                  # syscall to end the program
    syscall

numberSmaller:
    li      $v0,4
    la      $a0,message2
    syscall
    j       exit

numberLarger:
    li      $v0,4
    la      $a0,message3
    syscall
    j       exit