9.4.4. Calling SWIs from an application

The easiest way to call SWIs from your application code is to set up any required register values and call the relevant SWI in assembly language. For example:

	MOV	r0, #65			; load r0 with the value 65
	SWI 	0x0			; Call SWI 0x0 with parameter 
					; value in r0

The SWI instruction can be conditionally executed, as can all ARM instructions.

Calling a SWI from C is more complicated because it is necessary to map a function call onto each SWI with the __swi compiler directive. This allows a SWI to be compiled inline, without additional calling overhead, provided that:


You must use the -fz compiler option when compiling code that contains inline SWIs.

The parameters are passed to the SWI as if the SWI were a real function call. However, if there are between two and four return values, you must tell the compiler that the return values are being returned in a structure, and use the __value_in_regs directive. This is because a struct-valued function is usually treated as if it were a void function whose first argument is the address where the result structure should be placed.

Example 9.9 shows a SWI handler that provides SWI numbers 0x0 and 0x1. SWI 0x0 takes four integer parameters and returns a single result. SWI 0x1 takes a single parameter and returns four results.

Example 9.9. 

struct four
{	int a, b, c, d;
__swi (0x0) int calc_one (int,int,int,int);
__swi (0x1) __value_in_regs struct four calc_four (int);
/* You can call the SWIs in the following manner */
void func (void)
{	struct four result;
	int single, res1, res2, res3, res4;
	single = calc_one (val1, val2, val3, val4);
	result = calc_four (val5);
	res1 = result.a;
	res2 = result.b;
	res3 = result.c;
	res4 = result.d;
Copyright © 1997, 1998 ARM Limited. All rights reserved.ARM DUI 0040D