Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Lab Objective
2. Load and store instructions to transfer data items between ARM registers and memory
Assembler Directives
Assembler directives are commands to the assembler that direct the assembly process. Assem-
bler directives are also called pseudo opcodes or pseudo-operations. They are executed by the
assembler at assembly time not by the processor at run time. Machine code is not generated
for assembler directives as they are not directly translated to machine language. Some tasks
performed by these directives are:
ARM assembler supports a large number of assembler directives but in this lab manual we will
discuss only some important directives which are required for this lab.
AREA Directive
AREA directive allows the programmer to specify the memory location to store code and data.
Depending on the memory configuration of your device, code and data can reside in different
areas of memory. A name must be specified for an area directive. There are several optional
40
41
comma delimited attributes that can be used with AREA directive. Some of them are disscussed
below.
Attribute Explanation
CODE Contains machine instructions. READONLY is the default.
DATA Contains data, not instructions. READWRITE is the de-
fault.
READONLY Indicates that this area should not be written to.
READWRITE Indicates that this area may be read from or written to.
NOINIT Indicates that the data area is initialized to zero. It contains
only reservation directives with no initialized values.
The first instruction to be executed within an application is marked by the ENTRY directive.
Entry point must be specified for every assembly language program. An application can contain
only a single entry point and so in a multi-source module application, only a single module will
contain an ENTRY directive.
This directive causes the assembler to stop processing the current source file. Every assembly
language source module must therefore finish with this directive.
AREA MyCode , CODE, READONLY
ENTRY ; Entry p o i n t f o r t h e a p p l i c a t i o n
Start
; u s e r code
A project may contain multiple source files. You may need to use a symbol in a source file that
is defined in another source file. In order for a symbol to be found by a different program file,
we need to declare that symbol name as a global variable. The EXPORT directive declares a
symbol that can be used in different program files. GLOBAL is a synonym for EXPORT.
The IMPORT directive provides the assembler with a name that is not defined in the current
assembly.
AREA Example , CODE, READONLY
IMPORT User Code ; Import t h e f u n c t i o n name from
; other source f i l e .
EXPORT DoAdd ; Export t h e f u n c t i o n name
42 CHAPTER 5. ASSEMBLER DIRECTIVES AND MEMORY ACCESS INSTRUCTIONS
; t o be used by e x t e r n a l
; modules .
DoAdd ADD R0 , R0 , R1
The ARM directive instructs the assembler to interpret subsequent instructions as 32-bit ARM
instructions. If necessary, it also inserts up to three bytes of padding to align to the next word
boundary. The ARM directive and the CODE32 directive are synonyms.
The THUMB directive instructs the assembler to interpret subsequent instructions as 16-bit
Thumb instructions. If necessary, it also inserts a byte of padding to align to the next halfword
boundary.
In files that contain a mixture of ARM and Thumb code Use THUMB when changing from ARM
state to Thumb state. THUMB must precede any Thumb code. Use ARM when changing from
Thumb state to ARM state. ARM must precede any ARM code.
AREA ChangeState , CODE, READONLY
ARM
; This s e c t i o n s t a r t s i n ARM s t a t e
LDR r0 ,= s t a r t +1 ; Load t h e a d d r e s s and s e t t h e
; least significant bit
BX r0 ; Branch and exchange i n s t r u c t i o n s e t s
; Not n e c e s s a r i l y i n same s e c t i o n
THUMB ; F o l l o w i n g i n s t r u c t i o n s a r e Thumb
start MOV r1 ,#10 ; Thumb i n s t r u c t i o n s
ALIGN Directive
Use of ALIGN ensures that your code is correctly aligned. By default, the ALIGN directive
aligns the current location within the code to a word (4-byte) boundary. ALIGN 2 can also be
used to align on a halfword (2-byte) boundary in Thumb code. As a general rule it is safer to
use ALIGN frequently through your code.
PRESERVE8 Directive
The PRESERVE8 directive specifies that the current file preserves 8-byte alignment of the stack.
LDRD and STRD instructions (double-word transfers) only work correctly if the address they
access is 8-byte aligned. If your code preserves 8-byte alignment of the stack, use PRESERVE8
to inform the linker. The linker ensures that any code that requires 8-byte alignment of the stack
is only called, directly or indirectly, by code that preserves 8-byte alignment of the stack.
43
ARM assembler supports different data definition directives to insert constants in assembly
code. This directive allows the programmer to enter fixed data into the program memory and
treats that data as a permanent part of the program. Different variants of these directives
are:
SPACE Directive
The SPACE directive reserves a zeroed block of memory. ALIGN directive must be used to
align any code following a SPACE directive.
AREA MyData , DATA, READWRITE
data1 SPACE 255 ; d e f i n e s 255 b y t e s o f z e r o e d s t o r e
ARM is a load/store architecture. It does not support memory to memory data processing
operations. All data processing operations are executed in the registers i.e., data values needed
for an operation must be moved into registers before using them. We need instructions that
interact with memory to move data from memory to registers and vice versa. ARM has three
sets of instructions which interact with main memory.
In this lab manual we will discuss only single register data transfer and also different addressing
modes that that can be used to move the contents from memory to registers and vice versa.
The basic data transfer instructions supported by ARM assembler can load and store different
size of data from and to the memory respectively.
44 CHAPTER 5. ASSEMBLER DIRECTIVES AND MEMORY ACCESS INSTRUCTIONS
EXPORT main
main
; User code s t a r t s from t h e next l i n e ;
MOV R0 , #0x2D
MOV R1 , #0x40
ALIGN
LDR instruction can also be used to load any constant in the registers. It can construct any
32-bit numeric constant in a single instruction. It can be used to generate constants that are
out of range of the MOV and MVN instructions.
EXPORT main
main
; User code s t a r t s from t h e next l i n e ;
MOV R0 , #0
MUL R3 , R1 , R2
LSRS R3 , #9 ; L o g i c a l s h i f t r i g h t with f l a g s update
STR R3 , [ R4 ] ; S t o r e t h e c o n t e n t s o f R3 t o memory
; a d d r e s s a l r e a d y l o a d e d i n R4
STOP
B STOP
END
These instructions provide the most flexible way to transfer single data items between an ARM
register and memory. The data item may be a byte, a 16-bit half-word, a 32-bit word or a
64-bit double word. An optional modifier should be added to access the appropriate data type.
Following table shows the data types available and their ranges.
EXPORT main
main
; User code s t a r t s from t h e next l i n e ;
LDR R0 , =High
LDR R1 , =Low
LDR R2 , =R e s u l t
LDR R5 , =R e s u l t 1
MOV R3 , #0x5
MOV R4 , #0x8
LSL R3 , #4 ; S h i f t t h e l o w e r n i b b l e t o upper n i b b l e o f t h e l e a s t
; s i g n i f i c a n t byte
STRB R3 , [ R0 ]
STRB R4 , [ R1 ]
ORR R3 , R3 , R4 ; S e t l o w e r f b i t s o f R3 e q u a l t o l o w e r f o u r b i t s o f R4
STRB R3 , [ R2 ]
STR R3 , [ R5 ] ; Takes 32 b i t s t o s t o r e t h e r e s u l t
ALIGN
STOP
B STOP
END
Now, in the following example we will consider different addressing modes we can use to access
the memory using load and store instructions.
EXPORT main
main
; User code s t a r t s from t h e next l i n e ;
MOV R0 , #0x4A8
MOV R1 , #0x761
MOV R4 , #0x8D
MOV R3 , #0x0C
SUB R1 , R1 , R0
; O f f s e t Addressing
STR R1 , [ R2 ] ; S t o r e t h e 32 b i t c o n t e n t s o f R1 r e g i s t e r a t
; t h e a d d r e s s l o a d e d i n R2 . The c o n t e n t s o f
; R1 d o e s not change
STR R1 , [ R2 , R3 ] ; S t o r e t h e c o n t e n t s o f R1 a t memory a d d r e s s
; c a l c u l a t e d by adding v a l u e i n t h e b a s e
; r e g i s t e r R2 t o t h e v a l u e i n t h e r e g i s t e r R3 .
; Both R2 and R3 remain unchanged .
; Posti n d e x A d d r e s s i n g
STR R4 , [ R2 ] , #24
; Prei n d e x A d d r e s s i n g
48 CHAPTER 5. ASSEMBLER DIRECTIVES AND MEMORY ACCESS INSTRUCTIONS
STOP
B STOP
END