ARM Addressing Modes
ARM Addressing Modes
ARM Addressing Modes
1
R0 to R12 are the general-purpose registers.
R13 is reserved for the programmer to use it as the stack pointer.
R14 is the link register which stores a subroutine return address.
R15 contains the program counter and is accessible by the programmer.
2
Literal Addressing Mode
Examples Meaning
------------------------------------------------------------------------
CMP R0, #22
------------------------------------------------------------------------
ADD R1, R2, #18
------------------------------------------------------------------------
MOV R1, #30
------------------------------------------------------------------------
MOV R1, #0xFF
------------------------------------------------------------------------
CMN R0, #6400 ; R0 + #6400, update the N, Z, C and V flags
------------------------------------------------------------------------
CMPGT SP, R7, LSL #2 ; update the N, Z, C and V flags
------------------------------------------------------------------------
Register indirect addressing mode requires three read operations to access an operand. It is very
important because the content of the register containing the pointer to the operand can be modified at
runtime. Therefore, the address is a variable that allows the access to the data structure like arrays.
3
Register Indirect Addressing with an Offset
ARM supports a memory-addressing mode where the effective address of an operand is computed
by adding the content of a register and a literal offset coded into load/store instruction. For example,
4
ARM's Load and Store Encoding Format
The following picture illustrates the encoding format of the ARM's load and store instructions, which is
included in the lab material for your reference. Memory access operations have a conditional
execution field in bit 31, 03, 29, and 28. The load and store instructions can be conditionally executed
depending on a condition specified in the instruction. Now look at the following examples:
CMP R1, R2
---------------------------------------------------------------
LDREQ R3, [R4]
---------------------------------------------------------------
LDRNE R3, [R5]
5
Summary of ARM's Indexed Addessing Modes
;;; Directives
PRESERVE8
THUMB
__Vectors
DCD 0x20001000 ; stack pointer value when stack is empty
DCD Reset_Handler ; reset vector
ALIGN
N DCD 5
NUM1 DCD 3, -7, 2, -2, 10
POINTER DCD NUM1
; The program
; Linker requires Reset_Handler
6
AREA MYCODE, CODE, READONLY
ENTRY
EXPORT Reset_Handler
Reset_Handler
LOOP
LDR R3, [R2], #4 ; load value from array,
; increment array pointer to next word
ADD R0, R0, R3 ; add value from array to accumulator
SUBS R1, R1, #1 ; decrement work counter
BGT LOOP ; keep looping until counter is zero
LDR R4, SUMP ; get memory address to store sum
STR R0, [R4] ; store answer
STOP
B STOP
END
Another Example
;;; Directives
PRESERVE8
THUMB
__Vectors
DCD 0x20001000 ; stack pointer value when stack is empty
DCD Reset_Handler ; reset vector
7
ALIGN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Character array - string
; This type of format will construct a C string and null terminate.
; This means you can tell when the string ends
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
string1
DCB "Hello world!",0
; The program
; Linker requires Reset_Handler
ENTRY
EXPORT Reset_Handler
Reset_Handler
LDR R0, = string1 ; Load the address of string1 into the register R0
loopCount
LDRB R2, [R0], #1 ; Load the character from the address R0 contains
; and update the pointer R0
; using Post-indexed addressing mode
B loopCount
countDone
B countDone
8
Lab work:
Program#1:
Write an ARM assembly language program AddGT.s to add up all the numbers that are
great than 5 in the number array NUM1. Look at the following given code for more details
and complete it.
;The semicolon is used to lead an inline documentation
;When you write your program, you could have your info at the top document lock
;For Example: Your Name, Student Number, what the program is for, and what it does
etc.
;;; Directives
PRESERVE8
THUMB
__Vectors
DCD 0x20001000 ; stack pointer value when stack is empty
DCD Reset_Handler ; reset vector
ALIGN
; The program
; Linker requires Reset_Handler
ENTRY
EXPORT Reset_Handler
Reset_Handler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Program#2:
Write an ARM assembly language program Min-Max.s to find the maximum value and the
minimum value in the number array NUM1. Look at the following given code for more
details and complete it.
;The semicolon is uded to lead an inline documentation
;When you write your program, you could have your info at the top document lock
;For Example: Your Name, Student Number, what the program is for, and what it does
etc.
;;; Directives
PRESERVE8
THUMB
__Vectors
DCD 0x20001000 ; stack pointer value when stack is empty
DCD Reset_Handler ; reset vector
ALIGN
N DCD 12
NUM1 DCD 3, -7, 2, -2, 10, 20, 30, 15, 32, 8, 64, 66
POINTER DCD NUM1
; The program
; Linker requires Reset_Handler
ENTRY
EXPORT Reset_Handler
10
Reset_Handler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11