You are on page 1of 6

ARM Assembly Language Examples & Assembler

ARM Assembly Language Examples

CS 160

Ward 1

CS 160

Ward 2

Example 1: C to ARM Assembler


C:
x = (a + b) - c;

Example 2: C to ARM Assembler


C:
y = a*(b+c);

ARM:
ADR LDR ADR LDR ADD ADR LDR SUB ADR STR
CS 160

ARM:
; ; ; ; ; ; ; ; ; ; 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

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

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


C:
z = (a << 2) | (b & 15); ; ; ; ; ; ; ; ; ; 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 a C:

Example 4: Condition Codes


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]

ARM:
z z

(assume i in R1)

SUBS ADDEQ

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

CS 160

Ward 5

CS 160

Ward 6

Example 5: Condition Codes


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

Example 6: if statement [1]


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

ARM:
; 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

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

CS 160

Ward 7

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

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 ... ; ; ; ; ; ; ; 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

Ward 9

CS 160

Ward 10

Example 6: Heavy Conditional Instruction Use [1]

Example 6: Heavy Conditional Instruction Use [2]


ADRLT STRLT ADRLT LDRLT ADRLT LDRLT ADDLT ADRLT STRLT r4,x r0,[r4] r4,c r0,[r4] r4,d r1,[r4] r0,r0,r1 r4,y r0,[r4] ; ; ; ; ; ; ; ; ; 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

Same C code; different ARM implementation ARM:


; Compute and test the condition ADR for LDR a ADR r4,a a r0,[r4] r4,b ; get address ; get value of ; get address

; false block ADRGE r4,c


Ward 11 CS 160

; get address for c


Ward 12

CS 160

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

ARM Assembler

CS 160

Ward 13

CS 160

Ward 14

Assembly Language Basics

General Layout

CS 160

Ward 15

CS 160

Ward 16

Simple Example Description

Assembly Directives

and memory type.

CS 160

Ward 17

CS 160

Ward 18

sum1.s: Compute 1+2++n


AREA EXPORT ; r0 = ; r0 = sum1 MOV sum_loop ADD SUBS BNE sum_rtn MOV MOV END
CS 160 Ward 19 CS 160

sum2.s: Compute 1+2++n


AREA EXPORT ; r0 = ; r0 = sum MLA MOV r1,r0,r0,r0 r0,r1,LSR#1 ; n*(n+1) = n*n + n ; divide by 2 SUM, CODE, READONLY sum input variable n output variable sum

SUM, CODE, READONLY sum1 input variable n output variable sum

r1,#0

; set sum = 0

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

; set sum = sum+n ; set n = n-1

sum_rtn MOV END

pc,lr

r0,r1 pc,lr

; set return value

Ward 20

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


AREA EXPORT ; r0 = ; r0 = ; r1 = log MOV MOV log_loop TST ADDNE ADD MOVS BNE CMP MOVEQ log_rtn MOV
CS 160

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

r2, #0 r1, #-1

; set m = 0 ; set k = -1

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

; ; ; ; ;

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

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

pc,lr
Ward 21

END

You might also like