Assembly Language Programming

Pre-indexed Addressing

Although copying a single word of data is useful, frequently programs will need to shift large blocks of data. The copy program example could be expanded as follows:

copycode ADR r1, SOURCEDATA ; the value of r1 points to the SOURCEDATA location in memory
  ADR r2, DESTDATA ; the value of r2 points to the DESTDATA location in memory
  LDR r0, [r1] ; load value at memory location pointed to by r1 into r0 (SOURCEDATA)
  STR r0, [r2] ; store value in r0 to memory location pointed to by r2 (DESTDATA)
  ADD r1, r1, #4 ; step r1's value to next word location
  ADD r2, r2, #4 ; step r2's value to next word location
  LDR r0, [r1] ; load value at new memory location pointed to by r1 into r0
  STR r0, [r2] ; store value in r0 to new memory location pointed to by r2
  .    
SOURCEDATA   . ; source of data
    .  
DESTDATA   . ; destination for the data

The pre-indexed addressing mode provides a means of simplifying the process be eliminating the ADD instructions.

LDR r0, [r1, #4] ; load r0 with the value found at the memory
    ; location whose address is the value of r1 + 4

In the example, r1 contains the base address and #4 is the offset. The sum of the base address and the offset yields the effective address.

copycode ADR r1, SOURCEDATA ; the value of r1 points to the SOURCEDATA location in memory
  ADR r2, DESTDATA ; the value of r2 points to the DESTDATA location in memory
  LDR r0, [r1] ; load value at memory location pointed to by r1 into r0 (SOURCEDATA)
  STR r0, [r2] ; store value in r0 to memory location pointed to by r2 (DESTDATA)
  LDR r0, [r1, #4] ; load value at new memory location pointed to by the effective address of r1+4
  STR r0, [r2, #4] ; store value in r0 to new memory location pointed to by the effective address of r2+4
  .    
SOURCEDATA   . ; source of data
    .  
DESTDATA   . ; destination for the data

In pre-indexed addressing, the base address register (r1 and r2 in the example) are not modified. Occasionally it is useful to update the base address register such that it points to the new address. This is pre-indexed addressing with auto-indexing:

LDR r0, [r1, #4]! ; load value at memory location pointed to by the effective address of r1+4.
    ; Then, increment r1, such that r1 := r1 +4

The "!" suffix on the effective address indicates that the base register should be incremented after the data transfer.