avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-chat] re-entrant functions


From: Dave Hylands
Subject: Re: [avr-chat] re-entrant functions
Date: Tue, 31 Jan 2006 14:47:38 -0800

Umm. yes.

>  Are re-entrant functions supported in winavr? If so, how does one go about
> writing one? Is there some reentrant keyword that will cause the compiler to
> use the stack for passing parameters instead of fixed registers?

The secret to writing reentrant functions is not to use global data
(this is a sweeping generalization - obviously there are ways to write
reentrant functions that use global data, but that's a good place to
start).

It has nothing to do with calling conventions. The compiler will
ensure that the registers are saved and restored properly. You're
programming in C, don't think about how the assembly code is working.

Now there are also many different kinds of reentrancy, so you probably
need to give us a bit more of an idea of what you're trying to do.

Here's an example of a non-reentrant function (with minimal error
checking etc to keep the example simple).

char buf[ 10 ];

char *itoa( unsigned x )
{
    char *s = &buf[ 9 ];
    *s = 0;

    while ( x != 0 )
    {
        s--;
        *s = ( x % 10 ) + '0';
    }
    return s;
}

Here's an example of a reentrant one:

char *itoa( unsigned x, char *buf, int bufLen )
{
    char *s = &buf[ bufLen-- ];
    *s = 0;

    while ( x != 0 )
    {
        s--;
        *s = ( x % 10 ) + '0';
    }
    return s;
}

Pretty minor differences. Neither one cares about register calling conventions.

--
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]