ARM Technical Support Knowledge Articles

HOW ARE FUNCTION PARAMETERS HANDLED?

Applies to: C51 C Compiler

Answer


Information in this article applies to:


QUESTION

I understand that when a function is called, parameters are passed to registers. In my listing file, I have noticed that some of the parameters are stored in segments with names that look like ?_function?BYTE. What does this actually mean?

ANSWER

The following is an example program with the resulting listing file:

unsigned int bar (unsigned long arg1,unsigned long arg2, unsigned long arg3)
{
   unsigned long abc;

   abc = arg1 + arg2 + arg3;

   return (abc);
}

void main (void)
{
   // This call generates the following assembly listing

   bar(1234L,4567L,7890L);
}


               ; FUNCTION main (BEGIN)

0000 E4                CLR     A
0001 7500D7      R     MOV     ?_bar?BYTE+07H,#0D7H
0004 750011      R     MOV     ?_bar?BYTE+06H,#011H
0007 F500        R     MOV     ?_bar?BYTE+05H,A
0009 F500        R     MOV     ?_bar?BYTE+04H,A

000B 7500D2      R     MOV     ?_bar?BYTE+0BH,#0D2H
000E 75001E      R     MOV     ?_bar?BYTE+0AH,#01EH
0011 F500        R     MOV     ?_bar?BYTE+09H,A
0013 F500        R     MOV     ?_bar?BYTE+08H,A

0015 7FD2              MOV     R7,#0D2H
0017 7E04              MOV     R6,#04H
0019 FD                MOV     R5,A
001A FC                MOV     R4,A
001B 020000      R     LJMP    _bar

               ; FUNCTION main (END)

The function bar() was called with 3 long (or 4-byte) parameters giving a total of 12 bytes. This is why there are 12 MOV instructions in the listing. The C51 can only pass 3 parameters in registers and depending on the size of these parameter types, there may not be enough registers for all 3 parameters. Therefore, the C51 compiler assigns a fixed memory location for the rest of the parameters.

Since the function parameters in this example require a total of 12 bytes, the first 4 bytes (arg1) are stored in registers R4-R7, the next 4 bytes (arg2) are stored in the function's data segment ?_bar?Byte starting from byte# 8 - 11, and the last 4 bytyes (arg3) occupy byte #s 4 - 7. Which argument goes to a specific byte range is determined by the compiler.

MORE INFORMATION

SEE ALSO

Article last edited on: 2005-09-22 11:40:16

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential