Reverse Engineering for Beginners

(avery) #1

CHAPTER 24. 64-BIT VALUES IN 32-BIT ENVIRONMENT CHAPTER 24. 64-BIT VALUES IN 32-BIT ENVIRONMENT


ORRS r0,r0,r2
LSRS r1,r1,#7
BX lr
ENDP

24.4.3 MIPS


GCC for MIPS follows the same algorithm as Keil does for Thumb mode:


Listing 24.18: Optimizing GCC 4.4.5 (IDA)

f:
sll $v0, $a0, 25
srl $v1, $a1, 7
or $v1, $v0, $v1
jr $ra
srl $v0, $a0, 7


24.5 Converting 32-bit value into 64-bit one


#include <stdint.h>


int64_t f (int32_t a)
{
return a;
};


24.5.1 x86


Listing 24.19: Optimizing MSVC 2012

_a$ = 8
_f PROC
mov eax, DWORD PTR _a$[esp-4]
cdq
ret 0
_f ENDP


Here we also run into necessity to extend a 32-bit signed value into a 64-bit signed one. Unsigned values are converted
straightforwardly: all bits in the higher part must be set to 0. But this is not appropriate for signed data types: the sign
has to be copied into the higher part of the resulting number. TheCDQinstruction does that here, it takes its input value
inEAX, extends it to 64-bit and leaves it in theEDX:EAXregister pair. In other words,CDQgets the number sign fromEAX
(by getting the most significant bit inEAX), and depending of it, sets all 32 bits inEDXto 0 or 1. Its operation is somewhat
similar to theMOVSXinstruction.


24.5.2 ARM.


Listing 24.20: Optimizing Keil 6/2013 (ARM mode)

||f|| PROC
ASR r1,r0,#31
BX lr
ENDP


Keil for ARM is different: it just arithmetically shifts right the input value by 31 bits. As we know, the sign bit isMSB, and
the arithmetical shift copies the sign bit into the “emerged” bits. So after “ASR r1,r0,#31”, R1 containing 0xFFFFFFFF if the
input value was negative and 0 otherwise. R1 contains the high part of the resulting 64-bit value.


In other words, this code just copies theMSB(sign bit) from the input value in R0 to all bits of the high 32-bit part of the
resulting 64-bit value.

Free download pdf