The Wayback Machine - https://web.archive.org/web/20240921171458/https://www.geeksforgeeks.org/execution-from-ram-in-embedded-systems/
Open In App

Execution from RAM in Embedded systems

Last Updated : 11 Aug, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Prerequisite : Random Access Memory (RAM), Introduction of Embedded Systems

Introduction :
Unlike application programs in computers, software in embedded systems do not execute from RAM. In vast majority of modern embedded system architectures, programs (instructions) are stored in microcontroller’s flash memory (code flash/ program flash) and directly executes in place. Earlier, ROM were used for program storage and execution. 
Due to emerging technologies in memory devices, nowadays flash memory is being used. RAM is a volatile memory and contains nothing upon reset or power cycle. It is used to store data and hold the value of variables at run time. One can safely develop an embedded product by storing and executing the program in code flash (using downloading tool and debugger). MCU’s have dedicated portions of memory space for code flash and RAM. Memory maps in advanced microcontrollers contain Data flash, DSPR, PSPR, Boot ROM, EEPROM, debug memory etc. To make it simple, we will consider code flash to store and execute the programs and RAM to store the variables and data. There are certain situations where it becomes mandatory to run subroutines from RAM. 
This article covers the need to copy a code section to RAM, various ways to implement it, its advantages and disadvantages. Copying to RAM means hosting two copies of code– one in flash and other in RAM.

Fig.1. Microcontroller’s Memory map

Need of Execution from RAM :

  1. During reprogramming of flash memory (Bootloader functionality), certain functions to be executed from RAM like erase program flash, write data to program flash. The code shall not run from the same flash memory that is being erased/ programmed. In some chips, flash memory freezes while performing the operations on flash. Hence the code section is required to be placed in RAM and execute.
  2. During reprogramming of flash memory (Bootloader functionality), Interrupt vector addresses and ISR’s to be placed and executed from RAM.
  3. Bootloaders executing from RAM. The safest way for bootloader functionality is to receive the bootloader file from host device and place the complete code in RAM.
  4. Running programs from RAM is much faster than running from flash e.g. placing frequently executing code in RAM for time optimization.
  5. Some schedulers in embedded systems create task in RAM which points to program in flash.
  6. Run the application from RAM while flash operation is in progress.

Copy code section and Execute from RAM :

1. Some controllers provide quite a straight forward way to copy a code to RAM. All you need is, go through toolchain and user manuals. Different ways are listed below –

  • Some controllers provide an attribute or an API or compiler directives to copy code to RAM.
  • In some controllers, start-up routine runtime code (which itself executes from flash) copies program opcodes from flash to RAM as per need as it does for initialized variables.
  • In some controllers, update of linker script would be enough to place the code section in RAM. But there must be some source code which copies code section to RAM. In most of the cases, internally done by startup routine.

2. Copy function to RAM –

  • Below snippet copies the func_to_be_copied() function to RAM. dummy_endfunc() is located at the end of function to determine the size of the func_to_be_copied(). Ensure that the dummy_endfunc() is not optimized by compiler. Compiler directives like “#pragma” can be used to avoid optimization. ExecuteRamFunc() can be called to execute func_to_be_copied() from RAM.
     

C




// Global buffer stored in RAM and function is copied to this buffer 
unsigned char RAM_area[1024] 
void func_to_be_copied(void)
{
//piece of code
}
// No other function to be placed in between
// Make compiler settings for not to optimize dummy_endfunc
void dummy_endfunc(void)
{
    // No code
}
  
void CopyFlashFuncToRam(void)
{
   unsigned long int func_size = 0, cntr;
   unsigned char *dest_ptr;
   dest_ptr = (unsigned char *) func_to_be_copied ;  
   func_size = (unsigned long int)dummy_endfunc - (unsigned long int)func_to_be_copied;
   for(cntr = 0; cntr < func_size; cntr++)
   {
      RAM_area[cntr] = dest_ptr[cntr]; 
   }   
}
  
void ExecuteRamFunc(void)
{
   void(*func_ptr)(void) = (void(*)(void)) &RAM;_area[0];
   func_ptr();  
}


3. Size of the function can also be calculated by the use of return statement that can be placed at the end of func_to_be_copied(). Start copying the function till you encounter the opcode of return statement. There exist a risk that the data used inside function can be same as the return statement’s opcode. For this, you need to understand the alignment requirements of the instructions by referring the user manual.

4. Copy code section to RAM – 
Consider you have a piece of code placed in flash memory say a bootloader code which needs to be executed from RAM. The size of bootloader should be well known in advance. A signature byte can also be written at the end of bootloader code while downloading to flash to know the code size. If the size of bootloader is not known then the size of section allocated can be considered. The section of code can be copied to RAM in same way as explained in point no. (2). Make sure that the RAM is reserved in the linker script to be used for instruction execution.

While executing from RAM, consider the following –

  • The RAM used for instruction execution shall be reserved either through linker script or by declaring global buffer.
  • The start address of copied section shall be known so that the Program Counter can be pointed correctly or execute by pointing function pointer to the correct address.
  • RAM test can be performed at boot time to ensure its correctness.

Advantages :

  1. Performance – 
    Time optimization if the frequently executed critical routines are placed in RAM. RAM execution is significantly faster than flash.
  2. Decrease in power consumption –
    If the whole application is executing from RAM, flash can be powered off.
  3. Short addressing is usually required if code is executing from RAM i.e. accessing the controller’s RAM, data flash or EEPROM can be accessed by short pointers whereas if the code is executing from flash (flash are typically large memories, 100’s of KB to MB’s), it would need long addressing. In short addressing, assembler usually generates the instructions like sjmp, acall etc. which typically takes lesser number of machine cycles.

Disadvantages :

  1. RAM is a scarce resource, expensive and is usually of much smaller size compared to flash. If the non-static code is placed in RAM, it is the wastage of RAM space, thereby limiting the usage of variable data.
  2. Hosting of the same routine at two places- one in flash and other in RAM.
  3. Debugging code in RAM is hard. Debugging can be done in disassembly. ELF files are generally used for debugging which contains debug information, symbol tables etc. If the code is moved somewhere else, you cannot see the symbols in debugger, but the instructions executing in disassembly window can be viewed and debugged.
  4. Sometimes executing from RAM results in unexpected hard fault. Hence, it is recommended to first execute from flash, test the functionality and then re-execute from RAM.


Similar Reads

Classification of Embedded Systems
Embedded Systems are classified based on the two factors i.e. Performance and Functional Requirements Performance of Micro-controllers Based on Performance and Functional Requirements it is divided into 4 types as follows : Real-Time Embedded Systems : A Real-Time Embedded System is strictly time specific which means these embedded systems provides
3 min read
Introduction of Embedded Systems | Set-1
Before going to the overview of Embedded Systems, Let's first know the two basic things i.e., embedded and system, and what actually do they mean. System is a set of interrelated parts/components which are designed/developed to perform common tasks or to do some specific work for which it has been created. Embedded means including something with an
6 min read
Sorting larger file with smaller RAM
Suppose we have to sort a 1GB file of random integers and the available ram size is 200 Mb, how will it be done? The easiest way to do this is to use external sorting. We divide our source file into temporary files of size equal to the size of the RAM and first sort these files. Assume 1GB = 1024MB, so we follow following steps. Divide the source f
2 min read
Difference between Random Access Memory (RAM) and Content Addressable Memory (CAM)
Random Access Memory (RAM) is used to read and write. It is the part of primary memory and used in order to store running applications (programs) and program's data for performing operation. It is mainly of two types: Dynamic RAM (or DRAM) and Static RAM (or SRAM). RAM is made up of small memory cells that are arranged in a grid pattern. Each cell
4 min read
Magnetic Random Access Memory (M-RAM)
MRAM stands for magnetoresistive random access memory and is a non-volatile type of RAM. Magnetic state refers to the electrical resistance of a metal when it is placed in a magnetic field. The MRAM uses magnetic states and magnetisation direction in a ferromagnetic material(a material which is highly susceptible to magnetization to store data bits
4 min read
RAM Full Form
RAM stands for Random Access Memory. RAM is used to read and write into memory. RAM stores files and data of programs that are currently being executed by CPU. It is a volatile memory as data loses when power is turned off. RAM can be further divided into two classifications: Static RAM (SRAM), and Dynamic RAM (DRAM). History of RAM : In the year 1
2 min read
Similarities between RAM and ROM
In this article, we will discuss the overview of RAM and ROM, and then we will cover different types of each and then mainly focus will be on similarities between RAM and ROM. Let's discuss it one by one. Random-access memory(RAM) :Random-access memory is a type of memory namely volatile memory that is used to store programs and data being used by
3 min read
Difference between RAM and Cache
Memory is important in modern computing systems because it allows for efficient and quick data processing. Random Access Memory (RAM) and cache memory are two common types of memory used in computers. Both serve separate purposes and are required for a computer's maximum performance. RAM is the primary memory that stores data and programs currently
5 min read
Random Access Memory (RAM) and Read Only Memory (ROM)
Memory is a fundamental component of computing systems, essential for performing various tasks efficiently. It plays a crucial role in how computers operate, influencing speed, performance, and data management. In the realm of computer memory, two primary types stand out: Random Access Memory (RAM) and Read-Only Memory (ROM). Understanding the diff
8 min read
Different Types of RAM (Random Access Memory )
In the computer world, memory plays an important component in determining the performance and efficiency of a system. In between various types of memory, Random Access Memory (RAM) stands out as a necessary component that enables computers to process and store data temporarily. In this article, we will explore the world of RAM, exploring its defini
8 min read
Difference between Random Access Memory (RAM) and Hard Disk Drive (HDD)
A person novice to computers often is in confusion between Random Access Memory (RAM) and Hard Disk Drive (HDD). The data on which CPU needs to work out is stored temporarily in RAM. The disadvantage of RAM is, it's volatile and data is lost once the computer is turned off or shut down at failure. On the other hand, HDD is not volatile and used to
6 min read
Difference between RAM and SRAM
RAM is the temporary memory used in computers to store data and run programs. There are two main types of RAM - SRAM and DRAM. SRAM and DRAM are designed and work differently, which makes them better suited for different uses. SRAM is faster and uses less power, so it's often used in processors as cache memory. DRAM is slower but can store more dat
6 min read
MongoDB - Embedded Documents
MongoDB provides you a cool feature which is known as Embedded or Nested Document. Embedded document or nested documents are those types of documents which contain a document inside another document. Or in other words, when a collection has a document, this document contains another document, another document contains another sub-document, and so o
2 min read
Architecture of an Embedded System | Set-3
Typical embedded system mainly has two parts i.e., embedded hardware and embedded software. Embedded hardwares are based around microprocessors and microcontrollers, also include memory, bus, Input/Output, Controller, where as embedded software includes embedded operating systems, different applications and device drivers. Basically these two types
2 min read
Difference Between Computer and Embedded System
Computers and embedded systems play a central role in modern technology, providing the digital infrastructure that powers our interconnected world. From personal computers to smart devices and industrial products, these systems form the backbone of our daily lives, enabling communication, computation, and control across a range of applications. Her
8 min read
Program execution transfer instructions in 8086 microprocessor
Prerequisite - Branching instructions in 8085 microprocessor Program execution transfer instructions are similar to branching instructions and refer to the act of switching execution to a different instruction sequence as a result of executing a branch instruction. The two types of program execution transfer instructions are: Unconditional Conditio
3 min read
Program Execution in the CPU
You may be speculative however the central processor is programmed. It contains a special register — the instruction register — whose bit pattern determines what the central processor unit can do. Once that action has been completed, the bit pattern within the instruction register may be modified, and also the central processor unit can perform the
2 min read
Essential Registers for Instruction Execution
These are various registers required for the execution of instruction: Program Counter (PC), Instruction Register (IR), Memory Buffer (or Data) Register (MBR or MDR), and Memory Address Register (MAR). These are explained as follows below. Program Counter (PC) : It contains the address of an instruction to be executed next. The PC is updated by the
3 min read
Instruction execution and straight line sequencing in CO
Introduction :As instructions are a part of the program which are stored inside the memory, so every time the processor requires to execute an instruction, for that the processor first fetches the instruction from the memory, then decodes the instruction and then executes the instruction. The whole process is known as an instruction cycle. Instruct
4 min read
Computer Organization and Architecture | Pipelining | Set 1 (Execution, Stages and Throughput)
Pipelining is a technique used in modern processors to improve performance by executing multiple instructions simultaneously. It breaks down the execution of instructions into several stages, where each stage completes a part of the instruction. These stages can overlap, allowing the processor to work on different instructions at various stages of
9 min read
Real Time Systems
A real-time system means that the system is subjected to real-time, i.e., the response should be guaranteed within a specified timing constraint or the system should meet the specified deadline. For example flight control systems, real-time monitors, etc. Types of real-time systems based on timing constraints:Hard real-time system: This type of sys
4 min read
Types of OLAP Systems in DBMS
OLAP is considered (Online Analytical Processing) which is a type of software that helps in analyzing information from multiple databases at a particular time. OLAP is simply a multidimensional data model and also applies querying to it. Types of OLAP ServersRelational OLAPMulti-Dimensional OLAPHybrid OLAPTransparent OLAPRelational OLAP (ROLAP): St
6 min read
Need and Functions of Operating Systems
The fundamental goal of an Operating System is to execute user programs and to make tasks easier. Various application programs along with hardware systems are used to perform this work. Operating System is software that manages and controls the entire set of resources and effectively utilizes every part of a computer. The figure shows how OS acts a
9 min read
Tasks in Real Time systems
The system is subjected to real-time, i.e. response should be guaranteed within a specified timing constraint or system should meet the specified deadline. For example flight control systems, real-time monitors, etc. There are four types of tasks in real-time systems: Periodic tasksDynamic tasksCritical taskNon-critical taskPeriodic Tasks: In perio
7 min read
Tek Systems Interview Experience
There was an on-campus drive of Tek system in our college. Recruitment Process: (1)Quantitative + Technical Round:- In this round there were five sections in a total duration of 90 minutes: Around 10 aptitude questions, 5 picture representation puzzles, 5 verbal, 5 technical and 1 coding question. Aptitude questions were of medium difficulty, Pictu
4 min read
Special Purpose Computer Systems
There are various classes of computer systems based on their computational speed, usage, and hardware. The following are some special-purpose systems according to specific applications. They use: Real-time embedded systems Multimedia systemsMultimedia systemsHandheld and portable systems These are explained as following below. Real-time embedded sy
3 min read
Optical Storage Systems
THe Optical memory is an electronic storage medium that uses a laser beam to store and retrieve digital (binary) data. In optical storage technology, a laser beam encodes digital data on an optical disc or laser disc in the form of tiny pits arranged in a spiral pattern on the surface of the disc. Digital Versatile Disk (DVD), Compact Disk (CD) and
6 min read
Storage Structure in Operating Systems
Basically we want the programs and data to reside in main memory permanently. This arrangement is usually not possible for the following two reasons: Main memory is usually too small to store all needed programs and data permanently. Main memory is a volatile storage device that loses its contents when power is turned off or otherwise lost. There a
2 min read
NICE Systems Ltd. Interview Experience | Set 4 (On-Campus)
Company Profile: It is an analytics product company. Global talent Center (GTC) only in Pune in India. works in customer centre analytics (you can call it as customer care call recording and analytics), fraud detection, totally works in agile, Israel based company. Round 1: (150+ students have appeared for this round.) APTITUDE TEST CONTAINS TOTAL
3 min read
Ittiam Systems Interview Experience | Set 2 (On-Campus)
Round 1- Ittiam systems conducted an online coding contest on Hackerearth. Top 30 students were called for further interviews. Round 2- This was a technical round held over Skype. They started off with some puzzles followed by some technical questions. Question 1 (Puzzle) - A and B are playing a game with a biased coin having probability of getting
2 min read
Practice Tags :