0

let's suppose i have 16bit in $t0 and i need to change the 6 bit and 7 bit to 0 and 1 with preserving the other bits. So the bit 6 will have the value 0 and the bit 7 will have the value 0. I know i can do that by using the logic instructions andi and ori. Andi instruction can clear a bit and ori can set a value to a bit. Let's see the code: andi $t0,$t0,0x... ori $t0,$t0,0x...

What hex should i put for replacing the bits? Thanks!

folgore95
  • 1
  • 1
  • Every hex digit represents exactly 4 bits... 0000 = 0, 0001 = 1, 0010 = 2, 0100 = 4, 1000 = 8, and simply sum up the pattern of your interest, i.e. 1101 = 8+4+1 = D. So bit "6" is (I expect b0..b15, i.e. seventh bit from bottom) is 0000_0000_0100_0000 in binary, or 0040 in hexa. (if you meant 6th bit from bottom, then obviously 0020 hexa). Now for `and` you need inverse mask, i.e. ~0040 = FFBF. (also if you are on linux with KDE desktop, you can use KCalc in "numeral system" view with "show bit edit" to see/convert the values between hexa/decimal/octal/binary and get the "feel" for it) – Ped7g May 20 '18 at 16:03
  • 1
    [How do you set, clear, and toggle a single bit?](https://stackoverflow.com/q/47981/995714) – phuclv May 20 '18 at 16:14
  • @Ped7g ok. So should i do addi $t0, $t0, 0x0040. But What about the or instruction? – folgore95 May 20 '18 at 16:19
  • @LưuVĩnhPhúc thanks but i was looking for Assembly way. – folgore95 May 20 '18 at 16:20
  • I'm just showing you how to do that, obviously you need to convert them into assembly yourself – phuclv May 20 '18 at 16:34
  • `addi $t0, $t0, 0x0040` will take original content of register `t0`, and clear all bits except the seventh one, that one will keep it's value, and set this back to `t0`. So that's like test, if seventh bit was set (non zero result in `t0`) or not (zero result in `t0`). To clear some bit you need to use `and` with inverted bit-mask, so all bits retain their original value, except those missing in the "mask", which will become cleared. – Ped7g May 20 '18 at 16:38
  • The `or` is "or", i.e. bit set in either argument is enough to get set bit, so `or` is handy to set particular bit without affecting others. https://en.wikipedia.org/wiki/Bitwise_operation - maybe get some calculator which has binary/hexa options and has "and/or/xor/not" functions and try out yourself, if you fully understand what they do, and how those values change, it's very simple and basic knowledge, and unrelated to assembly, it's more about boolean logic part of algebra. ("assembly" part of this is, that CPUs usually have direct instructions for these bit operations = simple to use). – Ped7g May 20 '18 at 16:41

0 Answers0