2013-04-30 51 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)。