Assembly Language for Beginners

(nextflipdebug2) #1
1.14. CONDITIONAL JUMPS
.text:000000B8 f_signed ; CODE XREF: main+C
.text:000000B8 70 40 2D E9 STMFD SP!, {R4-R6,LR}
.text:000000BC 01 40 A0 E1 MOV R4, R1
.text:000000C0 04 00 50 E1 CMP R0, R4
.text:000000C4 00 50 A0 E1 MOV R5, R0
.text:000000C8 1A 0E 8F C2 ADRGT R0, aAB ; "a>b\n"
.text:000000CC A1 18 00 CB BLGT __2printf
.text:000000D0 04 00 55 E1 CMP R5, R4
.text:000000D4 67 0F 8F 02 ADREQ R0, aAB_0 ; "a==b\n"
.text:000000D8 9E 18 00 0B BLEQ __2printf
.text:000000DC 04 00 55 E1 CMP R5, R4
.text:000000E0 70 80 BD A8 LDMGEFD SP!, {R4-R6,PC}
.text:000000E4 70 40 BD E8 LDMFD SP!, {R4-R6,LR}
.text:000000E8 19 0E 8F E2 ADR R0, aAB_1 ; "a<b\n"
.text:000000EC 99 18 00 EA B __2printf
.text:000000EC ; End of function f_signed

Many instructions in ARM mode could be executed only when specific flags are set. E.g. this is often used
when comparing numbers.

Forinstance,theADDinstructionisinfactnamedADDALinternally,whereALstandsforAlways,i.e.,execute
always. The predicates are encoded in 4 high bits of the 32-bit ARM instructions (condition field). The
Binstruction for unconditional jumping is in fact conditional and encoded just like any other conditional
jump, but hasALin thecondition field, and it impliesexecute ALways, ignoring flags.

TheADRGTinstruction works just likeADRbut executes only in case the previousCMPinstruction founds
one of the numbers greater than the another, while comparing the two (Greater Than).

The nextBLGTinstruction behaves exactly asBLand is triggered only if the result of the comparison
has been (Greater Than). ADRGTwrites a pointer to the stringa>b\nintoR0andBLGTcallsprintf().
Therefore, instructions suffixed with-GTare to execute only in case the value inR0(which isa) is bigger
than the value inR4(which isb).

Moving forward we see theADREQandBLEQinstructions. They behave just likeADRandBL, but are to be
executed only if operands were equal to each other during the last comparison. AnotherCMPis located
before them (because theprintf()execution may have tampered the flags).

Then we seeLDMGEFD, this instruction works just likeLDMFD^91 , but is triggered only when one of the values
is greater or equal than the other (Greater or Equal). TheLDMGEFD SP!, {R4-R6,PC}instruction acts like
a function epilogue, but it will be triggered only ifa>=b, and only then the function execution will finish.

But if that condition is not satisfied, i.e.,a<b, then the control flow will continue to the next
“LDMFD SP!, {R4-R6,LR}”instruction, which is one more function epilogue. This instruction restores not
only theR4-R6registers state, but alsoLRinstead ofPC, thus, it does not return from the function. The
last two instructions callprintf()with the string «a<b\n» as a sole argument. We already examined an
unconditionaljumptotheprintf()functioninsteadoffunctionreturnin«printf()withseveralarguments»
section (1.8.2 on page 54).


f_unsignedis similar, only theADRHI,BLHI, andLDMCSFDinstructions are used there, these predicates
(HI = Unsigned higher, CS = Carry Set (greater than or equal)) are analogous to those examined before,
but for unsigned values.

There is not much new in themain()function for us:

Listing 1.112:main()
.text:00000128 EXPORT main
.text:00000128 main
.text:00000128 10 40 2D E9 STMFD SP!, {R4,LR}
.text:0000012C 02 10 A0 E3 MOV R1, #2
.text:00000130 01 00 A0 E3 MOV R0, #1
.text:00000134 DF FF FF EB BL f_signed
.text:00000138 02 10 A0 E3 MOV R1, #2
.text:0000013C 01 00 A0 E3 MOV R0, #1
.text:00000140 EA FF FF EB BL f_unsigned
.text:00000144 00 00 A0 E3 MOV R0, #0
.text:00000148 10 80 BD E8 LDMFD SP!, {R4,PC}
.text:00000148 ; End of function main

(^91) LDMFD

Free download pdf