Download Dynamic C User Manual

Transcript
4.23.1.3 Module Body
Every line of code after the EndHeader comment belongs to the body of the module until (1)
end-of-file or (2) the BeginHeader comment of another module. Dynamic C compiles the
entire body of a module if any of the names in the key or header are referenced anywhere in the
application. So keep modules small, don’t put all the functions in a library into one module. If you
look at the Dynamic C libraries you’ll notice that many modules consist of one function. This
saves on code size, because only the functions that are called are actually compiled into the application.
To further minimize waste, define code and data only in the body of a module. It is recommended
that a module header contain only prototypes and extern declarations because they do not generate any code by themselves. That way, the compiler will generate code or allocate data only if
the module is used by the application program.
4.23.2 Module Sample Code
There are many examples of modules in the Lib directory of Dynamic C. The following code will
illustrate proper module syntax and show the scope of directives, functions and variables.
/*** BeginHeader ticks*/
extern unsigned long ticks;
/*** EndHeader */
unsigned long ticks;
/*** BeginHeader Get_Ticks */
unsigned long Get_Ticks();
/*** EndHeader */
unsigned long Get_Ticks(){
...
}
/*** BeginHeader Inc_Ticks */
void Inc_Ticks( int i );
/*** EndHeader */
#asm
Inc_Ticks::
or
a
ipset 1
...
ipres
ret
#endasm
There are 3 modules defined in this code. The first one is responsible for the variable ticks, the
second and third modules define functions Get_Ticks() and Inc_Ticks that access the variable. Although Inc_Ticks is an assembly language routine, it has a function prototype in the
module header, allowing the compiler to check calls to it.
Chapter 4: Language
41