Reverse Engineering for Beginners

(avery) #1

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


BX lr
ENDP

So far so good. ARM has theBICinstruction, which explicitly clears specific bit(s).EORis the ARM instruction name for
XOR(“Exclusive OR”).


Optimizing Keil 6/2013 (Thumb mode)


Listing 19.25: Optimizing Keil 6/2013 (Thumb mode)

my_abs PROC
LSLS r0,r0,#1
; r0=i<<1
LSRS r0,r0,#1
; r0=(i<<1)>>1
BX lr
ENDP


set_sign PROC
MOVS r1,#1
; r1=1
LSLS r1,r1,#31
; r1=1<<31=0x80000000
ORRS r0,r0,r1
; r0=r0 | 0x80000000
BX lr
ENDP


negate PROC
MOVS r1,#1
; r1=1
LSLS r1,r1,#31
; r1=1<<31=0x80000000
EORS r0,r0,r1
; r0=r0 ^ 0x80000000
BX lr
ENDP


Thumb mode in ARM offers 16-bit instructions and not much data can be encoded in them, so here a MOVS/LSLS instruction
pair is used for forming the 0x80000000 constant. It works like this: 1 <<31 = 0x 80000000.


The code of my_abs is weird and it effectively works like this expression:(i<<1)>> 1. This statement looks meaningless.
But nevertheless, wheninput<< 1 is executed, theMSB(sign bit) is just dropped. When the subsequentresult>> 1
statement is executed, all bits are now in their own places, butMSBis zero, because all “new” bits appearing from the shift
operations are always zeroes. That is how theLSLS/LSRSinstruction pair clearsMSB.


Optimizing GCC 4.6.3 (Raspberry Pi, ARM mode)


Listing 19.26: Optimizing GCC 4.6.3 for Raspberry Pi (ARM mode)

my_abs
; copy from S0 to R2:
FMRS R2, S0
; clear bit:
BIC R3, R2, #0x80000000
; copy from R3 to S0:
FMSR S0, R3
BX LR


set_sign
; copy from S0 to R2:
FMRS R2, S0
; do OR:
ORR R3, R2, #0x80000000
; copy from R3 to S0:
FMSR S0, R3
BX LR

Free download pdf