Arm Assembly Language Programming
Arm Assembly Language Programming
33
label
opcode
Label
Label is an optional first field of an assembly statement. Labels are alphanumeric names used
to define the starting location of a block of statements. Labels can be subsequently used in
our program as an operand of some other instruction. When creating the executable file the
assembler will replace the label with the assigned value. Labels must be unique in the executable
file because an identical label encountered by the Assembler will generate an error.
ARM assembler has reserved first character of a line for the label field and it should be left
blank for the instructions with no labels. In some compilers, labels can be optionally ended
with colon(:) but it is not accepted by the ARM assembler as an end of the label field.
Defining appropriate labels makes your program look more legible. Program location can be
easily found and remembered using labels. It is easier to use certain functionality of your
program in an entirely different code .i.e., your code can be used as a library program. You
do not have to figure out memory addresses because it is a tedious task especially when the
instruction size in your microcontroller is not fixed.
Opcode (Mnemonics)
Opcode is the second field in assembly language instruction. Assembly language consists of
mnemonics, each corresponding to a machine instruction. Using a mnemonic you can decide
34
what operation you want to perform on the operands. Assembler must translate each mnemonic
opcode into their binary equivalent.
Operands
Next to the opcode is the operand field which might contain different number of operands.
Some of the instructions in Cortex-M3 will have no operand while other might have as many as
four operands. The number of operands in an instruction depends on the type of instruction,
and the syntax format of the operand can also be different. Normally, the first operand is the
destination of the operation.
Comments
Comments are messages intended only for human consumption. They have no effect on the
translation process and indeed are not acted on by the ARM Assembler. The comment field
of an assembly language instruction is also optional. A semicolon signies that the rest of the
line is a comment and is to be ignored by the assembler. If the semicolon is the rst non-blank
character on the line, the entire line is ignored. If the semicolon follows the operands of an
instruction, then only the comment is ignored by the assembler. The purpose of comments is
to make the program more comprehensible to the human reader. If you want to modify the
program in response to a product update and you havent used that code before then comments
go a long way to helping comprehension.
F i r s t ARM A s s e m b l y l a n g u a g e p r o g r a m
PRESERVE8
THUMB
; m a r k s t h e THUMB mode o f o p e r a t i o n
AREA
| . t e x t | , CODE , READONLY , ALIGN =2
ENTRY
; s t a r t i n g p o i n t o f t h e code e x e c u t i o n
35
EXPORT _ _ m a i n
;
__main
; address
; U s e r Code S t a r t s
END
; End o f t h e
d e c l a r a t i o n o f i d e n t i f i e r main
o f t h e main f u n c t i o n
from the next l i n e
p r o g r a m , m a t c h e d w i t h ENTRY k e y w o r d
; T h i s i s t h e f i r s t ARM A s s e m b l y l a n g u a g e p r o g r a m . D e s c r i b e t h e
; f u n c t i o n a l i t y o f t h e p r o g r a m a t t h e t o p . You c a n a l s o i n c l u d e
; t h e i n f o r e g a r d i n g t h e a u t h o r and t h e d a t e
; ;;
Directives
PRESERVE8
THUMB
; M a r k s t h e THUMB mode o f o p e r a t i o n
; ;;;;;;;;;
R5 , #0 x 1 2 3 4
R3 , #0 x 1 2 3 4
R6 , R5 , R3
; S t o r e some a r b i t r a r y n u m b e r s
B
END
STOP
; Endless loop
; End o f t h e p r o g r a m , m a t c h e d w i t h
; ENTRY k e y w o r d
; Add t h e v a l u e s i n R5 a n d R3 a n d s t o r e t h e
; r e s u l t i n R6
STOP
; ;;
Directives
PRESERVE8
THUMB
; M a r k s t h e THUMB mode o f o p e r a t i o n
Stack
EQU
0 x00000100
; D e f i n e s t a c k s i z e t o be 2 5 6 b y e s
; A l l o c a t e space f o r the s t a c k .
AREA STACK , N O I N I T , READWRITE , ALIGN =3
36
StackMem
;
SPACE
Stack
I n i t i a l i z e the two e n t r i e s o f v e c t o r t a b l e .
AREA RESET , DATA , READONLY
EXPORT _ _ V e c t o r s
__Vectors
DCD S t a c k M e m + S t a c k
DCD R e s e t _ H a n d l e r
; s t a c k p o i n t e r v a l u e when s t a c k i s e m p t y
; reset vector
ALIGN
; ;;;;;;;;;
AREA
| . t e x t | , CODE , READONLY , ALIGN =2
ENTRY
; Marks t h e s t a r t i n g p o i n t o f t h e code e x e c u t i o n
EXPORT R e s e t _ H a n d l e r
Reset_Handler
; U s e r Code S t a r t s f r o m t h e n e x t l i n e
MOV
MOV
ADD
R5 , #0 x 1 2 3 4
R3 , #0 x 1 2 3 4
R6 , R5 , R3
; Move some a r b i t r a r y n u m b e r i n R5
B
END
STOP
; Endless loop
; End o f t h e p r o g r a m , m a t c h e d w i t h
; ENTRY k e y w o r d
; Add t h e v a l u e s i n R5 a n d R3 a n d s t o r e t h e
; r e s u l t i n R6
STOP
Assembler Directives
Every program to be executed by a computer is a sequence of statements that can be read by
the humans. An assembler must differentiate between the statements that will be converted
into executable code and that instruct the assembler to perform some specific function.
Assembler directives are commands to the assembler that direct the assembly process. Assembler 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:
1. Assign the program to certain areas in memory.
2. Define symbols.
3. Designate areas of memory for data storage.
4. Place tables or other fixed data in memory.
5. Allow references to other programs.
37
Attribute
CODE
DATA
READONLY
READWRITE
NOINIT
Explanation
Contains machine instructions. READONLY is the default.
Contains data, not instructions. READWRITE is the default.
Indicates that this area should not be written to.
Indicates that this area may be read from or written to.
Indicates that the data area is initialized to zero. It contains only
reservation directives with no initialized values.
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
comma delimited attributes that can be used with AREA directive. Some of them are discussed
below.
Example 3.3
AREA
E x a m p l e , CODE , READONLY
; user code
; An e x a m p l e c o d e s e c t i o n .
Example 3.4
AREA
ENTRY
Start
38
; user code
END
; I n f o r m s t h e a s s e m b l e r a b o u t t h e end o f a s o u r c e
file
Example 3.5
AREA
DoAdd
E x a m p l e , CODE , READONLY
IMPORT U s e r _ C o d e
;
;
EXPORT DoAdd
;
;
;
ADD
R0 , R0 , R1
I m p o r t t h e f u n c t i o n name f r o m
other source f i l e .
E x p o r t t h e f u n c t i o n name
t o be u s e d b y e x t e r n a l
modules .
Example 3.6
39
AREA
start
C h a n g e S t a t e , CODE , READONLY
ARM
; T h i s s e c t i o n s t a r t s i n ARM s t a t e
LDR
r 0 , = s t a r t +1 ; L o a d t h e a d d r e s s a n d s e t t h e
; least significant bit
BX
r0
; B r a n c h and exchange i n s t r u c t i o n s e t s
THUMB
MOV
r1 , # 1 0
; N o t n e c e s s a r i l y i n same s e c t i o n
; 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
; 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.
Example 3.7
40
DCD
0, 0, 0
; D e f i n e s 3 words
initialized
to zeros
SPACE Directive
The SPACE directive reserves a zeroed block of memory. ALIGN directive must be used to
align any code following a SPACE directive.
Example 3.8
AREA
data1