CHAPTER 18. ARRAYS CHAPTER 18. ARRAYS
Listing 18.36: 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.
18.7.3 MIPS.
Listing 18.37: 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 = month2
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>
.byte 0, 0
aFebruary: .ascii "February"<0>
.byte 0
aMarch: .ascii "March"<0>
.byte 0, 0, 0, 0
aApril: .ascii "April"<0>
.byte 0, 0, 0, 0
aMay: .ascii "May"<0>
.byte 0, 0, 0, 0, 0, 0
aJune: .ascii "June"<0>
.byte 0, 0, 0, 0, 0
aJuly: .ascii "July"<0>
.byte 0, 0, 0, 0, 0
aAugust: .ascii "August"<0>
.byte 0, 0, 0
aSeptember: .ascii "September"<0>
aOctober: .ascii "October"<0>
.byte 0, 0
aNovember: .ascii "November"<0>
.byte 0
aDecember: .ascii "December"<0>
.byte 0, 0, 0, 0, 0, 0, 0, 0, 0
18.7.4 Conclusion.
This is a bit old-school technique to store text strings. You may find a lot of it in Oracle RDBMS, for example. It’s hard to
say if it’s worth doing on modern computers. Nevertheless, it was a good example of arrays, so it was added to this book.