6.4.1. The default method

Unless special conditions apply (as detailed in following sections), C structures are passed in registers that, if necessary, overflow onto the stack and are returned through a pointer to the memory location of the result.

For struct-valued functions, a pointer to the location where the struct result is to be placed is passed in a1 (the first argument register). The first argument is then passed in a2, the second in a3, and so on. It is as if:

struct s f(int x)

were compiled as:

void f(struct s *result, int x)

Example 6.5. 

typedef struct two_ch_struct
	char ch1;
	char ch2;
two_ch max( two_ch a, two_ch b )
	return (a.ch1 > b.ch1) ? a : b;

Example 6.5 is available in the file examples/candasm/two_ch.c, and can be compiled to produce assembly language source using:

armcc -S two_ch.c -li

Example 6.6 shows the code armcc produces (the version of armcc supplied with your release may produce output slightly different from that listed here).

Example 6.6. 

		STMDB		sp!,{a1-a3}
		LDRB		a3,[sp,#4]
		LDRB		a2,[sp,#8]
		CMP		a3,a2
		ADDLE		a2,sp,#8
		ADDGT		a2,sp,#4
		LDR		a2,[a2,#0]
		STR		a2,[a1,#0]
		ADD		sp,sp,#0xc
		MOV 		pc,lr

The STMDB instruction saves the arguments onto the stack. Registers a2 and a3 are used as temporary registers to hold the required part of the structures passed, and a1 is a pointer to an area in memory in which the resulting structure is placed.

Copyright © 1997, 1998 ARM Limited. All rights reserved.ARM DUI 0040D