5.3.3. Calling Subroutines

To call subroutines in assembly language, use a Branch Link instruction. The syntax is:

		BL	label

where label is usually the label on the first instruction of the subroutine. (It could alternatively be a program-relative or register-relative expression, see Register-relative and program-relative expressions of the ARM Software Development Toolkit Reference Guide.)

The BL instruction:

After the subroutine code is executed you can use a MOV pc,lr instruction to return. By convention, registers r0-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 ARM and Thumb Procedure Call Standards. Refer to Chapter 6 Using the Procedure Call Standards for more information.

Example 5.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 the toolkit. Refer to Code examples for instructions on how to assemble, link, and execute the example.

Example 5.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				; Angel semihosting ARM SWI.
doadd		ADD		r0, r0, r1				; Subroutine code.
		MOV		pc, lr				; Return from subroutine.
		END						; Mark end of file
Copyright © 1997, 1998 ARM Limited. All rights reserved.ARM DUI 0040D
Non-Confidential