Reverse Engineering for Beginners

(avery) #1

CHAPTER 7. SCANF() CHAPTER 7. SCANF()


sub rsp, 56
lea rcx, OFFSET FLAT:$SG2924 ; 'Enter X:'
call printf
lea rdx, QWORD PTR x$[rsp]
lea rcx, OFFSET FLAT:$SG2926 ; '%d'
call scanf
cmp eax, 1
jne SHORT $LN2@main
mov edx, DWORD PTR x$[rsp]
lea rcx, OFFSET FLAT:$SG2927 ; 'You entered %d...'
call printf
jmp SHORT $LN1@main
$LN2@main:
lea rcx, OFFSET FLAT:$SG2929 ; 'What you entered? Huh?'
call printf
$LN1@main:
; return 0
xor eax, eax
add rsp, 56
ret 0
main ENDP
_TEXT ENDS
END


7.3.6 ARM.


ARM: Optimizing Keil 6/2013 (Thumb mode)


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

var_8 = -8


PUSH {R3,LR}
ADR R0, aEnterX ; "Enter X:\n"
BL __2printf
MOV R1, SP
ADR R0, aD ; "%d"
BL __0scanf
CMP R0, #1
BEQ loc_1E
ADR R0, aWhatYouEntered ; "What you entered? Huh?\n"
BL __2printf

loc_1A ; CODE XREF: main+26
MOVS R0, #0
POP {R3,PC}


loc_1E ; CODE XREF: main+12
LDR R1, [SP,#8+var_8]
ADR R0, aYouEnteredD_ ; "You entered %d...\n"
BL
2printf
B loc_1A


The new instructions here areCMPandBEQ^9.


CMPis analogous to the x86 instruction with the same name, it subtracts one of the arguments from the other and updates
the conditional flags if needed.


BEQjumps to another address if the operands were equal to each other, or, if the result of the last computation was 0, or if
the Z flag is 1. It behaves asJZin x86.


Everything else is simple: the execution flow forks in two branches, then the branches converge at the point where 0 is
written into theR0as a function return value, and then the function ends.


ARM64


(^9) (PowerPC, ARM) Branch if Equal

Free download pdf