Reverse Engineering for Beginners

(avery) #1

CHAPTER 19. MANIPULATING SPECIFIC BIT(S) CHAPTER 19. MANIPULATING SPECIFIC BIT(S)


Listing 19.44: C/C++

if (value & (1<<n))


This is usually implemented in x86 code as:


Listing 19.45: x86

; CL=n
MOV REG, 1
SHL REG, CL
AND input_value, REG


19.6.3 Set specific bit (known at compile stage)


Listing 19.46: C/C++

value=value|0x40;


Listing 19.47: x86

OR REG, 40h


Listing 19.48: ARM (ARM mode) and ARM64

ORR R0, R0, #0x40


19.6.4 Set specific bit (specified at runtime).


Listing 19.49: C/C++

value=value|(1<<n);


This is usually implemented in x86 code as:


Listing 19.50: x86

; CL=n
MOV REG, 1
SHL REG, CL
OR input_value, REG


19.6.5 Clear specific bit (known at compile stage)


Just applyANDoperation with the inverted value:


Listing 19.51: C/C++

value=value&(~0x40);


Listing 19.52: x86

AND REG, 0FFFFFFBFh


Listing 19.53: x64

AND REG, 0FFFFFFFFFFFFFFBFh


This is actually leaving all bits set except one.


ARM in ARM mode hasBICinstruction, which works like theNOT+ANDinstruction pair:


Listing 19.54: ARM (ARM mode)

BIC R0, R0, #0x40

Free download pdf