MIPS分支总是一样的

时间:2013-09-13 00:44:10

标签: mips

所以使用这个MIPS代码,我试图获得他们想要的树大小的用户输入,输出是成本。 199为> 8,99> = 6,69> = 3,39> = 0,并且只是一个仅对负输入表示正面的短语。

我的问题是无论输入什么,程序总是吐出69,什么错了?

    #data declarations: declare variable names used in program, storage allocated in RAM
                .data  

    msg1:           .asciiz     "Enter the height of a tree to purchase: "
    msg21:          .asciiz     "The cost: "
    msg22:          .asciiz     " dollars\n"
    msg3:           .asciiz     "\n"
    msgNeg:         .asciiz     "Please enter a positive integer for the tree height.\n"
    big:            .word       8
    med:            .word       6
    small:          .word       3
    negative:       .word       0

    #The program begins after .text
                .text
    main:
                    #Prints first line and receives input
        la $a0, msg1
        li $v0, 4
        syscall
        li $v0, 5
        syscall
        la $a0, msg3
        li $v0, 4
        syscall
                    #Stores user input in parameter variable
        move $a0, $v0
                    #Calls cost function to determine cost
        addi $sp, $sp, -4
        sw $ra, 0($sp)
        jal cost
        lw $ra, 0($sp)
        addi $sp, $sp, 4
                    #stores returned value to temporary register
        move $t0, $v0
                    #Prints out the cost of the tree
        la $a0, msg21
        li $v0, 4
        syscall
        move $a0, $t0
        li $v0, 1
        syscall
        la $a0, msg22
        li $v0, 4
        syscall

                    #return
        jr $ra

    cost:
                    #finds the range of the user value
        la $t6, small
        lw $t7, 0($t6)
        bge $a0, $t7, smTree
        nop
        la $t6, med
        lw $t7, 0($t6)
        bge $a0, $t7, medTree
        nop
        la $t6, big
        lw $t7, 0($t6)
        bgt $a0, $t7, bigTree
        nop
        la $t6, negative
        lw $t7, 0($t6)
        blt $a0, $t7, negTree
        nop
                        #else statement, returns 39
        li $v0, 39
        jr $ra

    bigTree:
                        #first branch, returns 199
        li $v0, 199
        jr $ra

    medTree:
                        #second branch, returns 99
        li $v0, 99
        jr $ra

    smTree:
                        #third branch, returns 69
        li $v0, 69
        jr $ra
    negTree:
                #fourth branch, returns a phrase
        la $a0, msgNeg
        li $v0, 4
        syscall
        jr $ra

1 个答案:

答案 0 :(得分:1)

当您阅读用户的号码时:

    li $v0, 5
    syscall
    la $a0, msg3
    li $v0, 4
    syscall
                #Stores user input in parameter variable
    move $a0, $v0

$v0将不再保留用户的值。它已被4覆盖,这与返回69成本的情况相对应。您必须在$v0之后立即将syscall 5复制到另一个临时注册表。

此外,cost的逻辑目前是这样的:

if (value >= 3)
    return 69;
if (value >= 6)
    return 99;
if (value >= 8)
    return 199;

因此任何值> = 3都将返回69,并且永远不会检查更大的案例。您只需更改订单即可修复它:

if (value >= 8)
    return 199;
if (value >= 6)
    return 99;
if (value >= 3)
    return 69;
相关问题