A89: Re: Just subscribed... my first question
[Prev][Next][Index][Thread]
A89: Re: Just subscribed... my first question
Your explanation seems to make sense, but I have an example from my code
that contradicts it.
In main():
term *groups[MAX_VARS] = {NULL};
term is a typedef for a struct that contains two unsigned ints and a
pointer. MAX_VARS is 16. The program with the above line compiles to 2421
bytes and requires bzero(), which I assume is similar to the bcopy routine
you mentioned. When I change it to this:
static term *groups[MAX_VARS] = {NULL};
it compiles to 2427 bytes and doesn't require anything from inits.o. So even
without the extra overhead of the inits.o it is still 6 bytes bigger when I
use static.
Just for fun, I changed the MAX_VARS to 1600. Now the size difference is
huge. With static: 8783. Without: 2463. My first guess is that the use of
static in this case causes the whole array to be stored in the file, but the
math doesn't seem to come out right.
So bottom line: I'm confused. And tired. Goodnight.
-Kevin
-----Original Message-----
From: Zeljko Juric <zjuric@utic.net.ba>
To: assembly-89@lists.ticalc.org <assembly-89@lists.ticalc.org>
Date: Tuesday, April 04, 2000 3:33 PM
Subject: A89: Re: Just subscribed... my first question
>
>Hi!
>
>I will elaborate why "static" initialized arrays may make program.
>Look this, for example:
>
>void fnc(void)
>{
> int a[10]={15,12,8,6,14,13,7,9,2,5};
> ...
>}
>
>In above case, array "a" is "auto", so it need to be allocated
>on the stack frame (A6 usually points to the stack frame). But,
>as it is initialized, the initialization need to be done in a
>run time, because auto variables are allocated whenever the
>function "fnc" is called, and destroyed whenever the "fnc"
>finishes. If the array is small, it can be done for example
>by using a construction like
>
>move.w #15,(%a6,-30)
>move.w #12,(%a6,-28)
>move.w #8,(%a6,-26)
>...
>
>etc. If the array is big, the compiler will store initializer
>sequence (15,12,8,... in above example) somewhere in the executable
>file (.89z on TI-89), and will call "bcopy" routine which will
>copy this sequence to the space allocated on the stack. This
>need to be done at the begining on "fnc". As this will be performed
>each time "fnc" is called, the array will always be initilalized
>again, so eventual changes in array data will not survive after
>the function finishes.
>
>Now, look this example:
>
>void fnc(void)
>{
> static int a[10]={15,12,8,6,14,13,7,9,2,5};
> ...
>}
>
>Array "a" is now static, so it will survive after the end of
>function "fnc". Such array will retain it's value across
>function calls, so the initialization need to be performed
>just once. The initialization in fact will be performed just
>at the compile time: the compiler will embed the sequence
>15,12,8,6,14,13,7,9,2,5 in the executable file, and
>declaration like static int a[10]=... will just make that
>"a" points to this allocated area. This is surely shorter
>than initializing the array in run time (e.g. copying to the
>stack frame). Clear?
>
>So, if the array hold constant data which will not be
>modified, static allocation has an advantage...
>
>Note that usage of non-array static variables may even have an
>opposite effect: increasing of program size, because static
>data require additional entries in the program relocation table.
>I don't have time at the moment to elaborate this in more details.
>
>Zeljko Juric
>
>> I read in one of the archives that using static variables in TI-GCC makes
>> the program smaller. Could someone explain this? As far as I know, static
>> has 2 meanings in C:
>>
>> 1. When used to modify a local variable, specifies that the variable is
>to
>> have static duration. This means it is allocated when the program begins
>and
>> deallocated when the program finishes. A variable declared as static in a
>> function will retain it's value across function calls:
>>
>> int inc(void)
>> {
>> static int i = 0; /*initialization occurs on the first call only*/
>> return ++i;
>> }
>>
>> inc will return 1 the first time it is called, 2 the second time it is
>> called, and so on.
>>
>> Variables that are not declared with a specific storage class modifier
>> (static, register, extern) are assumed to be auto. auto declared
>variables
>> are allocated when program execution reaches the block in which they are
>> declared and deallocated when that block finishes.
>>
>> 2. When used to modify global variables, static specifies that the
>variable
>> has file scope and exists only in the file in which it is declared. This
>is
>> not useful in TI-GCC, since mutiple source files is not supported.
>>
>> So basically, I don't understand how static, as opposed to auto could
>affect
>> the file size. Can anyone elaborate?
>>
>> -Kevin
>>
>