ARM Assembly Language Examples & Assembler Example 1: C to ...

408 downloads 609 Views 321KB Size Report
Examples. Ward 3. CS 160. Example 1: C to ARM Assembler. • C: x = (a + b) - c;. • ARM: ADR r4,a .... Same C code; different ARM implementation. ARM:.
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 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

y

; 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 1 continue if n != 0

log_rtn

CS 160

END

Ward 21