In: Computer Science
Part (b): Reversing the order of bits in a word
Recall that in our course we define a word to be a 32-bit sequence (i.e., four
consecutive bytes). For some algorithms it is useful to have a reversed version of
that 32-bit sequence. (The deeply curious can read a brief description about such
use in Fast Fourier Transform algorithm implementations by visiting Wikipedia at
this link: http://bit.ly/2rnvwz6 ).
Your task for part (b) is to complete the code in reverse.asm that has been
provided for you. Please read this file for more detail on what is required.
Some test cases are provided to you.
# Compute the reverse of the input bit sequence that must be stored # in register $8, and the reverse must be in register $15. .text start: lw $8, testcase3 # STUDENTS MAY MODIFY THE TESTCASE GIVEN IN THIS LINE # STUDENTS MAY MODIFY CODE BELOW # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv nop add $15, $0, -10 # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # STUDENTS MAY MODIFY CODE ABOVE exit: add $2, $0, 10 syscall .data testcase1: .word 0x00200020 # reverse is 0x04000400 testcase2: .word 0x00300020 # reverse is 0x04000c00 testcase3: .word 0x1234fedc # reverse is 0x3b7f2c48
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
.text
start:
lw $8, testcase3 # STUDENTS MAY MODIFY THE TESTCASE GIVEN IN THIS
LINE
# STUDENTS MAY MODIFY CODE BELOW
# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
move $9,$8
li $10,1 #load one
li $12,0
loop:
sll $15,$15,1 #shift left 15
and $11,$9,$10 #get first bit
or $15,$15,$11 #or the value
srl $9,$9,1#shift right 9
add $12,$12,1
blt $12,32,loop
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# STUDENTS MAY MODIFY CODE ABOVE
exit:
add $2, $0, 10
syscall
.data
testcase1:
.word 0x00200020 # reverse is 0x04000400
testcase2:
.word 0x00300020 # reverse is 0x04000c00
testcase3:
.word 0x1234fedc # reverse is 0x3b7f2c48