Can any microcontrollers compile code into RAM?

by Chuckz on Fri Feb 08, 2013 9:11 am

I have a friend who has a question about the various microcontrollers. Can someone please answer this for different microcontrollers?

Can any of them compile their own code into RAM, and execute it from RAM? The PICs I'm familiar with definitely cannot. Every little change in the code requires powering down and re-programming the program flash memory, and then powering back up. IOW, every itty bitty change presents a total disruption in what the µC was doing.
Posts: 159
Joined: Mon May 30, 2011 10:54 am

Re: Can any microcontrollers compile code into RAM?

by adafruit_support_rick on Fri Feb 08, 2013 10:45 am

That's a pretty broad question - there are lots of microcontroller architectures out there.

I can start answering it by telling you that most microcontrollers use what is known as a "Harvard architecture", which means that program space and data space are on different internal memory buses. This is a speed optimization which allows instructions and data to be accessed in parallel (i.e., simultaneously). It also means that the processor cannot execute instructions from data space.

Desktop and other general-purpose computers typically use what is known as a "Von Neumann architecture", which has a single address space for both instructions and data.

Von Neumann architectures are much more flexible, and so they are well-suited to general computing where many different programs can be loaded and executed dynamically. But they are also subject to errors caused by bugs which lead to the execution of data as instructions. Viruses exploit this weakness by disguising their code as data and then causing the processor to execute it.

Microcontrollers, on the other hand, are not intended for general computing. They are designed to be embedded in a purpose-built device, and committed to a specific set of tasks. Harvard architectures are well-suited to this, since a non-volatile, read-only program space is highly desirable - programming errors (or viruses) cannot modify or damage the program, power failures cannot erase the program, etc.

There are far too many microcontrollers for me to catalog, but off the top of my head I can tell you that the Atmel AVR family and the Intel 8051 family are both Harvard architecture.
User avatar
Posts: 8456
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Can any microcontrollers compile code into RAM?

by westfw on Fri Feb 08, 2013 12:35 pm

The TI msp430 CPUs are Von Neumann, and can execute code from ram.
unfortunately, they don't usually have much ram.
User avatar
Posts: 1373
Joined: Fri Apr 27, 2007 12:01 pm
Location: SF Bay area

Re: Can any microcontrollers compile code into RAM?

by franklin97355 on Sat Feb 09, 2013 12:54 am

The Parallax Propeller also can compile into RAM and run the code until powered down.
User avatar
Posts: 2662
Joined: Mon Apr 21, 2008 1:33 pm

Re: Can any microcontrollers compile code into RAM?

by thefatmoop on Sun Feb 17, 2013 4:21 pm

Yeah Frank beat me to it. the parallax propeller can have the compiled code tossed directly into ram which is seriously fast and convenient.

but that's about the only plus to the propeller. I'd suggest you avoid it

now if he means compile it's own code that sounds more like an os and you're not going to find that in a micro. raspberry pi is an option
User avatar
Posts: 317
Joined: Tue Aug 19, 2008 3:36 pm