3 calling subroutines – ARM VERSION 1.2 User Manual

Page 29

Advertising
background image

Writing ARM and Thumb Assembly Language

ARM DUI 0068B

Copyright © 2000, 2001 ARM Limited. All rights reserved.

2-17

2.3.3

Calling subroutines

To call subroutines, use a branch and link instruction. The syntax is:

BL destination

where

destination

is usually the label on the first instruction of the subroutine.

destination

can also be a program-relative or register-relative expression. Refer to B

and BL on page 4-58 for further information.

The

BL

instruction:

places the return address in the link register (lr)

sets pc to the address of the subroutine.

After the subroutine code is executed you can use a

MOV pc,lr

instruction to return. By

convention, registers r0 to r3 are used to pass parameters to subroutines, and to pass
results back to the callers.

Note

Calls between separately assembled or compiled modules must comply with the
restrictions and conventions defined by the procedure call standard. Refer to the Using
the Procedure Call Standard
in ADS Developer Guide for more information.

Example 2-2 shows a subroutine that adds the values of its two parameters and returns
a result in r0. It is supplied as

subrout.s

in the

examples\asm

subdirectory of ADS. Refer

to Code examples on page 2-2 for instructions on how to assemble, link, and execute the
example.

Example 2-2

AREA subrout, CODE, READONLY
; Name this block of code
ENTRY ; Mark first instruction to execute
start MOV r0, #10 ; Set up parameters
MOV r1, #3
BL doadd ; Call subroutine
stop MOV r0, #0x18 ; angel_SWIreason_ReportException
LDR r1, =0x20026 ; ADP_Stopped_ApplicationExit
SWI 0x123456 ; ARM semihosting SWI

doadd ADD r0, r0, r1 ; Subroutine code
MOV pc, lr ; Return from subroutine
END ; Mark end of file

Advertising