Page 79 - ARM 64 Bit Assembly Language
P. 79
64 Chapter 3
shifts the contents of x1 left three bits, adds the result to the contents of x2 and uses the
sum as an address in memory from which it loads eight bytes into x3. Recall that shift-
ing a binary number left by three bits is equivalent to multiplying that number by eight.
This addressing mode is typically used to access an array, where x2 contains the ad-
dress of the beginning of the array, and x1 is an integer index. The integer shift amount
depends on the size of the objects in the array.
This is convenient when the size of the items in an array are powers of two. For exam-
ple, the shift would be lsl #3 for double-words, lsl #2 for words, and lsl #1 for
half-words. For an array of structures, this method is only appropriate if the size of the
structures in the array is a power of two. Many programs use 32-bit integers (words).
For example, int in C is often 32-bits. The following instruction illustrates how to ac-
cessanarrayofwords:
ldr w14, [x2, x3, lsl #2]
where w14 is the register to which the array element indexed by x3 is saved.
To store an item from register x0 into an array of half-words, the following instruction
could be used:
strh w0, [x4, w5, lsl #1]
where x4 holds the 64-bit address of the first byte of the array, and w5 holds the integer
index for the desired array item.
Subroutines often keep data on the stack, including their return addresses and local
variables, if they use them. The following instruction shows how to store a double-word
variable on the stack:
str x0, [sp, x1, lsl #3]
In this instruction x1 is an offset to the local variable, starting from the stack pointer as
the base address, and x0 is the value used to overwrite the local variable on the stack.
If Rm is specified as a 32-bit register (Wm), then the <option> for sign extension can
be applied. The programmer can choose either sign extend word (sxtw) or unsigned
extend word (uxtw). Sign extension and unsigned extension are used to preserve the
values of binary numbers when more bits are used to represent them. Sign extension
replicates the sign bit while unsigned extension uses only zeros to extend the number. If
a 32-bit negative register offset is used to calculate a memory address, then it should be
sign extended:
ldr w1, [x2, w3, sxtw]
In this case, w3 is sign extended to become a 64-bit value, and then that sign-extended
value is added to x3 to form the memory address. w1 is loaded with the word in mem-
ory at the calculated address.
Literal: label
When using a literal load instruction, an address in memory within one megabyte of
the program counter can be calculated. This is possible because the label address is