Reverse Engineering for Beginners

(avery) #1
CHAPTER 14. LOOPS CHAPTER 14. LOOPS
CMP R4, #0xA
BLT loc_35C
MOV R0, #0
LDMFD SP!, {R4,PC}

Iteration counteriis to be stored in theR4register.

The“MOV R4, #2”instruction just initializesi.
The“MOV R0, R4”and“BL printing_function”instructions compose the body of the loop, the first instruction
preparing the argument forf()function and the second calling the function.

The“ADD R4, R4, #1”instruction just adds 1 to theivariable at each iteration.

“CMP R4, #0xA”comparesiwith0xA(10). The next instructionBLT(Branch Less Than) jumps ifiis less than 10.


Otherwise, 0 is to be written intoR0(since our function returns 0) and function execution finishes.

Optimizing Keil 6/2013 (Thumb mode)

_main
PUSH {R4,LR}
MOVS R4, #2

loc_132 ; CODE XREF: _main+E
MOVS R0, R4
BL printing_function
ADDS R4, R4, #1
CMP R4, #0xA
BLT loc_132
MOVS R0, #0
POP {R4,PC}

Practically the same.

Optimizing Xcode 4.6.3 (LLVM) (Thumb-2 mode)

_main
PUSH {R4,R7,LR}
MOVW R4, #0x1124 ; "%d\n"
MOVS R1, #2
MOVT.W R4, #0
ADD R7, SP, #4
ADD R4, PC
MOV R0, R4
BLX _printf
MOV R0, R4
MOVS R1, #3
BLX _printf
MOV R0, R4
MOVS R1, #4
BLX _printf
MOV R0, R4
MOVS R1, #5
BLX _printf
MOV R0, R4
MOVS R1, #6
BLX _printf
MOV R0, R4
MOVS R1, #7
BLX _printf
MOV R0, R4
MOVS R1, #8
BLX _printf
MOV R0, R4
MOVS R1, #9
BLX _printf
MOVS R0, #0
POP {R4,R7,PC}
Free download pdf