开发者

Getting MSB of a 32bit integer in MIPS, and merging it into the LSB of another value

Is there an开发者_运维知识库y way to find the MSB of a 32bit integer in MIPS and then replace that to the LSB of another integer?

To elaborate, suppose A = 1000 and B = 1001 (4-bit examples to keep it short.)

I need to get the MSB of B ie 1 and swap this with the LSB of A.

Now A should become 1001.


# Integer 1 -> $a0
# Integer 2 -> $a1
# Result -> $a3

# Moving MSB to LSB, shifting in zeros
srl     $t1, $a0, 31

# Applying r = a ^ ((a ^ b) & mask)
# a = $a1
# b = $t1 = $a0 >> 31
xor     $t2, $a1, $t1
andi    $t2, $t2, 1          # mask=1 - keep only the low bit
xor     $a3, $a1, $t2

Assembly is fun!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜