首页 文章

使用MIPS解析和打印整数时出现问题

提问于
浏览
0

我正在做一个家庭作业,我需要使用MIPS中的运行时堆栈来计算带括号的数学问题,并且我遇到了一些障碍:

我已经到了我试图用用户提供的输入解析整数的地步 . 当它只处理单个数字时它工作得非常好,但是当我得到两位数字时它给了我问题(我使用的是Syscall 4或打印字符串函数) . 例如,我打进77,它会给我“H” . 所以我将系统调用切换为1,打印整数命令,现在我得到了疯狂的大数字 . 无论如何我能完成我需要做的事情吗?

我的代码到目前为止 . 忽略加法和减法方法,它们尚未实现 . 我觉得在解决这个问题之后,这些应该很容易介绍 .

.data

Welcome:    .asciiz "\nCalculate a Fully Parenthesized Expression.\n"
promptExpr: .asciiz "Enter the expression: "
bufExpr:    .space  200

    .text
    .globl main

main:
    la  $a0, Welcome
    li  $v0, 4
    syscall

    la  $a0, promptExpr
    li  $v0, 4
    syscall

    li  $v0, 8
    la  $a0, bufExpr
    li  $a1, 200
    syscall

    li  $t0, 0
    subu    $sp, $sp, 4
    sw  $t0, ($sp)
    li  $t1, 0

Loop:   lb  $t0, bufExpr($t1)
    beq $t0, 10, endProg
    beq $t0, 45, negCheck
    bgt $t0, 47, num
    beq $t0, 41, calc
    bne $t0, 32, push
    addi    $t1, $t1, 1
    j   Loop

endProg:
    li  $t1, 0
    la  $a0, ($sp)
    li  $v0, 1
    syscall

    li  $v0, 10
    syscall

num:    
    move    $t2, $t0
    addi    $t1, $t1, 1
    lb  $t0, bufExpr($t1)
    bgt $t0, 47, collect
    subu    $sp, $sp, 4
    sw  $t2, ($sp)
    addu    $t1, $t1, 1
    j   Loop

collect:
    # collects the entire integer by multiplying the current amount by ten
    # and adding the next digit.
    li  $t7, 10
    mul $t2, $t2, $t7
    addu    $t2, $t2, $t0
    addi    $t1, $t1, 1
    lb  $t0, bufExpr($t1)
    bgt $t0, 47, collect
    subu    $sp, $sp, 4
    sw  $t2, ($sp)
    j   Loop

push:
    subu    $sp, $sp, 4
    sw  $t0, ($sp)
    addu    $t1, $t1, 1
    j   Loop

negCheck:

calc:
    lw  $t4, ($sp)
    addu    $sp, $sp, 4
    lw  $t5, ($sp)
    addu    $sp, $sp, 4
    move    $t0, $t4
    beq $t5, 40, push
    lw  $t6, ($sp)
    addu    $sp, $sp, 4
    lw  $t7, ($sp)
    addu    $sp, $sp, 4
    beq $t5, 43, addMath
    beq $t5, 45, subMath

addMath:

subMath:

对不起,如果我的代码有点乱,MIPS让我头疼 .

先感谢您!

1 回答

  • 0

    您需要在 num / collect 例程中减去 '0' (十进制 48 ),将输入字符串中的字符转换为 0..9 范围内的值 .

    否则,如果您在提示符处输入字符串 12 ,您将获得 '1' * 10 + '2' ,即 49 * 10 + 50 (= 540) .

相关问题