Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Insufficient space in data memory?

Status
Not open for further replies.

matt_d82

New Member
I'm trying to compile a program with the SDCC compiler which is to be run on an Intel 8051 chip (AT89S53). The program code is fairly large but there is not much ram type data being stored. The way I understand it, the code goes into the EEPROM (12k bytes on the AT89S53), and global variables and the stack will use the internal ram (256 bytes).

When I try compile my program, I get a linker error saying:
?ASlink-Error-Insufficient space in data memory. 63 bytes short.

If I cut back on global variables (which I've already done as much as I can), the number of bytes short does goes down. But 256 bytes should have been plenty before I did the trimming. I tryed telling SDCC my internal ram size is 1024 bytes (using the --iram-size 1024 option) just to be certain it thinks i have enough internal ram, but it still says it is the same 63 bytes short.

Does anyone know how to get around this? What is using up all the ram?
 
The problem you are having relates to the many memory spaces of the 8051. There are 5 different memory spaces. You have 256 bytes in the "data" space. SFR are in this space so they use up some of this space. Then there is "idata" which is another 256 bytes. "xdata" can be up to 65kB and on your controller will be whatever is left of the 1k of ram that doesn't fit in the first two. There is also "bdata" which 16 bytes in "data" but you can address the bits separately (e.g. change the second bit on the 3rd byte). Finaly "code" is where your program goes.

The reason for all these spaces has to do with the 8051 instruction set. in one instruction you can do an add on locations in data. The same add could take ~ 10 instructions for xdata. So things that need to be fast should go in data.

Your compiler will have a way to tell it where to put the variable. on keil the declaration looks like:

xdata unsigned int n; //n goes in xdata ram.
idata unsigned int x; //x goes into idata ram.

Hope this helps

Brent
 
That is one of the major difficulty to write embedded C.
It is important to understand how and where the variables are stored.
Whenever possible, define the variable with shortest length as possible.
There is no need to use an entire 1 byte for flag, you could just use 1 bit instead.

Good programming skill helps too.
For example,
int A,B,C;
A=1;B=1

C=A+B;
printf("%d", C);

we can actually modify the code to
printf("%d", A+B);
this will save 1 byte of RAM, as the C is just for temporay use and can be obmitted (if you dont need to use it in later of your program)

If some part of the code is to be used frequently, it is always better to create a function for that. This helps to save RAM as well.

Never define variable to store constant value. Define it as constant, and it will be stored in ROM.

However, the giant killer of RAM is actually... Array. For instance, the buffer array. So, examine your code carefully and try to reduce the variable declared.
 
Thanks for the replies guys! The quick fix was to just delcare all my global variables in idata. I had already gone and changed all the "ints" to "chars" that I could. And I do have a couple fair sized buffer arrays, but I cannot think of a way to to get by without them.
 
Another thing to look out for on the 8051 is signed variables; the 8051 doesn't have instructions for dealing with them. If you don't need the variable to be signed your code will run faster is you declare the variable as unsigned.
 
Previously I only worked with PIC, now I am getting into 8051 as required by my job spec. I realise 8051 architectures has pretty big difference with Motorola and Microchip, especially the I/O port and memory.

I have been searching for books in topic of embedded C programming. Dissapointedly, these books usually just give brief introduction to C programming to particular compiler for particular microcontroller only. I havent came across book that teach how to optimize the code, reduce the memory usage.. etc Please recommend if anyone know there is any nice book in this topic. Now I decided to learn assembly language. I dont plan to use them for the entire program, but from there, I wish i could find the way to optimize my C code.
 
A good C compiler should show you the assembly code that your C code generates. But if you know the 8051 assembly instruction (doesn't have to be very in depth just a general idea) you can write C code that comiles more directly into good assembly. For example to take advantage of the branch if equal to zero instruction make your loops count down to zero instead of up to a number. Also use your memory spaces wisely put stuf that needs to be fast into data and slow stuff into xdata. You can move info between these spaces to optimise for a current task. Just think about what the assesmbly will look like when you are coding your C and write code taht looks the closest to the good assembly.

If you're looking for books there's a two volume "C and the 8051" thats fairly good. The first book I haven't seen but it seems like a fairly basic look at C programming for the 8051. The second book has good simple coverage about writing multitasking programs.

Brent
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top