Assembly Language for Beginners

(nextflipdebug2) #1

1.20. ARRAYS


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

; R0 = month
MOVS r1,#0xa
; R1 = 10
MULS r0,r1,r0
; R0 = R1R0 = 10month
LDR r1,|L0.68|
; R1 = pointer to the table
ADDS r0,r0,r1
; R0 = R0+R1 = 10*month + pointer to the table
BX lr


Optimizing Keil for ARM mode uses add and shift operations:


Listing 1.259: Optimizing Keil 6/2013 (ARM mode)

; R0 = month
LDR r1,|L0.104|
; R1 = pointer to the table
ADD r0,r0,r0,LSL #2
; R0 = R0+R0<<2 = R0+R04 = month5
ADD r0,r1,r0,LSL #1
; R0 = R1+R0<<2 = pointer to the table + month52 = pointer to the table + month*10
BX lr


ARM64


Listing 1.260: Optimizing GCC 4.9 ARM64

; W0 = month
sxtw x0, w0
; X0 = sign-extended input value
adrp x1, .LANCHOR1
add x1, x1, :lo12:.LANCHOR1
; X1 = pointer to the table
add x0, x0, x0, lsl 2
; X0 = X0+X0<<2 = X0+X04 = X05
add x0, x1, x0, lsl 1
; X0 = X1+X0<<1 = X1+X02 = pointer to the table + X010
ret


SXTWis used for sign-extension and promoting input 32-bit value into a 64-bit one and storing it in X0.


ADRP/ADDpair is used for loading the address of the table.


TheADDinstructions also has aLSLsuffix, which helps with multiplications.


MIPS


Listing 1.261: Optimizing GCC 4.4.5 (IDA)
.globl get_month2
get_month2:
; $a0=month
sll $v0, $a0, 3
; $v0 = $a0<<3 = month8
sll $a0, 1
; $a0 = $a0<<1 = month
2
addu $a0, $v0
; $a0 = month2+month8 = month*10
; load address of the table:
la $v0, month2
; sum up table address and index we calculated and return:
jr $ra
addu $v0, $a0


month2: .ascii "January"<0>

Free download pdf