c - Mips如何计算整数中的唯一数字
问题描述
您好,我正在将此 C 代码转换为 MIPS,但遇到了一个小障碍。该程序应该计算整数中的唯一数字。我遇到的麻烦是while循环中的“IF语句”。我似乎无法让我的 MIPS 代码执行该 C 操作。
C代码:
while(user > 0) {
digit = 1 << (user % 10);
if(!(store & digit)) {
store += digit;
count++;
}
user /= 10;
}
MIPS 代码:
WhileLoop:
#while(user > 0)
blez $t0, Exit
#$t3 = user % 10
div $t3, $t0, 10
#get remainder
mfhi $s0
#move remainder to $t0
move $t3, $s0
#$t6 = 1
li $t6, 1
#$t8 = 1 << (user % 10)
sllv $t8, $t6, $t3
#$t4 = store& digit
and $t4, $t7, $t8
#!(store& digit)
bne $t4, 0, ExitIf
#store += digit
add $t7, $t7, $t8
#count++
addi $t9, $t9, 1
ExitIf:
div $t0, $t0, 10
#get the quotient
mflo $s1
move $t0, $s1
j WhileLoop
解决方案
您的 MIPS 代码正在执行以下操作:
store &= digit
您确定要更新吗?store
那不是您的 C 代码正在做的事情。
此外,由于您的 C 代码示例不完整(程序集也是如此),我们无法判断您是否正确反转了程序集版本的条件。
在 C 中,if ( a ) then-part
需要if (! a) goto skip; then-part; skip: ;
在汇编中。
推荐阅读
- javascript - .then() 在嵌套的 promise.all 和 fetch 完成之前执行
- c# - 如何在 WPF 应用程序中获取 2 层深处的组件的子级?
- functional-programming - 如何将字符串转换为整数数组,其中包含对应字符的 ascii 值?
- c++ - QMenu 内存泄漏
- ios - 错误:“UIView”类型的值没有成员“view”,在尝试加载共享 pdf 时出现错误
- python-2.7 - CSV通过python导入mysql
- python - 如何为不同颜色的条形图制作动画?
- teechart - Marks.position 变量
- python - 如何使用 PropertyMock 在 pytest 单元测试中返回请求响应属性?
- algorithm - 合并辅助程序的复杂性