ARM Assembly Language Examples
ARM Assembly Language Examples
C to ARM Assembler
C:
z = (a << 2) | (b & 15);
ADR r4,a
; get address for a
LDR r0,[r4] ; get value of a
MOV r0,r0,LSL#2 ; perform shift
ADR r4,b
; get address for b
LDR r1,[r4] ; get value of b
AND r1,r1,#15 ; perform AND
ORR r1,r0,r1
; perform OR
ADR r4,z
; get address for z
STR r1,[r4] ; store value for z
C:
if (a < b)
{
x = 5;
y = c + d;
}
else x = c - d;
MOV r0,#5 ;
ADR r4,x
STR r0,[r4] ;
ADR r4,c
LDR r0,[r4] ;
ADR r4,d
LDR r1,[r4] ;
ADD r0,r0,r1
ADR r4,y
STR r0,[r4] ;
B after
;
block
ADR r4,a
LDR r0,[r4]
ADR r4,b
LDR r1,[r4]
CMP r0,r1
BGE fblock
Structure of Assembly
AREA prog1, CODE, READONLY ; Name this block of code as
prog 1
ENTRY
Start
mov r0,#10
mov r1,#3
add r0,r0,#1
stop
b stop
end
Assembler Directives
AREA:
Define a block of data or code
Ex.AREA sectionname,{attr} {attr}
Sectionname: is the name that the section is given.
Attr:
ALIGN = expr the term expr can have any value
from 0 to 31. The section is alined in 2^expr
boundary.
CODE:
The
section
contains
machine
instructions. READONLY is the default.
COMMON: This indicates a common data section.
DATA
:
The
section
contains
data
not
instructions. READWRITE is the default.
READONLY: This indicates that this section
should not be written , and it can therefore , be
placed in read-only memory.
READWRITE: This section can be read from and
Assembler Directives
RN: Register name definition
The RN directive defines a register name for a specified
register.
Syntax:
Name RN expr
Where name is the name to be assigned to the register.
RN is the directive
Expr evaluates to a number from 0 to 15.
Example:
Coeff1 RN 8; R8 as a coeff1
Dest RN R0; defines dest for register 6.
Assembler Directives
EQU Equate a symbol to a numeric constant
The EQU directive gives a symbolic name to a numeric
constant, a register-relative value or a program-relative value.
This directive is similar to #define to define constant in C.
Syntax:
Example:
SRAM_BASE EQU 0x4000 0000
abc EQU 2
XYZ EQU label+8
Assembler Directives
ENTRY Declare an entry point
The ENTRY directive declares an entry point to a
program
Syntax:
ENTRY
Assembler Directives
DCD, DCW and DCD- Allocate Memory and
specify contents
The DCB directive allocates one or more byte of memory
DCW directive allocates one or more halfworlds of
memory
DCD directive allocates one or more words of memory.
Syntax:
{label} DCB expr, {expr
{label} DCW expr, {expr
{label} DCD expr, {expr
Example:
Coeff DCW 0xFE37,0x8ECC
C_string DCB C_String,0
Assembler Directives
ALIGN- ALIGN data or code to appropriate
boundries
The ALIGN directive aligns the current location to a
specified boundary by padding with zeros or NOP
instructions
Syntax:
ALIGN {expr{,offset}}
Example:
ALIGN 4 will align on 4-byte boundary
ALIGN 8 will align on 8-byte boundary
Assembler Directives
SPACE- RESERVE a block of memory
The space directive reserves zeroed block of
memory.
Syntax:
label SPACE expr
Example:
Data1 SPACE 255; defines 255 bytes of zeroed storage.
END :
END of a source file.