Raafeh Sajjad
Raafeh Sajjad

Reputation: 39

Swapping 3rd and 5th bit with each other in Mips

My initial program is following but i need help to move forward :

.data
str: .asciiz "Please enter an Integer: "
.text
main:
li $v0,4
la $a0,str1
syscall

li $v0,4
la $a0,str2
syscall

li $v0,4
la $a0,str
syscall

li $v0,5
syscall
move $t0,$v0

sll $t1,$t0,3
sll $t2,$t0,5

bnez $t1,label1
move $t1,1

I want swap the 3rd and 5th bit of the integer put by the user .

Upvotes: 0

Views: 587

Answers (1)

Erik Eidt
Erik Eidt

Reputation: 26646

If (in 8 bits) at the start you have: abcdefgh and you want to swap values from bit position number 5 and bit position number 3, then the output looks like this: abedcfgh.

There's probably a dozen ways to accomplish that.

(I've written this up using bit position 5 and bit position 3 using normal bit position numbering where the low bit (lowest bit/least significant bit/LSB) is taken as bit position number 0 — if you really wanted the 5th bit and the 3rd bit swapped, those would be bit positions 4 and 2.  If you wanted 5th and 3rd bits from the left/MSB, that would also be different.)

So far, you have $t1 = abcdefgh000, and $t2= abcdefgh00000. That's probably not going to be too useful.

But if you shifted right instead, you'd have 000abcde and 00000abc, which is getting closer to isolating the two bits of interest.  Use an & operator with constant 1 and you've got 0000000e and 0000000c, that is the basic extraction of two 1-bit fields.

Still, you don't really need to right justify those bits as we would normally do in bit field extraction, though, as they can instead be moved directly to the new desired bit position using right & left shifts.

One intermediate you want is a copy of the original, with holes (zeros) in the positions of interest, while others are the value of c in the bit 3 position otherwise surrounded by zeros and the value e in the bit 5 position similarly otherwise surrounded by zeros.

  76543210
  abcdefgh    original value
  11010111    mask, constant value 215 decimal
  ======== &  bitwise "and" operation
  ab0d0fgh    intermediate value #1

  76543210
  abcdefgh    original value
         2    shift count=2, decimal
  ======== >> binary right shift operation
  00abcdef    intermediate value #2

  76543210
  00abcdef    intermediate value #2
  00001000    mask, 8 (decimal)
  ======== &  bitwise and operation
  0000c000    intermediate value #3, "c" in the bit 3 position

  76543210
  ab0d0fgh    intermediate value #1
  0000c000    intermediate value #3
  ======== |  bitwise or operation
  ab0dcfgh    intermediate value #4
  
  76543210
  abcdefgh    original value
         2    shift count=2, decimal
  ======== << binary left shift operation
  cdefgh00    intermediate value #5

  76543210
  cdefgh00    intermediate value #5
  00100000    mask, 32 (decimal)
  ======== &  bitwise and operation
  00e00000    intermediate value #6, "e" in the bit 5 position

  76543210
  ab0dcfgh    intermediate value #4
  00e00000    intermediate value #6
  ======== |  bitwise or operation
  abedcfgh    desired output

For the |'s (bitwise or) operations, addition would also work here as we know one operand has 0's where the other has either a 0 or a 1, and hence no carry will happen so the result of + (addition) will be the same as | (bitwise or operation).

I've shown this in 8 bits, and you'll have to adjust for full 32 bits.

Upvotes: 1

Related Questions