不断收到异常 5 [存储中地址错误],后跟存储中未对齐的地址。我该如何解决这个问题?

发布于 2025-01-18 04:30:13 字数 6147 浏览 3 评论 0原文

我的项目要求我输入表达式,将其转换为后缀,然后能够对其进行评估。我得到了输入并将其转换为表达式。但是,当我尝试将整数推入评估程序中的堆栈时,我一直在遇到异常5 [商店中的地址错误]。我不知道为什么它会一直给我这个例外。我的假设是Pushtostack2正在引起问题。

.dataprompt1: .asciiz "Please enter an expression: "prompt2: .asciiz "\nThe expression you want to evaluate is "prompt3: .asciiz "\nThe postfix expression is "prompt4: .asciiz "\nThe evaluated answer is "userInput: .space 30postfixString: .space 30convertedString: .space 30

line: .asciiz "\n"

.text

.globl mainmain:#Calls input procedurejal input

#Calls postfix procedure
jal postfix

#Calls evaluate procedure
jal evaluation

#Calls output procedure 
#jal output

#Ends program
li $v0, 10
syscall

#Procedure to get input from user and put it into a stringinput:#Print first promptli $v0, 4la $a0, prompt1syscall

#Get user input
li $v0, 8
la $a0, userInput
la $a1, 100
syscall

#Print second prompt
li $v0, 4
la $a0, prompt2
syscall

#Prints user input
li $v0, 4
la $a0, userInput
syscall

#Returns code back to the main procedure
jr $ra

#Procedure to change string from infix to postfixpostfix:

la $t0, userInput   # $t0 = input
la $t3, postfixString # $t3 = postfix string

li $t1, 0           # increment i
lb $t2, 0($t0)      # $t2 = character

while:
    #Loops until counter equals null
    beq $t2, $zero, endloop

    #prints the character it is on in the string - testing only
    #li $v0, 11
    #move $a0, $t2
    #syscall 

    #If character is '(', it will be pushed to stack
    beq $t2, '(', pushToStack

    #If character is an operand, it will add to postfix string
    beq $t2, '0', addToPostfixStr
    beq $t2, '1', addToPostfixStr
    beq $t2, '2', addToPostfixStr
    beq $t2, '3', addToPostfixStr
    beq $t2, '4', addToPostfixStr
    beq $t2, '5', addToPostfixStr
    beq $t2, '6', addToPostfixStr
    beq $t2, '7', addToPostfixStr
    beq $t2, '8', addToPostfixStr
    beq $t2, '9', addToPostfixStr

    #If character is an operator, it will be pushed to stack
    beq $t2, '+', pushToStack
    beq $t2, '-', pushToStack
    beq $t2, '*', pushToStack
    beq $t2, '/', pushToStack

    #If character is ')', it will be popped from stack
    beq $t2, ')', popFromStack

    #Procedure to push '(' or operator to the stack
    pushToStack:
        addi $sp, $sp, -1 #Points to next spot in stack -1 because it is a char
        sb $t2, 0($sp) #Stores charater from $t2 to where stack pointer is pointing to

        j endIteration

    #Procedure to pop from the stack
    popFromStack:
        lb $t4, 0($sp)      #Allocates space to store ')'
        addi $sp, $sp, 1    #Moves to next slot in stack

        move $t2, $t4       #Moves stored info from t4 to t2
        
        lb $t4, 0($sp)      #Allocates space to store ')'
        addi $sp, $sp, 1    #Moves to next slot in stack

        #Check if next is '(' or operator
        beq $t2, '(', addToPostfixStr
        beq $t2, '+', addToPostfixStr
        beq $t2, '-', addToPostfixStr
        beq $t2, '*', addToPostfixStr
        beq $t2, '/', addToPostfixStr

        j endIteration

    #Procedure to add characters to the postfix string
    addToPostfixStr:
        sb $t2, 0($t3)      #Saves operand or operator to the postfix string
        addi $t3, $t3, 1    #Moves to the next slot in string

        j endIteration

    #Procedure to end the current iteration
    endIteration:             
        addi $t0, $t0, 1            # $t0 = $t0 + 1
        lb $t2 0($t0)   
        j while                     #loop backs to while procedure

endloop:
    #Prints prompt 3
    li $v0, 4
    la $a0, prompt3
    syscall

    #Prints the expression after postfix
    li $v0, 4
    la $a0, postfixString
    syscall

    #Skips a line
    li $v0, 4
    la $a0, line
    syscall

    #Returns code back to the main procedure
    jr $ra

evaluation:

#Skips a line
li $v0, 4
la $a0, line
syscall

#Loads the postfix string
la $t0, postfixString

#Prints the expression after postfix
li $v0, 4
move $a0, $t0
syscall

 #Skips a line
li $v0, 4
la $a0, line
syscall

li $t1, 0           # increment i
lb $t2, 0($t0)      # $t2 = character

while2:
    #Loops until counter equals null
    beq $t2, $zero, endloop2

    #Prints the character from the postfix expression
    #li $v0, 11
    #move $a0, $t2
    #syscall

    #If the character is a number, it will send to the convertToInt procedure
    beq $t2, '0', convertToInt
    beq $t2, '1', convertToInt
    beq $t2, '2', convertToInt
    beq $t2, '3', convertToInt
    beq $t2, '4', convertToInt
    beq $t2, '5', convertToInt
    beq $t2, '6', convertToInt
    beq $t2, '7', convertToInt
    beq $t2, '8', convertToInt
    beq $t2, '9', convertToInt


    #beq $t2, '+', popFromStack2
    #beq $t2, '-', popFromStack2

    j endIteration2

    #Converts character to an integer
    convertToInt:

        addi $t7, $t2, -48  #Converted integer is now stored in $t4

        #Skips a line
        li $v0, 4
        la $a0, line
        syscall

        #Prints the integer
        li $v0, 1
        move $a0, $t7
        syscall

        #Skips a line
        li $v0, 4
        la $a0, line
        syscall

        #Takes integer that is stored in $t4 and pushes it into stack
        beq $t7, 0, pushToStack2
        beq $t7, 1, pushToStack2
        beq $t7, 2, pushToStack2
        beq $t7, 3, pushToStack2
        beq $t7, 4, pushToStack2
        beq $t7, 5, pushToStack2
        beq $t7, 6, pushToStack2
        beq $t7, 7, pushToStack2
        beq $t7, 8, pushToStack2
        beq $t7, 9, pushToStack2

        j endIteration2

    pushToStack2:
        addi $sp, $sp, -4 #Points to next spot in stack -4 beacuse it is an int
        sw $t7, ($sp) #Stores integer from $t4 to where stack pointer is pointing to

        #Prints the integer
        #li $v0, 1
        #move $a0, $sp
        #syscall

        j endIteration2
    

endIteration2:             
        addi $t0, $t0, 1            # $t0 = $t0 + 1
        lb $t2, 0($t0)   
        j while2                    #loop backs to while2 procedure

endloop2:

    #Returns code back to the main procedure
    jr $ra`

我环顾了互联网,并尝试了.Align是否有效,但没有解决任何问题。我尝试了其他寄存器,以防它已经在先前使用过。

My project requires me to enter an expression, convert it to postfix, and then be able to evaluate it. I got taking an input and converting it to an expression. However, while I'm trying push my integer into a stack in my evaluation procedure, I keep getting Exception 5 [address error in store]. I cannot figure out why it keeps giving me that exception. My assumption is that pushToStack2 is causing the issue.

.dataprompt1: .asciiz "Please enter an expression: "prompt2: .asciiz "\nThe expression you want to evaluate is "prompt3: .asciiz "\nThe postfix expression is "prompt4: .asciiz "\nThe evaluated answer is "userInput: .space 30postfixString: .space 30convertedString: .space 30

line: .asciiz "\n"

.text

.globl mainmain:#Calls input procedurejal input

#Calls postfix procedure
jal postfix

#Calls evaluate procedure
jal evaluation

#Calls output procedure 
#jal output

#Ends program
li $v0, 10
syscall

#Procedure to get input from user and put it into a stringinput:#Print first promptli $v0, 4la $a0, prompt1syscall

#Get user input
li $v0, 8
la $a0, userInput
la $a1, 100
syscall

#Print second prompt
li $v0, 4
la $a0, prompt2
syscall

#Prints user input
li $v0, 4
la $a0, userInput
syscall

#Returns code back to the main procedure
jr $ra

#Procedure to change string from infix to postfixpostfix:

la $t0, userInput   # $t0 = input
la $t3, postfixString # $t3 = postfix string

li $t1, 0           # increment i
lb $t2, 0($t0)      # $t2 = character

while:
    #Loops until counter equals null
    beq $t2, $zero, endloop

    #prints the character it is on in the string - testing only
    #li $v0, 11
    #move $a0, $t2
    #syscall 

    #If character is '(', it will be pushed to stack
    beq $t2, '(', pushToStack

    #If character is an operand, it will add to postfix string
    beq $t2, '0', addToPostfixStr
    beq $t2, '1', addToPostfixStr
    beq $t2, '2', addToPostfixStr
    beq $t2, '3', addToPostfixStr
    beq $t2, '4', addToPostfixStr
    beq $t2, '5', addToPostfixStr
    beq $t2, '6', addToPostfixStr
    beq $t2, '7', addToPostfixStr
    beq $t2, '8', addToPostfixStr
    beq $t2, '9', addToPostfixStr

    #If character is an operator, it will be pushed to stack
    beq $t2, '+', pushToStack
    beq $t2, '-', pushToStack
    beq $t2, '*', pushToStack
    beq $t2, '/', pushToStack

    #If character is ')', it will be popped from stack
    beq $t2, ')', popFromStack

    #Procedure to push '(' or operator to the stack
    pushToStack:
        addi $sp, $sp, -1 #Points to next spot in stack -1 because it is a char
        sb $t2, 0($sp) #Stores charater from $t2 to where stack pointer is pointing to

        j endIteration

    #Procedure to pop from the stack
    popFromStack:
        lb $t4, 0($sp)      #Allocates space to store ')'
        addi $sp, $sp, 1    #Moves to next slot in stack

        move $t2, $t4       #Moves stored info from t4 to t2
        
        lb $t4, 0($sp)      #Allocates space to store ')'
        addi $sp, $sp, 1    #Moves to next slot in stack

        #Check if next is '(' or operator
        beq $t2, '(', addToPostfixStr
        beq $t2, '+', addToPostfixStr
        beq $t2, '-', addToPostfixStr
        beq $t2, '*', addToPostfixStr
        beq $t2, '/', addToPostfixStr

        j endIteration

    #Procedure to add characters to the postfix string
    addToPostfixStr:
        sb $t2, 0($t3)      #Saves operand or operator to the postfix string
        addi $t3, $t3, 1    #Moves to the next slot in string

        j endIteration

    #Procedure to end the current iteration
    endIteration:             
        addi $t0, $t0, 1            # $t0 = $t0 + 1
        lb $t2 0($t0)   
        j while                     #loop backs to while procedure

endloop:
    #Prints prompt 3
    li $v0, 4
    la $a0, prompt3
    syscall

    #Prints the expression after postfix
    li $v0, 4
    la $a0, postfixString
    syscall

    #Skips a line
    li $v0, 4
    la $a0, line
    syscall

    #Returns code back to the main procedure
    jr $ra

evaluation:

#Skips a line
li $v0, 4
la $a0, line
syscall

#Loads the postfix string
la $t0, postfixString

#Prints the expression after postfix
li $v0, 4
move $a0, $t0
syscall

 #Skips a line
li $v0, 4
la $a0, line
syscall

li $t1, 0           # increment i
lb $t2, 0($t0)      # $t2 = character

while2:
    #Loops until counter equals null
    beq $t2, $zero, endloop2

    #Prints the character from the postfix expression
    #li $v0, 11
    #move $a0, $t2
    #syscall

    #If the character is a number, it will send to the convertToInt procedure
    beq $t2, '0', convertToInt
    beq $t2, '1', convertToInt
    beq $t2, '2', convertToInt
    beq $t2, '3', convertToInt
    beq $t2, '4', convertToInt
    beq $t2, '5', convertToInt
    beq $t2, '6', convertToInt
    beq $t2, '7', convertToInt
    beq $t2, '8', convertToInt
    beq $t2, '9', convertToInt


    #beq $t2, '+', popFromStack2
    #beq $t2, '-', popFromStack2

    j endIteration2

    #Converts character to an integer
    convertToInt:

        addi $t7, $t2, -48  #Converted integer is now stored in $t4

        #Skips a line
        li $v0, 4
        la $a0, line
        syscall

        #Prints the integer
        li $v0, 1
        move $a0, $t7
        syscall

        #Skips a line
        li $v0, 4
        la $a0, line
        syscall

        #Takes integer that is stored in $t4 and pushes it into stack
        beq $t7, 0, pushToStack2
        beq $t7, 1, pushToStack2
        beq $t7, 2, pushToStack2
        beq $t7, 3, pushToStack2
        beq $t7, 4, pushToStack2
        beq $t7, 5, pushToStack2
        beq $t7, 6, pushToStack2
        beq $t7, 7, pushToStack2
        beq $t7, 8, pushToStack2
        beq $t7, 9, pushToStack2

        j endIteration2

    pushToStack2:
        addi $sp, $sp, -4 #Points to next spot in stack -4 beacuse it is an int
        sw $t7, ($sp) #Stores integer from $t4 to where stack pointer is pointing to

        #Prints the integer
        #li $v0, 1
        #move $a0, $sp
        #syscall

        j endIteration2
    

endIteration2:             
        addi $t0, $t0, 1            # $t0 = $t0 + 1
        lb $t2, 0($t0)   
        j while2                    #loop backs to while2 procedure

endloop2:

    #Returns code back to the main procedure
    jr $ra`

I looked around the internet and tried if .align would work but it didn't fix anything. I tried a different register in case it was already used prior.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文