Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How XC8 Compiler uses memory ROM and RAM when we declare Variable??

Status
Not open for further replies.

xpress_embedo

Advanced Member level 4
Joined
Jul 5, 2011
Messages
1,154
Helped
161
Reputation
396
Reaction score
189
Trophy points
1,353
Location
India
Activity points
10,591
I am using XC8 Compiler with PIC18F4550, i want to know some basic questions how compiler actually works when we declare a variable.

When i compile the program:-


Code C - [expand]
1
2
3
4
#include<xc.h>
void main()
{
}



My Memory Usage is this.
Memory Usage1.PNG


when i use the following code


Code C - [expand]
1
2
3
4
5
6
#include<xc.h>
unsigned int i=0;
void main()
{
    
}



My Memory Usage is:-
Memory Usage2.PNG

Why 4 Bytes increased in Program ROM and 2 Bytes in RAM.
2Bytes can be increase in RAM but why 4 Bytes in ROM, Please explain me this.

When using the following Code, Storing value in ROM Only.

Code C - [expand]
1
2
3
4
5
6
#include<xc.h>
const unsigned int i=0;
void main()
{
    
}



My memory usage is as Follow:-
Memory Usage3.PNG

Its Okay that RAM Should not have anything, but why ROM goes to 22bytes while without anything it just have 14 bytes.
 

xpress_embedo,

For complete understanding you have to read about the data alignment, padding and packing concepts.

question : first data memory changes , why ?

you have storage classes auto, static, register by default storage class is auto.
In first window, the reason you have no data memory as no variable present.
in second , u have an auto variable 'int' 2 bytes.
in third you mentioned as const variable,const is a type qualifier. The other type qualifier is volatile. The purpose of const is to announce objects that may be placed in read-only memory, and perhaps to increase opportunities for optimization. so you have 0 data memory and increased program memory.

question : 2Bytes can be increase in RAM but why 4 Bytes in ROM, Please explain me this.
in first window : may be due to saving of the paths of the projects and corresponding files .
second and third window it due to data alignment, it depends on the padding bits .
try out with static , volatile. instead of declaring it globally , declare as local variables check then you will not have any data memory but it will have program memory.
declaration of variables need to properly aligned or else you will waste a lot of program memory,where in optimization techniques comes in to picture !
 
The best way to find out is to review the generated code in a compiler list with code disassembly. I think you are long enough working with embedded C compilers to get involved with reading lists and symbol tables.

- - - Updated - - -

There are also implementation dependent points that require a look on the general settings and compiler specifics:
- does it generate variable space and initilization code for unused variables (optimization settings)
- how are constants handled by default
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top