Sunday 11 February 2018

ARM Programming 3 - Baby steps in understanding Memory allocations and MAP file in Keil

A simple view of MAP file in Keil 5 with more experiments

The project created in post 2 is used in this post to do some experiments and understand the memory assignment done by the IDE.

The total memory used by our project in each memory area will be listed by the linker map file.
There are few short forms used such as ZI, RW, RO, BSS etc. which we will understand soon but not immediately.

For short,


  1. ZI stands for Zero Initialized data
  2. RW stands for Read Write data
  3. RO stands for RO data
Zero Initialized data can be global variables which are not initialized or initialized with zero values.
Example: 

int array[200];
int array2[100] = {0};
int main()
{
   ...
}
Read Write data can be global variables with non zero initialized values or local variables.

Example:

int array[10] = {2};
int array2[10] = {1,2};
int main ()
{
    int array3[10];
    int array4[10] = {1,2,3,4,4,5,6,7,8,9,0};
    int a,b,c;
    ...
}    

 Read only data will be code, instructions or constant data which will not change during execution. 


Before going through a few experiments, i will show where the map file will be located.
In this example project, the MAP file is located in the folder by name Listings with in the project folder. below image shows the example map file used in this discussion.


1000ARMS
Locating MAP file in Keil
This is going to be very good exercise but will take time for me to complete. hang on.


Build window showing RO data, RW data and ZI data for the example project

Okay. Let us open the .MAP file

and scroll down until we find below section.
1000ARMS
Image Component Size for two source files
Let's have a look at first file of ours, which is project.c.
The compiler complies it and creates one object file called project.o which is present in above image.

Project.o is an object file which has 132 bytes of CODE, with 12 bytes of data, 48 bytes if RO data, 32 bytes of RW Data, 24 bytes of ZI Data and some debug info data too.

Below is the contents of project.C file. let us have a look.
1000ARMS
Project.C file contents
An unsigned long long int takes 8 bytes of space in memory. So, the variable array[3] needs 8 times 3 that is 24 bytes of memory space. Since this is global variable and is zero initialized, it is mapped into ZI data of project.o file.

Each object file will have ZI, RW, CODE, RO, RW sections. Similarly, for second source file project2.o, all sections will be there. The linker finally gathers all object files' info and creates one final file. We will see about Linker later. I do not know much about it still.

Let us do some small changes in the source file project.c so that we understand the MAP file reading better.

let us see present scenario again together with the source file and the generated MAP file, with project.o under our lens.


1000ARMS
Experiment ONE
Now, let me increase the size of zero initialized array (the one highlighted in the above image). Let me double it.


1000ARMS
Experiment TWO
so, in experiment TWO we can see that the ZI data has increased from 24 to 48, to reflect the doubled size of zero initialized array. Rest all components remain unchanged.

 In next thing, i will initialize the global array to non zero value to see what happens. Let us call this experiment THREE.


1000ARMS
Experiment THREE
as I can see and you too can, RW data is now sum of experiment TWO's RW Data (32) and ZI Data(48). Even if one member of the global array is non zero, the whole array is mapped to RW section of the object file.

Let us call experiment FOUR as the little study done by modifying the RO Data.
As seen in experiment FOUR image, RO Data of the project.o object file is 48 bytes big. the 48 comes from 6 initialized variables of the array names array3[100].

I will add one more defined value in the array3 ans observe the MAP file again.


Experiment FOUR
The size of RO Data has changed from 48 to 56 which is an increase of 8 bytes which corresponds to 1 extra value of long long int type in the array[3].

If you have followed me till here, then I think, MAP file idea is sufficient for now to understand which object files has how much data of what sections.

In the coming post, i will try to share about Linker file. How to map these sections into the location we want. Why give control to the system? let us control it. Let us control where which variables go and sit in RAM during execution. at-least for the sake of learning how to map different sections to different regions. That is called scatter file. We will learn about it yaar. chill. Next in 1000ARMS we will see scatter file in more detail.

Please leave comments if there are any mistakes or suggestions.

--

This a post which is a part of mission 1000 ARMs which is to empower the author to be able to write 1000 ARM programs

1 comment:

  1. Thank you so much for the post you do. I like your post and all you share with us is up to date and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job.
    cs作业

    ReplyDelete

ARM Programming 5 - Placing key variable in custom Flash Memory Location

How to Place a variable in custom location of MCU internal Flash Memory? Now, it is getting interesting. We were able to place the RAM v...