gpt4 book ai didi

堆栈指针/程序计数器错误 MIP

转载 作者:行者123 更新时间:2023-12-03 09:06:10 24 4
gpt4 key购买 nike

这是用于让您玩 Bulls 和 Cows 的程序的一部分的功能。
main 跳转并链接到“猜测”标签,然后继续获取正确的输入值。
现在,该函数本身运行正常,但我无法在完成后返回主函数。
我很确定我已经为正确的返回地址正确设置了堆栈指针,以便在需要时将 main 加载到 $ra 中,但它仍然给我一个程序计数器错误,并且测试表明计数器错误确实存在当它试图跳回 main 时发生。

由于函数调用本身使用堆栈,我检查以确保我从 $sp 中的正确空间加载了 $ra(在这种情况下,0($sp) 应该保存 main 的 $ra),它似乎是正确,但程序计数器错误在运行时仍然存在。

我将不胜感激任何有关此事的意见!

    #Gets user's guess and checks

.data
prompt: .asciiz "\nEnter four unique hexadecimal digits: "
invalidInput: .asciiz "\nInvalid guess: must be four unique hexadecimal digits"
validInput: .byte '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','a','b','c','d','e','f'
input: .word 4

.text

#Get the user input
guess:
addi $sp, $sp, -4 #Here is where I save the return address to main
sw $ra, 0($sp) #

la $a0, prompt
li $v0, 4
syscall

la $a0, input
li $a1, 5
li $v0, 8
syscall

jal checkAll #Jumps to the only other call that affects the $sp

#This is the instruction that is flooping around with my program counter, although it seems right!
lw $ra, 0($sp) #When the function is done, load the return address to main
addi $sp, $sp, 4
jr $ra #jump back to main

#Call to quit - for testing
li $v0, 10
syscall

#Makes room and gets input validated
checkAll:
addi $sp, $sp, -8 #This function alters the $sp for it's own calls
sw $ra, 8($sp)
sw $a0, 4($sp)

li $s0, 3
la $a1, validInput
b isValid

lw $a0, 4($sp) #But then it restores the $sp and is able to link back to the 'guess' call
lw $ra, 8($sp)
addi $sp, $sp, 8
jr $ra

#Checks each character from the input agaisnt the valid input characters, basically if the input is hexadecimal
isValid:
blt $s0, $0, unique

la $t0, ($a0)
add $t1, $s0, $0
add $t2, $t1, $t0
lb $t3, ($t2)
li $s1, 21

#Checks each input character agaisnt each valid input character
checkNum:
blt $s1, $0, invalid
la $t0, ($a1)
add $t1, $s1, $0
add $t2, $t1, $t0
lb $t4, ($t2)
bne $t3, $t4, notEqual
j equal

#Character from input is a valid character
equal:
add $s0, $s0, -1
j isValid

#Character from input is not a valid character
notEqual:
add $s1, $s1, -1
j checkNum

#Check if the input is not duplicated
unique:
la $t0, ($a0)
lb $t1, 0($t0)
lb $t2, 1($t0)
lb $t3, 2($t0)
lb $t4, 3($t0)

beq $t1, $t2, invalid
beq $t1, $t3, invalid
beq $t1, $t4, invalid
beq $t2, $t3, invalid
beq $t2, $t4, invalid
beq $t3, $t4, invalid

jr $ra

invalid:
la $a0, invalidInput
li $v0, 4
syscall

j guess

最佳答案

这里有几件事。

首先,您正在使用 .word 4到店input .这将分配一个值为 4 的字这可能不是你想要的。我把它改成了 .space 256 .

接下来,您使用系统调用 8使用 5 在字符串中读取 read人物。这将不起作用,因为正确的输入至少是 6字符,例如:"1234\n\0" .

所以,无论如何,你的问题是当你从你的函数返回时 checkAll您忘记恢复 $ra并增加 $sp .下面在分支无效我改变了jr $ra到:

lw   $ra, 8($sp)
addi $sp, $sp, 8
jr $ra

这使得您的执行的愉快路径没有那个特定的错误,但是您的程序似乎受到了一些概念上的混淆。例如,您对待 checkAll作为一个函数通过 jal 调用它并返回 jr ,但在函数无效的情况下,您 j直通 guess .这种不平衡的处理是一个等待的灾难,可能会导致堆栈损坏。

关于堆栈指针/程序计数器错误 MIP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20361504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com