Assembly Language for Beginners

(Jeff_L) #1

1.16. LOOPS


CMP R4, #0xA
BLT loc_35C
MOV R0, #0
LDMFD SP!, {R4,PC}

Iteration counteriis to be stored in theR4register. TheMOV R4, #2instruction just initializesi. The
MOV R0, R4andBL printing_functioninstructions compose the body of the loop, the first instruction
preparing the argument forf()function and the second calling the function. TheADD R4, R4, #1in-
struction just adds 1 to theivariable at each iteration. CMP R4, #0xAcomparesiwith0xA(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}


In fact, this was in myf()function:

Free download pdf