Мы получили следующий код на C и попросили перевести его на MIPS.Нам не нужно обращаться с Ло и Привет;они хранятся для нас в $ 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
Любые идеи относительно того, где я могу пойти не так, иливозможно другие подходы?
Спасибо!