Re: [PIC] Learning C

"Forrest Christian (List Account)" <[email protected]>
Newsgroups gmane.comp.hardware.microcontrollers.pic
Message-ID <CAKsZx=3iXkNpyJHLC+ynNt+qh1Q0zzdDnVMdNheREW2_yfpffA@mail.gmail.com>
The C standard doesn't mention where automatic duration variables (like x
in your example) will be stored.   It leaves that 100% up to the compiler.
 It could be on the stack, or it could be some static allocation, or it
could be a register, or somewhere else.

A good example is XC8, and other compilers for the 8 bit PICs.  It does
what it calls static stack allocation.  How it works is somewhat easy to
explain:

Every auto variable that won't fit in a register is assigned a specific
memory address which never changes for a particular build of the code.
Because XC8 is aware of the call tree, it is able to assign these variables
in a way that variables that are never "active" at the same time can share
the same memory space.   For example, all library functions that use a
temporary variable and then return can use the exact same space for those
variables.   On the other hand, an auto variable which exists in main()
will need to be assigned a dedicated space which is not reused.

This also simplifies the output code.  Instead of having to deal with the
stack pointer, the functions can then just go after the memory directly.
This does cause problems for functions which are called by interrupt
routines and as such, the compiler will duplicate functions which are used
by the main program and interrupts, compiling each to their own set of
variables.  XC8 or one of the other compilers I've used will emit a warning
when it does that.  I'll usually use that as an excuse to look at the
called function and determine if it can be eliminated from the interrupt in
some way that improves the code.

One other note is that "statically compiled stacks" like this don't support
recursion as it's impossible to tell the call depth.  As a result, either
the compiler will fail the compile with an error, or in some cases the
compiler will do a dynamic stack just for the recursive functions.
 Looking at the XC8 compiler docs it looks like on larger PICs with better
support for a stack it supports this mixed methodology.

Note that this whole issue is one of the reasons why the AVR crowd used to
point at their core and said it was better for C, since it has better stack
support and also has other features which are more useful for C such as (if
I recall correctly) more data registers to store temporary values in.

One final note:   Any C complier which compiles for hardware with a very
restricted or non-existent stack is going to have to support some sort of
method for having auto variables not being on the stack.  Even if that
support is to tell the programmer that they need to fix this (or worse,
produce a broken executable).

On Mon, Nov 18, 2024, 4:01 AM smplx <[email protected]> wrote:

> Hi Forrest,
> I am intrigued by what you say. Could you please point me at any specific
> embedded C compilers that do this.
>
> Friendly Regards
> Sergio Masci
>
> On Sun, 17 Nov 2024, Forrest Christian (List Account) wrote:
>
> > With many embedded compilers, x may or may not be on the stack, as the
> > compiler will, in some cases, use non-stack memory for local variables.
> > This is especially true on smaller microcontrollers with limited stack
> > space.
> >
> > One would need to read the compiler manual to determine if x will always
> be
> > on the stack,  whether there is a way to force x to be on the stack,  or
> if
> > there is a compiler specific way to get the stack pointer.
> >
> > On Sat, Nov 16, 2024, 4:09 AM smplx <[email protected]> wrote:
> >
> >> write a function:
> >>
> >> void * get_stack_ptr(void)
> >> {
> >>         int x;
> >>
> >>         return &x;
> >> }
> >>
> >> This will return the address of the local variable x which will be on
> the
> >> stack.
> >>
> >> use it as:
> >>
> >>         if ((unsigned long)get_stack_ptr() == (unsigned
> long)fixed_value+N)
> >>
> >> // where N is a constant TBD (around +/- 4 to 12)
> >>
> >>
> >> On Fri, 15 Nov 2024, David VanHorn wrote:
> >>
> >> > Same here. I can work with c fairly well, but some things seem
> unusually
> >> > difficult in c.  Like comparing the current value of the stack pointer
> >> to a
> >> > fixed value ( used in my sanity check routine)  I assume there is a
> way
> >> to
> >> > get there.
> >> >
> >> > On Sun, Nov 10, 2024, 11:34 AM David C Brown <[email protected]>
> wrote:
> >> >
> >> >> Although I enjoy programming in Assembler, the fact that so many
> >> libraries
> >> >> abd code examples are in C I think that the time has come for me to
> >> learn
> >> >> that language.
> >> >>
> >> >> Any advice on how to do that would be welcomed, be it a book or
> online
> >> >> resources.
> >> >> I am a competent programmer having programmed in  with Forth,
> Fortran.
> >> >> \Pascal, Visual basic
> >> >> __________________________________________
> >> >> David C Brown
> >> >> 43 Bings Road
> >> >> Whaley Bridge
> >> >> High Peak                           Phone: 01663 733236
> >> >> Derbyshire                eMail: [email protected]
> >> >> SK23 7ND          web: www.bings-knowle.co.uk/dcb
> >> >> <http://www.jb.man.ac.uk/~dcb>
> >> >>
> >> >>
> >> >>
> >> >> *Sent from my etch-a-sketch*
> >> >> --
> >> >> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> >> >> View/change your membership options at
> >> >> https://mailman.mit.edu/mailman/listinfo/piclist
> >> >>
> >> > --
> >> > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> >> > View/change your membership options at
> >> > https://mailman.mit.edu/mailman/listinfo/piclist
> >> --
> >> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> >> View/change your membership options at
> >> https://mailman.mit.edu/mailman/listinfo/piclist
> >>
> > --
> > http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> > View/change your membership options at
> > https://mailman.mit.edu/mailman/listinfo/piclist
> --
> http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
> View/change your membership options at
> https://mailman.mit.edu/mailman/listinfo/piclist
>
-- 
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
https://mailman.mit.edu/mailman/listinfo/piclist
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.