Assembly Language for Beginners

(nextflipdebug2) #1

1.14. CONDITIONAL JUMPS


That is how you can get rid of conditional jumps in ARM mode.


Why is this so good? Read here:2.10.1 on page 466.


There is no such feature in x86, except theCMOVccinstruction, it is the same asMOV, but triggered only
when specific flags are set, usually set byCMP.


Optimizing Keil 6/2013 (Thumb mode)


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

.text:00000072 f_signed ; CODE XREF: main+6
.text:00000072 70 B5 PUSH {R4-R6,LR}
.text:00000074 0C 00 MOVS R4, R1
.text:00000076 05 00 MOVS R5, R0
.text:00000078 A0 42 CMP R0, R4
.text:0000007A 02 DD BLE loc_82
.text:0000007C A4 A0 ADR R0, aAB ; "a>b\n"
.text:0000007E 06 F0 B7 F8 BL 2printf
.text:00000082
.text:00000082 loc_82 ; CODE XREF: f_signed+8
.text:00000082 A5 42 CMP R5, R4
.text:00000084 02 D1 BNE loc_8C
.text:00000086 A4 A0 ADR R0, aAB_0 ; "a==b\n"
.text:00000088 06 F0 B2 F8 BL
2printf
.text:0000008C
.text:0000008C loc_8C ; CODE XREF: f_signed+12
.text:0000008C A5 42 CMP R5, R4
.text:0000008E 02 DA BGE locret_96
.text:00000090 A3 A0 ADR R0, aAB_1 ; "a<b\n"
.text:00000092 06 F0 AD F8 BL __2printf
.text:00000096
.text:00000096 locret_96 ; CODE XREF: f_signed+1C
.text:00000096 70 BD POP {R4-R6,PC}
.text:00000096 ; End of function f_signed


OnlyBinstructions in Thumb mode may be supplemented bycondition codes, so the Thumb code looks
more ordinary.


BLEis a normal conditional jumpLess than or Equal,BNE—Not Equal,BGE—Greater than or Equal.


f_unsignedis similar, only other instructions are used while dealing with unsigned values:BLS(Unsigned
lower or same) andBCS(Carry Set (Greater than or equal)).


ARM64: Optimizing GCC (Linaro) 4.9


Listing 1.114: f_signed()

f_signed:
; W0=a, W1=b
cmp w0, w1
bgt .L19 ; Branch if Greater Than (a>b)
beq .L20 ; Branch if Equal (a==b)
bge .L15 ; Branch if Greater than or Equal (a>=b) (impossible here)
; a<b
adrp x0, .LC11 ; "a<b"
add x0, x0, :lo12:.LC11
b puts
.L19:
adrp x0, .LC9 ; "a>b"
add x0, x0, :lo12:.LC9
b puts
.L15: ; impossible to get here
ret
.L20:
adrp x0, .LC10 ; "a==b"
add x0, x0, :lo12:.LC10
b puts

Free download pdf