CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Global Variables

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Bradley Looy
Guest







Global Variables
PostPosted: Fri Jun 20, 2003 8:15 am     Reply with quote

<font face="Courier New" size=-1>How come when I add global variables to my code there is both a large increase in used RAM and in ROM? In the example below I comment out the BUFFER variable declaration and ROM use drops 10\%. Does that make sense?

Example:

#include <16F877.H>

#use delay(clock=16000000)
#fuses HS,NOWDT,PUT,BROWNOUT

// GLOBAL VARIABLES
int Mode;
int Buffer[50];

void main() {
int tmp, tmp2;

Mode = INIT;

while (TRUE) {
// code is here
}

}</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515424
R.J.Hamlett
Guest







Re: Global Variables
PostPosted: Fri Jun 20, 2003 8:29 am     Reply with quote

:=How come when I add global variables to my code there is both a large increase RAM and in ROM? In the example below I comment out the BUFFER variable declaration and ROM use drops 10\%. Does that make sense?
:=
:=Example:
:=
:=#include <16F877.H>
:=
:=#use delay(clock=16000000)
:=#fuses HS,NOWDT,PUT,BROWNOUT
:=
:=// GLOBAL VARIABLES
:=int Mode;
:=int Buffer[50];
:=
:=void main() {
:= int tmp, tmp2;
:=
:= Mode = INIT;
:=
:= while (TRUE) {
:= // code is here
:= }
:=
:=}
By default, variables declared as 'global', are zeroed by the compiler. It shouldn't make a 10\% difference, unless the existing code is small, but will add perhaps 40 instructions to clear the block.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515425
Bradley Looy
Guest







Re: Global Variables
PostPosted: Fri Jun 20, 2003 8:36 am     Reply with quote

<font face="Courier New" size=-1>:=By default, variables declared as 'global', are zeroed by the compiler. It shouldn't make a 10\% difference, unless the existing code is small, but will add perhaps 40 instructions to clear the block.
:=
:=Best Wishes


My code is about 8K (almost max on the '877). So 10\% is a significant difference. I think. Any additional thoughts?

Listing Stats before and after:


ROM used: 6728 (82\%)
Largest free fragment is 1183
RAM used: 20 (11\%) at main() level
106 (61\%) worst case
Stack: 6 locations



ROM used: 7844 (96\%)
Largest free fragment is 177
RAM used: 70 (40\%) at main() level
162 (93\%) worst case
Stack: 6 locations</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515426
R.J.Hamlett
Guest







Re: Global Variables
PostPosted: Fri Jun 20, 2003 9:02 am     Reply with quote

:=<font face="Courier New" size=-1>:=By default, variables declared as 'global', are zeroed by the compiler. It shouldn't make a 10\% difference, unless the existing code is small, but will add perhaps 40 instructions to clear the block.
:=:=
:=:=Best Wishes
:=
:=
:=My code is about 8K (almost max on the '877). So 10\% is a significant difference. I think. Any additional thoughts?
:=
:=Listing Stats before and after:
:=
:=
:= ROM used: 6728 (82\%)
:= Largest free fragment is 1183
:= RAM used: 20 (11\%) at main() level
:= 106 (61\%) worst case
:= Stack: 6 locations
:=
:=
:=
:= ROM used: 7844 (96\%)
:= Largest free fragment is 177
:= RAM used: 70 (40\%) at main() level
:= 162 (93\%) worst case
:= Stack: 6 locations</font>
Is all you are adding, the actual buffer, or are you actually 'talking' to it?. The overhead of the zeroing, should be only a few bytes. However remember that if you replace a load of accesses, in (say) half a dozen locations in your code, changing from say i=b, to i=b[n], every single access, will change from a one instruction transfer, to involving setting up the table pointers, and retrieving the indexed variable (probably about ten instructions). This is why it is 'best' to access buffers from one location, using a single routine to do the actual access.
I would suspect though, that your reason, is because of where you declare the buffer. With the 87x, RAM accesses are still 'page based'. Now if you place the buffer as one of the first variables, it will have resulted in perhaps half of your other variables being moved into the second bank, and hence page switching has to take place far more often, increasing the code size. The 'solution', is to place the buffer declaration after the declaration of all commonly used variables, or manually take control, and locate it to perhaps the last memory page.
The same applies in general, as an efficient 'structure' for your variables. There are 96 bytes in the first page, and perhaps twenty of these are used for the scratch area by the compiler. Hence it is well worth declaring the variables that are used most commonly, so that they are in this page, and moving the variables that are rarely used to the 'tail' of the declarations, to keep page switching to a minimum.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515427
Bradley Looy
Guest







Re: Global Variables
PostPosted: Fri Jun 20, 2003 9:39 am     Reply with quote

:=I would suspect though, that your reason, is because of where you declare the buffer. With the 87x, RAM accesses are still 'page based'. Now if you place the buffer as one of the first variables, it will have resulted in perhaps half of your other variables being moved into the second bank,

****

AHA! You are exactly right. I didn't think of that. Thanks, I will locate the buffer manually. The good thing, is that it (the global buffer) is a temporary thing. I wouldn't normally do this on a permanant basis. Thanks Again.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515429
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group