0

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 .

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • Rotate (sll/srl/OR) and then [bit-blend with a mask](https://stackoverflow.com/questions/68048922/how-to-use-bitwise-operations-to-assign-specific-bits-of-a-byte-according-to-two) would be one way. But since MIPS doesn't natively have rotates, maybe better to mask before (and after?) shifting so you can just AND/OR into the original? Hmm, maybe not. – Peter Cordes Dec 13 '21 at 18:19
  • Well if there is just a sample code that can explain me some logic of how to point at the individual bits of integers then it would be very helpful – Raafeh Sajjad Dec 13 '21 at 18:34
  • 1
    This kind of bit manipulation is somewhat language independent, so there's no need to "think in assembly" for this. Try it in on paper or in C first using &, |, <<, >>, etc,. if-then's if you must (they aren't required for this though), then take that to assembly language. – Erik Eidt Dec 13 '21 at 19:21

1 Answers1

1

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.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • The querent wants the 3rd bit, which would be bit #2 not bit #3. You're taking the 4th bit, the top of the low nibble, aka bit #3, in your examples. Obviously that's fine because it's just an example, but it might be misleading to the OP if they assume that your bit 3 is the bit they want. – Peter Cordes Dec 14 '21 at 01:42
  • @PeterCordes, oops, good catch. Hopefully, they get the general idea. Same to be said for bit 5 vs. 5th bit. – Erik Eidt Dec 14 '21 at 01:43
  • 1
    You should probably just add a paragraph about bit numbering before or after the example, to make sure it's mentioned in your answer. Perhaps even at the very top. Perhaps just say "you were talking about the 3rd bit, bit #2, but the examples in this answer are showing bit #3, the 4th bit. The principle is of course the same." – Peter Cordes Dec 14 '21 at 01:45