11.4 Dynamic functions

  -----------------------

 

      Dynamic functions are functions which are defined but are only inserted

  into the program code if there are calls. They can be used as macros.

 

      The definition of a dynamic function starts with a colon ':',

 

      Example of a stack dynamic function:

 

          : void setvideomode (byte mode)

          {

          AL = mode;

          AH = 0;

          $ INT 0x10

          }

 

      Example of a function dynamic register:

 

          : int ABS () /* AX = number, which absolute value search for*/

          {

          IF (int AX < 0)

               -AX;

          }

 

/* AX = number whose absolute value is sought/

 

 

    11.4.1 Loading a dynamic function at a specific address in memory

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

        Dynamic functions, if they are used as macros and if they were called

    in a program, are inserted into the program code at the very end of

    compilation. It is not known where exactly in the program they will turn

    up. To locate a dynamic function at a specific place in a program, do the

    following:

 

    :void proc ( int par1, par2)

    {

       ...

    }

 

        We have a dynamic function whose code is supposed to be located

    before the code of a regular function in the program. Before defining

    this function, write the following string:

 

    @ void proc ();

 

        This causes a dynamic function to be inserted into the program code

    not at its end, as usual, but at the place where this string is found.

    If the dynamic function has parameters, these must be registered.

 

        The compiler has a still more powerful way to locate all dynamic

    objects (functions, variables, structures) at the desired spot and not at

    the end of the program, as usual. This is the directive '#setdinproc'. On

    encountering this directive, the compiler immediately places all dynamic

    objects currently known to it at the site of declaration of this

    directive. Later dynamic objects are placed as usual at the end of the

    program if '#setdinproc' is not applied again.

 

        This can be be put to good use when creating TSRs and device drivers.