Bits Manipulation MiPS Assembly

1

The program takes a user input key, extracts the rightmost 5 bits from that key, then replaces those bits in between bits 2 to 6.

This is what I've tried.

lw $s1, 0($s0)               # extracts the first word in the array
beq $s1, $zero, done         # at the end of the array is a zero => 0


move $t1, $s2                 #s2 has the input key
move $t0, $s1                 # put the word in a temporary register
andi $t2, $t1, 0x0000001f     # use the input mask to return the rightmost 5 bits

sll $t3, $t2, 2               # move the bits over to align with bits 2-6

andi $t0, $t3, 0xffffff83

So my thinking is to take the input key, extract the 5 bits with andi $t2, $t1,0x0000001f, then shift left logical two bits to align the extracted 5 bits with bits 2 to 6.

This is where I'm stuck. How do I replace the bits that I took from the user input key and put them in the bit locations 2 through 6? The input mask for the second andi I believe is correct but I don't think I'm using the proper registers.

EDIT: Thank you for the replies. I figured out the solution with the provided suggestions. For anyone having a similar problem:

I removed the andi at the bottom. And to avoid providing the full answer, the bits that need to be replaced in the data word will have to be modified with an input mask. This can be done with a different andi instruction. There are a couple more steps but this should at least point anyone in the right direction for the future.

assembly
mips
bit-shift
bits
asked on Stack Overflow Mar 13, 2017 by (unknown user) • edited Mar 14, 2017 by (unknown user)

1 Answer

1

"replacing" bits is done by compositing two values into single. For that and/or/xor can be used, the shared principle is to prepare source/target value in such way, that the final composition will produce desired result.

Examples on 4 bit values, to copy bits b1-b3 from source s to destination d:

or/xor/add variant (final bits preserved, others cleared):

s = 0101 => and 0xE => 0100 => `or/xor/add` to `d`
d = 1100 => and 0x1 => 0000     => 0100

and variant (final bits preserved, others set):

s = 0101 => or 0x1 => 0101 => `and` to `d`
d = 1100 => or 0xE => 1110     => 0100

Note the 0xE = ~0x1 (so the bits cleared in register are to be preserved in the other, and vice versa).

answered on Stack Overflow Mar 13, 2017 by Ped7g

User contributions licensed under CC BY-SA 3.0