Reverse Engineering for Beginners

(avery) #1

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


Listing 19.18: Optimizing GCC (Linaro) 4.9

f:
and w0, w0, -513 ; 0xFFFFFFFFFFFFFDFF
orr w0, w0, 16384 ; 0x4000
ret


19.2.7 ARM64: Non-optimizing GCC (Linaro) 4.9


Non-optimizing GCC generates more redundant code, but works just like optimized:


Listing 19.19: Non-optimizing GCC (Linaro) 4.9

f:
sub sp, sp, #32
str w0, [sp,12]
ldr w0, [sp,12]
str w0, [sp,28]
ldr w0, [sp,28]
orr w0, w0, 16384 ; 0x4000
str w0, [sp,28]
ldr w0, [sp,28]
and w0, w0, -513 ; 0xFFFFFFFFFFFFFDFF
str w0, [sp,28]
ldr w0, [sp,28]
add sp, sp, 32
ret


19.2.8 MIPS.


Listing 19.20: Optimizing GCC 4.4.5 (IDA)

f:
; $a0=a
ori $a0, 0x4000
; $a0=a|0x4000
li $v0, 0xFFFFFDFF
jr $ra
and $v0, $a0, $v0
; at finish: $v0 = $a0&$v0 = a|0x4000 & 0xFFFFFDFF


ORIis, of course, the OR operation. “I” in the instruction name mean that the value is embedded in the machine code.


But after that we haveAND. There was no way to useANDIbecause it’s not possible to embed the 0xFFFFFDFF number in
a single instruction, so the compiler has to load 0xFFFFFDFF into register $V0 first and then generatesANDwhich takes all
its values from registers.


19.3 Shifts


Bit shifts in C/C++ are implemented using≪and≫operators.


The x86ISAhas the SHL (SHift Left) and SHR (SHift Right) instructions for this.


Shift instructions are often used in division and multiplications by powers of two: 2 n(e.g., 1, 2, 4, 8, etc):16.1.2 on
page 199,16.2.1 on page 204.


Shifting operations are also so important because they are often used for specific bit isolation or for constructing a value of
several scattered bits.


19.4 Setting and clearing specific bits:FPUexample


Here is how bits are located in thefloattype in IEEE 754 form:

Free download pdf