Sei sulla pagina 1di 6

ARM Assembly Language

Examples & Assembler


ARM Assembly Language
Examples

CS 160

Ward 1

Example 1: C to ARM Assembler


C:

Example 2: C to ARM Assembler


y = a*(b+c);

ARM:

ARM:

CS 160

Ward 2

C:

x = (a + b) - c;
ADR
LDR
ADR
LDR
ADD
ADR
LDR
SUB
ADR
STR

CS 160

r4,a
r0,[r4]
r4,b
r1,[r4]
r3,r0,r1
r4,c
r2,[r4]
r3,r3,r2
r4,x
r3,[r4]

;
;
;
;
;
;
;
;
;
;

get address for a


get value of a
get address for b, reusing r4
get value of b
compute a+b
get address for c
get value of c
complete computation of x
get address for x
store value of x
Ward 3

ADR
LDR
ADR
LDR
ADD
ADR
LDR
MUL
ADR
STR
CS 160

r4,b
r0,[r4]
r4,c
r1,[r4]
r2,r0,r1
r4,a
r0,[r4]
r2,r2,r0
r4,y
r2,[r4]

;
;
;
;
;
;
;
;
;
;

get address for b


get value of b
get address for c
get value of c
compute partial result
get address for a
get value of a
compute final value for y
get address for y
store y
Ward 4

Example 3: C to ARM Assembler

Example 4: Condition Codes

C:

C:

z = (a << 2) |

(b & 15);

if (i == 0)
{
i = i +10;
}

ARM:
ADR
LDR
MOV
ADR
LDR
AND
ORR
ADR
STR

r4,a
r0,[r4]
r0,r0,LSL#2
r4,b
r1,[r4]
r1,r1,#15
r1,r0,r1
r4,z
r1,[r4]

;
;
;
;
;
;
;
;
;

get address for


get value of a
perform shift
get address for
get value of b
perform AND
perform OR
get address for
store value for

ARM:
z
z

CS 160

Ward 5

C:
if (a < b) { x = 5; y = c + d; } else x = c - d;

ARM:

ARM:

CS 160

R0, R0, R0
R0, #15
R1, R1, R1
R0, R0, #1
start

Ward 6

Example 6: if statement [1]

C:
for ( i = 0 ; i < 15 ; i++)
{
j = j + j;
}

SUB
CMP
ADDLT
ADDLT
BLT

R1, R1, #0
R1, R1, #10

CS 160

Example 5: Condition Codes

start

(assume i in R1)

SUBS
ADDEQ

; i -> R0 and i = 0
;
is i < 15?
;
j = j + j
;
i++

Ward 7

; compute and test condition


ADR r4,a
; get address for a
LDR r0,[r4] ; get value of a
ADR r4,b
; get address for b
LDR r1,[r4] ; get value for b
CMP r0,r1
; compare a < b
BGE fblock
; if a >= b, branch to false block

CS 160

Ward 8

Example 6: if statement [2]


; true block
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

;
;
;
;
;
;
;
;
;
;
;

Example 6: if statement [3]


; false block
fblock
ADR r4,c
LDR r0,[r4]
ADR r4,d
LDR r1,[r4]
SUB r0,r0,r1
ADR r4,x
STR r0,[r4]
after
...

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 block

CS 160

Ward 9

Example 6: Heavy Conditional Instruction Use [1]

Same C code; different ARM


implementation
ARM:

ADR
for
LDR
a
ADR

CS 160

r4,a
a
r0,[r4]
r4,b

; get address

; get address

Ward 10

Example 6: Heavy Conditional Instruction Use [2]


r4,x
r0,[r4]
r4,c
r0,[r4]
r4,d
r1,[r4]
r0,r0,r1
r4,y
r0,[r4]

; false block
ADRGE r4,c

; get value of

Ward 11

get address for c


get value of c
get address for d
get value for d
compute a-b
get address for x
store value of x

CS 160

ADRLT
STRLT
ADRLT
LDRLT
ADRLT
LDRLT
ADDLT
ADRLT
STRLT

; Compute and test the


condition

;
;
;
;
;
;
;

CS 160

;
;
;
;
;
;
;
;
;

get address for


store x
get address for
get value of c
get address for
get value of d
compute y
get address for
store y

x
c
d

; get address for c


Ward 12

Example 6: Heavy Conditional Instruction Use [3]


LDRGE
ADRGE
LDRGE
SUBGE
ADRGE
STRGE

r0,[r4]
r4,d
r1,[r4]
r0,r0,r1
r4,x
r0,[r4]

;
;
;
;
;
;

get value of c
get address for d
get value for d
compute a-b
get address for x
store value of x

CS 160

ARM Assembler

Ward 13

CS 160

Assembly Language Basics

CS 160

Ward 14

General Layout

Ward 15

CS 160

Ward 16

Simple Example Description

Assembly Directives

and memory type.

CS 160

Ward 17

CS 160

sum1.s: Compute 1+2++n


AREA
EXPORT
; r0 =
; r0 =

Ward 18

sum2.s: Compute 1+2++n

SUM, CODE, READONLY


sum1
input variable n
output variable sum

AREA
EXPORT
; r0 =
; r0 =

SUM, CODE, READONLY


sum
input variable n
output variable sum

sum1
MOV

r1,#0

; set sum = 0

sum
MLA
MOV

sum_loop
ADD
SUBS
BNE

r1,r1,r0
r0,r0,#1
sum_loop

; set sum = sum+n


; set n = n-1

sum_rtn
MOV
MOV

r0,r1
pc,lr

; set return value

sum_rtn
MOV

r1,r0,r0,r0
r0,r1,LSR#1

; n*(n+1) = n*n + n
; divide by 2

pc,lr

END

END
CS 160

Ward 19

CS 160

Ward 20

log.s: Compute k (n <= 2^k)


AREA
EXPORT
; r0 =
; r0 =
; r1 =

LOG, CODE, READONLY


log
input variable n
output variable m (0 by default)
output variable k (n <= 2^k)

MOV
MOV

r2, #0
r1, #-1

; set m = 0
; set k = -1

r0, #1
r2, r2, #1
r1, r1, #1
r0, r0, LSR #1
log_loop

;
;
;
;
;

CMP
MOVEQ

r2, #1
r0, #1

; test m ==1
; set m = 1 if true

MOV

pc,lr

log

log_loop
TST
ADDNE
ADD
MOVS
BNE

test LSB(n) == 1
set m = m+1 if true
set k = k+1
set n = n>>1
continue if n != 0

log_rtn

CS 160

END

Ward 21

Potrebbero piacerti anche