1
Microcontrollers / Re: Memory model for Microcontrollers
« Last post by westfw on Today at 05:34:02 am »Quote
I came across this link https://www.geeksforgeeks.org/memory-layout-of-c-program/amp/ explaining the C memory layout and how variables are stored in memory sections. The example used here involves a PC.
I'm curious to know if this memory model is also applicable to microcontrollers like 8051, PIC, AVR, and ARM. Have you used this c memory model for these microcontrollers?
On the negative side, there is more discussion here on the nature and motivation of the poster than there is on either answering or clarifying the question (on both the OP's side AND the other forum members.)
If either side would just SHUT UP about whether this is some sort of ChatGPT experiment, perhaps there would be enough useful content here for ... OTHER people to learn something (maybe here, maybe through "AIs" - what do you care if your answers get filtered through algorithms before it gets sent to the next person who asks a question? I doubt whether many of you are using your EEVBlog forum posts in the "publications" section of your resume...
So, with that in mind...
- The "memory model" of having text, data, bss, stack, and heap is a C language thing. The exact implementation varies.
- As a "C language thing", the memory model is very much applicable to microconrollers.
- Yes, it's used on PIC, AVR, and ARM, and probably 8051.
- Yes, I've used it on both AVR and ARM (and also embedded x86, 68k, PPC, and MIPS.)
- The referenced web page does NOT really "involve a (modern) PC" when it comes to the details of the locations and growth direction of the various memory areas. With the 64bit address space now commonly available, a PC program probably picks some ridiculously large part of the address space for each piece, and then uses the OS and MMU/Pager to attach pieces of that address space to real memory as needed. eg Code at 0x1000000000000, Data at 0x2000000000000000, Heap at 0x73adf00000000000, Stack at 0x88deadbee0000000. (Note the weird addresses for Heap and Stack. It's a modern feature to randomize those addresses for security reasons. And the program doesn't need Stack or Heap addresses till runtime.) Also, it leaves out the now-common "read only data" section, which is important on PC-like systems for security purposes, and on microcontrollers so that constant data can be placed in flash.
- Exact implementations vary. For example, some compilers use a separate data stack and return stack (I think this is probably required for PIC and 8051, but there are AVR compilers that do it as well.)
- Hardware varies. On many microcontrollers, flash and RAM are not contiguous, or even in the same address space. And I've even used systems where the stack grows upward. A non-readable return stack is pretty common (that's why "two stacks" on a PIC.)
- Optimizations vary. I believe that implementing the data stack on a PIC is sufficiently expensive that it is a compiler option to allocate function-local data (which would normally be on a stack) globally, which is pretty easy as long as you haven't done any recursion.
- Implementation and management details of the Heap are especially variable. Systems I've used have kept careful track of free/malloc calls - which task did them, from which PC, a queue of the last N mallocs and frees, etc (I think it's a law: "in any large system, the complexity of malloc/free will increase until you also need to implement some sort of "lighter weight memory allocator."")
- The usual suspects have posted a bunch of useful information (THANK YOU nctnico, nominalAnimal, brucehoult!) about how a lot of the memory layout is actually under developer control. (but I'd claim that the "model" remains the same.)
If Kittu20 needs additional explanation or details, they need to be more explicit about telling us exactly what parts they don't understand, or what additional information they need, or how they are confused. (that this hasn't been forthcoming is ... one of the problems in this thread.)
Meanwhile, there are signs that OTHER people (including myself) are learning stuff from the technical-content posts in this thread, which is a good thing.
[/list]