100% found this document useful (1 vote)
1K views

ARM Assembly Language Examples

In this PPT various Assembler directives are shown for the ARM Processor, and Examples of how to convert C languge statements to ARM Assembly is given.

Uploaded by

abhi198808
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views

ARM Assembly Language Examples

In this PPT various Assembler directives are shown for the ARM Processor, and Examples of how to convert C languge statements to ARM Assembly is given.

Uploaded by

abhi198808
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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

for ( i = 0 ; i < 15 ; i++)


{
j = j + j;
}

SUB R0, R0, R0


; i -> R0 and i = 0
start CMP R0, #15
; is i < 15?
ADDLT R1, R1, R1
;j=j+j
ADDLT R0, R0, #1
; i++
BLT start

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

generate value for x


; get address for x
store x
; get address for c
get value of c
; get address for d
get value of d
; compute y
; get address for y
store y
branch around false

; get address for a


; get value of a
; get address for b
; get value for b
; compare a < b
; if a >= b, branch to false block

fblock ADR r4,c


; get address
for c
LDR r0,[r4] ; get value of c
ADR r4,d
; get address for
d
LDR r1,[r4] ; get value for d
SUB r0,r0,r1
; compute a-b
ADR r4,x
; get address for
x
STR r0,[r4] ; store value of x
after

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:

Name EQU expr, {type}


Where name is the Symbolic name
EQU is the directive
Expr is a register relative address, an absolute address, or 32bit integer constant.
Type is a optional.

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

You must specify at least one ENTRY point for a


program. If no ENTRY exists, a warning is
generated at link time.
Example:
AREA prog1, code , readonly
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.

You might also like