【发布时间】:2019-07-21 19:14:27
【问题描述】:
我们得到以下 C 代码,并要求将其翻译成 MIPS。我们不需要处理 lo 和 hi;它们为我们存储在 $a0 和 $a1 中:
void decode_request(unsigned long int request, int* array) {
// lo and hi are already stored for us in $a0 and $a1
unsigned lo = (unsigned)((request << 32) >> 32);
unsigned hi = (unsigned)(request >> 32);
for (int i = 0; i < 6; ++i) {
array[i] = lo & 0x0000001f;
lo = lo >> 5;
}
unsigned upper_three_bits = (hi << 2) & 0x0000001f;
array[6] = upper_three_bits | lo;
hi = hi >> 3;
for (int i = 7; i < 11; ++i) {
array[i] = hi & 0x0000001f;
hi = hi >> 5;
}
这是我的尝试:
.globl decode_request
decode_request:
li $t0, 0 # int i = 0;
first_loop:
bge $t0, 6, after_first_loop # branch if greater than or equal to 6
mul $t0, $t0, 4 # account for ints; storage size = word
and $t1, $a0, 0x0000001f # lo & 0x0000001f;
sw $t1, 4($t1) # array[i] = lo & 0x0000001f;
srl $t2, $a0, 5 # lo >> 5;
sw $t2, 0($t2) # lo = lo >> 5;
add $t0, $t0, 1 # ++i
j first_loop # jump back to top of loop
after_first_loop:
sll $t3, $a1, 2 # (hi << 2)
and $t3, $t3, 0x0000001f # & 0x0000001f
mul $t3, $t3, 4 # account for ints; storage size = word
sw $t3, 0($t3) # store back into memory
or $t4, $t3, $a0 # array[6] = upper_three_bits | lo;
sw $t4, 24($t4) # store back into memory; use 24 since int = 4 bytes and we have offset of 6
srl $a1, $a1, 3 # hi >> 3;
sw $a1, 0($a1) # hi = hi >> 3;
second_loop:
li $t5, 7 # int i = 7;
bge $t0, 11, end # branch if greater than or equal to 11
mul $t5, $t5, 4 # account for ints; storage size = word
and $t6, $a1, 0x0000001f # hi & 0x0000001f
sw $t6, 4($t6) # array[i] = hi & 0x0000001f;
srl $t7, $a0, 5 # hi >> 5;
sw $t7, 0($t7) # lo = lo >> 5;
add $t5, $t5, 1 # ++i
j second_loop # jump back to top of second_loop
end:
jr $ra
关于我可能出错的地方或其他方法的任何想法?
谢谢!
【问题讨论】:
-
在您的程序中什么不起作用?你试过在上面使用调试器吗?
-
我试过在上面运行 QtSpim,是的。不过,我对 MIPS 还是有点陌生,所以我不确定如何解释这些错误。
-
为什么不直接使用 gcc?
-
@Swordfish,会不会是因为它可以是家庭作业?不回头寻找解决方案会是一种挑战吗?
标签: c mips code-translation