- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个程序来检查整数中的 16 位是 1 还是 0。我选择通过右移一位 15 次并检查每次移位中的第一位是否为零或非零来实现此目的。然后,如果第一位是 1,我将增加一个整数。
我用 C 编写了一些代码,代表我的代码的非用户输入版本。
int j = 100;
int checker = 0;
int count = 0;
for (i=0; i<16; i++) {
checker = j & 0x1;
if (checker > 0)
count++;
j = (j >> 1);
}
我在 MIPS 中的代码:
.data
userprompt: .asciiz "Enter positive integer: "
newline: .asciiz "\n"
.text
.globl main
main:
li $v0, 4 # System call: Display string
la $a0, userprompt # Load string userprompt for output
syscall
li $v0, 5 # System call: Read integer
syscall
move $s0, $v0 # Store integer from v0 to s0
move $s1, $s0 # s1 = s0
li $t0, 0 # t0 = 0
jal chk_zeros # Run function: chk_zeroes
li $v0, 1 # System call: Read integer
move $a0, $t2 # Store integer from t2 to a0
syscall
li $v0, 10 # System call: quit
syscall
chk_zeros:
bgt $t0, 15, exitchk # t0 <= 15
addi $t0, $t0, 1 # Add one to t0
andi $t1, $s1, 0x1 # Check if first bit is non-zero, store in t1
bgtz $t1, chk_zerosadd # If t1 >= 0
j chk_zeros
chk_zerosadd:
addi $t2, $t2, 1 # Add one to t2
jr $ra # Return to after the if statement (does not work!)
exitchk:
jr $ra
我遇到的问题是让 chk_zerosadd 返回到分支语句之后。 jr $ra 似乎让我返回到 chk_zerosadd 中的主函数。
最佳答案
bgtz
不会将下一个 PC 地址放入返回地址寄存器,因此 jr $ra
不会返回到分支语句之后的指令。您可以使用 bgtzal
(如果大于零则分支并链接),这将为您提供您正在寻找的行为,或者您可以重新安排您的代码,以便您在添加上分支,而不是像这样分支到它:
andi $t1, $s1, 0x1 # Check if first bit is non-zero, store in t1
beq $t1, chk_zerosskipadd # Jump if $t1 is zerp
addi $t2, $t2, 1 # Add one to t2
chk_zerosskipadd:
# continue execution...
srl $s1, $s1, 1 # j = (j >> 1);
j chk_zeros
关于c - MIPS 中 For 循环分支后跳转到地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28955790/
我的老师说你可以像这样简单地将一个字符加载到寄存器中: 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
我是一名优秀的程序员,十分优秀!