2013-04-30 49 views
0

我在做家庭作业,我需要使用MIPS运行栈来计算括号中的数学问题,我已经打了一个有点暴牙的:问题分析和打印整数与MIPS

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

我的代码到目前为止。忽略加减法,它们还没有实现。我觉得在我解决这个问题后,应该很容易介绍。

.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让我头疼。

预先感谢您!

回答

0

你需要减去'0'(十进制48)在num/collect程序来从你的输入字符串中的字符转换为数值范围0..9

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