1

因此,使用此 MIPS 代码,我试图让用户输入他们想要的树大小,而输出就是成本。199 for > 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
4

1 回答 1

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;
于 2013-09-13T02:50:02.180 回答