Keil Logo

C51: Mixing Reentrant Functions and Non-reentrant Functions


Information in this article applies to:

  • C51 Version 5.50a and later

QUESTION

Is it possible to mix reentrant and non-reentrant functions in the same program? Can a non-reentrant function call a reentrant function which then calls another non-reentrant function?

ANSWER

Yes. The compiler and linker keep track of all memory used by the non-reentrant functions and create a call tree that is correct. The overlaid memory will be maintained correctly.

In the following example:

#pragma noregparms

unsigned int f1 (unsigned char arg1, unsigned char arg2)
{
return ((unsigned) arg1 << 8) | arg2;
}


unsigned int r1 (unsigned char arg1, unsigned char arg2) reentrant
{
return f1(arg1, arg2);
}


unsigned int f2 (unsigned char arg1, unsigned char arg2)
{
return r1 (arg1, arg2);
}

void main (void)
{
unsigned int j;

j = f2 (0x12, 0x34);

while (1);
}

The main function calls f2, which calls r1 (which is reentrant), which calls f1 (which is not reentrant). The linker builds the following call tree for this example:

SEGMENT                          DATA_GROUP
  +--> CALLED SEGMENT          START    LENGTH
----------------------------------------------
?C_C51STARTUP                  -----    -----
  +--> ?PR?MAIN?MAIN

?PR?MAIN?MAIN                  0008H    0002H
  +--> ?PR?F2?MAIN

?PR?F2?MAIN                    000AH    0002H
  +--> ?PR?_?R1?MAIN

?PR?_?R1?MAIN                  -----    -----
  +--> ?PR?F1?MAIN

?PR?F1?MAIN                    000CH    0002H

From this call tree, you can see that the memory used by the main function (?PR?MAIN?MAIN) at 0008h is exclusive of the memory used by the f2 function (?PR?F2?MAIN) at 000Ah and the f1 function (?PR?F1?MAIN) at 000Ch.

SEE ALSO


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.