3.4. Clearing¶
Clearing bits is the opposite of masking. Instead of wanting to keep certain bits, we want to 0 out specific bits while keeping other ones. For instance, we might want to clear out bits 25-29 from this pattern:
31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 1 | 0 | 1 | 1 | 0 | 1 |
1 | B | A | D | D | E | E | D |
To do so, we could mask by ANDing with this pattern:
31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
C | 1 | F | F | F | F | F | F |
Bits 25-29 would become 0 and everything else would remain as is. However, that pattern is too complex to represent as an immediate. Any time we are trying to clear a small number of bits, we will have a similar problem of needing a large mask to AND with.
Instead, we can use the bitwise clear instruction - it uses a pattern to specify which bits to clear (ANDing with a pattern specifies which bits to keep).
BIC
rd, rn, rm / #BIt Clear. Take the pattern from rn, clear out the bits that are 1 in either register rm or immediate #, place the results in rd.
To continue the earlier sample, instead of AND with 0xC1FFFFFF (which is illegal) to say “keep bits 31-30 and 24-0”, we can BIC with 0x3E000000 to say “clear bits 29-25”:
31 | 30 | 29 | 28 | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 19 | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
3 | E | 0 | 0 | 0 | 0 | 0 | 0 |
LDR r5, =0x1BADDEED
@Clear bits 25-29
BIC r6, r5, #0x3E000000
@Clear bits 16-31 (first two bytes)
BIC r7, r5, #0xFF000000 @clear first byte
BIC r7, r7, #0x00FF0000 @now clear second