Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Watcom C/C++ User's Guide - to finish our discussion, we provide examples that illustrate the use of http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
To finish our discussion, we provide examples that illustrate the use of
variables in the in-line assembly code.  The following example illustrates
the use of static variable references in the auxiliary pragma.

Example:

     #include <stdio.h>

     static short         _rowcol;
     static unsigned char _page;



     extern void BIOSSetCurPos( void );
     #pragma aux BIOSSetCurPos =     \
             "mov  dx,_rowcol"       \
             "mov  bh,_page"         \
             "push bp"               \
             "mov ah,2"              \
             "int 10h"               \
             "pop bp"                \
             modify [ah bx dx];



     void main()
     {
         _rowcol = (5 << 8) | 20;
         _page = 0;
         BIOSSetCurPos();
         printf( "Hello world\n" );
     }

The only rule to follow here is that the auxiliary pragma must be defined
after the variables are defined.  The in-line assembler is passed
information regarding the sizes of variables so they must be defined first.

If we look at a fragment of the disassembled code, we can see the result.


         _rowcol = (5 << 8) | 20;
      0008  c7 06 00 00 14 05                 mov     word ptr
__rowcol,0514H

         _page = 0;
      000e  c6 06 00 00 00                    mov     byte ptr __page,00H

         BIOSSetCurPos();
      0013  8b 16 00 00                       mov     dx,__rowcol
      0017  8a 3e 00 00                       mov     bh,__page
      001b  55                                push    bp
      001c  b4 02                             mov     ah,02H
      001e  cd 10                             int     10H
      0020  5d                                pop     bp

The following example illustrates the use of automatic variable references
in the auxiliary pragma.  Again, the auxiliary pragma must be defined after
the variables are defined so the pragma is placed in-line with the function.

Example:

     #include <stdio.h>

     void main()
     {
         short         _rowcol;
         unsigned char _page;

         extern void BIOSSetCurPos( void );
     #   pragma aux  BIOSSetCurPos = \
             "mov  dx,_rowcol"       \
             "mov  bh,_page"         \
             "push bp"               \
             "mov ah,2"              \
             "int 10h"               \
             "pop bp"                \
             modify [ah bx dx];


         _rowcol = (5 << 8) | 20;
         _page = 0;
         BIOSSetCurPos();
         printf( "Hello world\n" );
     }

If we look at a fragment of the disassembled code, we can see the result.


         _rowcol = (5 << 8) | 20;
      000e  c7 46 fc 14 05                    mov     word ptr -4H[bp],0514H

         _page = 0;
      0013  c6 46 fe 00                       mov     byte ptr -2H[bp],00H

         BIOSSetCurPos();
      0017  8b 96 fc ff                       mov     dx,-4H[bp]
      001b  8a be fe ff                       mov     bh,-2H[bp]
      001f  55                                push    bp
      0020  b4 02                             mov     ah,02H
      0022  cd 10                             int     10H
      0024  5d                                pop     bp

You should try to avoid references to automatic variables as illustrated by
this last example.  Referencing automatic variables in this manner causes
them to be marked as volatile and the optimizer will not be able to do a
good job of optimizing references to these variables.

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson