- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是用于让您玩 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/
我的老师说你可以像这样简单地将一个字符加载到寄存器中: li $s2 "A" li $s1 "0" 除了,当我将我的文件加载到 Qtspim 中时,它一直向我抛出一个语法错误。有谁知道我的老师是否对我
我正在使用二进制 MIPS 指令,我发现 this helpful document .但是,我需要澄清一下:所有分支指令都具有立即值。这是立即值符号扩展吗? 大多数其他立即数是,如果不是,那将意味着
我有一个寄存器($t2),它有一个随机生成的数字,然后我乘以 4。我的问题是,在使用 lw 指令时,是否可以将 $t2 中的值用作偏移量? 最佳答案 在 MIPS 中,您可以使用寄存器、偏移量或两者的
因此,我正在MIPS中构建一个计算器程序,并且试图编写乘法和除法函数。 目前,我像这样在循环中读取整数: li $v0, 5 syscall 然后根据用户要执行的操作最终调用我的函数multi和div
我们目前正在讨论 MIPS 架构。我逐渐了解了计算机体系结构和 MIPS 汇编,这很好。 但是,我尝试用谷歌搜索这个答案,但没有找到合适的答案。我对 ISA 和微架构设计下面的一层感到困惑。 MIPS
我正在研究分支延迟槽。在 spim 上尝试过。 j some j a j b j c j d ori $9, $0, 13 some: a: b: c: d: 令我惊讶的是,它将 9 美元更改
好吧,这个问题更像是一个讨论。我有一个在 VHDL 中实现 pipelined MIPS 处理器的项目。 我完全熟悉流水线的概念,但我从未用 VHDL 实现它。在VHDL 中学习流水线处理器 的实现有
关于带有流水线和转发的 MIPS 架构: add $s0, $t1, $t2 sw $s0, 0($sp) add 指令将在第 3 步(执行操作)准备好结果,但我假设 sw 指令需要第 2 步(指令解
我刚开始学习 MIPS 指令的异常处理程序。 我需要让我的程序有算术溢出异常,以便我可以测试我的异常处理程序。 我有两个数组 A 和 B。数组 A 有十六进制数,数组 B 有整数。 如何通过添加十六进
我正在学习微编程,但对微指令到底是什么感到困惑。我正在使用 MIPS 架构。我的问题如下 例如,我有 ADD 指令,微指令会是什么样子? add指令有多少条微指令。在网上有什么地方可以看到 MIPS
我需要多少档才能正确执行以下指令。我对我所做的事情有点困惑,所以我在这里看到专家的答案。 lw $1,0($2); beq $1,$2,Label; 请注意,检查是否会发生分支将在解码阶段完成。但是源
.data stack: .word 3, 2 .text .globl main main: la $s1, stack #assign stack start
我正在用 MIPS 编写一个回文检查器,我试图让它不区分重音,这样像“ahà”这样的东西也会被认为是一个回文。然而,它看起来并不像小写和大写字母之间有固定值的不区分大小写的场景那么简单。 我问过我的老
我正在尝试向以下内容添加 jal 功能,但我对它的工作原理感到困惑。我知道它将旧的 PC+4 值存储在 $ra 寄存器中,然后将控制权转移到函数,该函数通过 return $ra 传回控制权 但是如何
我正在学习如何执行 MIPS,但我对乘法感到困惑。假设我正在将以下 C 代码转换为 MIPS。 c = b + a*3 a,b,c分别存放在寄存器$s1,$s2,$s3中。我应该如何在 MIPS 中编
我正在为明天的考试而学习,并且我对加载/存储字节主题感到困惑。我有这个例子: 我完全不明白他是怎么得到红色答案的。有人可以帮我解释一下吗? 最佳答案 add $s3, $zero, $zero
我不明白如何翻译标签。任何人都可以给我一个帮助 假设我们有以下代码: loop: add $t2,$t2,$t1 addi $t2,$t2,4 sw $t2,4($s0)
当我阅读 MIPS 架构时,我遇到了影子寄存器,据说它们是通用寄存器的副本。 我无法理解以下内容:何时使用影子寄存器? 最佳答案 MIPS 影子寄存器用于减少处理中断时的寄存器加载/存储开销。分配了影
您好,我想让 hello world 输出用户输入的次数,比如如果他们写了 3 ,那么 hello world 将被打印 3 次。我该怎么做呢?这是我目前正在使用的: .data n:
我正在使用 MARS MIPS 模拟器,我想在我的程序中打印一个换行符。 .data space: .asciiz "\n" .text addi $v0, $zero, 4 # print
我是一名优秀的程序员,十分优秀!