CHAPTER 19. MANIPULATING SPECIFIC BIT(S) CHAPTER 19. MANIPULATING SPECIFIC BIT(S)
loc_28:
; if i!=32, jump to loc_14 and also prepare next shifted value:
bne $v1, $a3, loc_14
sllv $a1, $t0, $v1
; return
jr $ra
or $at, $zero ; branch delay slot, NOP
19.6 Conclusion.
Analogous to the C/C++ shifting operators≪and≫, the shift instructions in x86 areSHR/SHL(for unsigned values) and
SAR/SHL(for signed values).
The shift instructions in ARM areLSR/LSL(for unsigned values) andASR/LSL(for signed values). It’s also possible to add
shift suffix to some instructions (which are called “data processing instructions”).
19.6.1 Check for specific bit (known at compile stage)
Test if the 1000000 bit (0x40) is present in the register’s value:
Listing 19.38: C/C++
if (input&0x40)
Listing 19.39: x86
TEST REG, 40h
JNZ is_set
; bit is not set
Listing 19.40: x86
TEST REG, 40h
JZ is_cleared
; bit is set
Listing 19.41: ARM (ARM mode)
TST REG, #0x40
BNE is_set
; bit is not set
Sometimes,ANDis used instead ofTEST, but the flags that are set are the same.
19.6.2 Check for specific bit (specified at runtime).
This is usually done by this C/C++ code snippet (shift value bynbits right, then cut off lowest bit):
Listing 19.42: C/C++
if ((value>>n)&1)
....
This is usually implemented in x86 code as:
Listing 19.43: x86
; REG=input_value
; CL=n
SHR REG, CL
AND REG, 1
Or (shift 1 bitntimes left, isolate this bit in input value and check if it’s not zero):