总是得到相同的整数

Always getting back same Integer

有一个家庭作业问题要求我获取两个用户输入,比较它们,并将它们输入到一个方程式中。

这是我的模板:($t1 + 5) - ($t2 * 2) = 结果

但是,我每次 运行 时似乎都得到一个 return 值 5(或 05)。我不太确定我做错了什么。

代码如下:

.text

# First Input - Saved to $t1
la  $a0, input
li  $v0, 4
syscall

li  $v0, 5
move    $a0, $t1
syscall

# Second Input - Saved to $t2
la  $a0, input2
li  $v0, 4
syscall

li  $v0, 5
move    $a0, $t2
syscall

# Compare the two Inputs
bgt $t1, $t2, Bigger
blt $t1, $t2, Smaller

# If the 1st is greater
# ($t1 + 5) - ($t2 * 2) = result
Bigger:
    add $t4, $t1, 5 # $t4 = $t1 + 5
    mul $t5, $t2, 2 # $t5 = $t2 * 2
    sub     $t7, $t4, $t5   # $t7 = $t4 - $t5
    syscall

    li  $v0, 1
    move    $a0, $t7
    syscall

    li  $v0, 10
    syscall

# If the 1st is smaller
Smaller: 
    add $t4, $t2, 5 # $t4 = $t2 + 5
    mul $t5, $t1, 2 # $t5 = $t1 * 2
    sub     $t7, $t4, $t5   # $t7 = $t4 - $t5

    li  $v0, 1
    move    $a0, $t7
    syscall

    li  $v0, 10
    syscall


.data

input:  .asciiz "Enter the First Integer: "

input2: .asciiz "Enter the Second Integer: "

Halt:   li  $v0, 10
        syscall

有什么帮助吗?谢谢!

read_int 系统调用只需要 $v0 中的函数代码 5 和 returns 中的输入值。所以代替:

li  $v0, 5
move    $a0, $t1
syscall

你应该这样做:

li  $v0, 5
syscall
move    $t1, $v0

当然其他号码也一样。

结合@markgz 关于 $t 寄存器被调用者保存的评论,整个代码可能如下所示:

.text
# First Input - Saved to $t1
la  $a0, input
li  $v0, 4
syscall

li  $v0, 5
syscall
move    $s0, $v0  # save to $s0

# Second Input - Saved to $t2
la  $a0, input2
li  $v0, 4
syscall

li  $v0, 5
syscall
move    $t1, $s0 # restore 1st number
move    $t2, $v0

# Compare the two Inputs
bgt $t1, $t2, Bigger
blt $t1, $t2, Smaller

# If the 1st is greater
# ($t1 + 5) - ($t2 * 2) = result
Bigger:
    add $t4, $t1, 5 # $t4 = $t1 + 5
    mul $t5, $t2, 2 # $t5 = $t2 * 2
    sub     $t7, $t4, $t5   # $t7 = $t4 - $t5

    li  $v0, 1
    move    $a0, $t7
    syscall

    li  $v0, 10
    syscall

# If the 1st is smaller
Smaller:
    add $t4, $t2, 5 # $t4 = $t2 + 5
    mul $t5, $t1, 2 # $t5 = $t1 * 2
    sub     $t7, $t4, $t5   # $t7 = $t4 - $t5

    li  $v0, 1
    move    $a0, $t7
    syscall

    li  $v0, 10
    syscall


.data

input:  .asciiz "Enter the First Integer: "

input2: .asciiz "Enter the Second Integer: "

Halt:   li  $v0, 10
        syscall