Annotation of os2sdk/demos/examples/csalias/csalias.c, revision 1.1.1.1

1.1       root        1: /* This example illustrates the use of the following 286DOS function:
                      2:  *     DOSCREATECSALIAS
                      3:  *
                      4:  * A data segment is allocated by calling DOSALLOCSEG. A small assembly 
                      5:  * language routine is created in this data segment. An alias code selector 
                      6:  * for this data segment is obtained by calling DOSCREATECSALIAS. Using the 
                      7:  * alias code selector, the routine in the data segment is called. After 
                      8:  * returning from the routine, the alias code selector and the data segment 
                      9:  * are freed by calling DOSFREESEG.
                     10:  *
                     11:  * This program illustrates the function - it does not do anything useful.
                     12:  * A more likely use of aliasing code to data is where the code is actually
                     13:  * generated at run time, rather than statically declared in this example.
                     14:  *
                     15:  * Copyright (C) Microsoft Corp. 1986
                     16:  */
                     17: 
                     18: #include       <doscalls.h>            /* CP/DOS function declarations      */
                     19: #include       <stdio.h>               /* printf C lib function declaration */
                     20: #include       <dos.h>                 /* FP_OFF and FP_SEG macros          */
                     21: 
                     22: #define        PRIVATE 0               /* segment will not be shared */
                     23: #define        SOMENUM 3               /*to be squared by proc in CSAlias'ed segment*/
                     24: 
                     25: unsigned char square[] = { /*a routine that computes the square of an integer*/
                     26:        0x55,                           /* push bp             */
                     27:        0x8b, 0xec,                     /* mov  bp,sp          */
                     28:        0x8b, 0x46, 0x06,               /* mov  ax,[bp+6]      */
                     29:        0xf7, 0xe0,                     /* mul  ax             */       
                     30:        0x8b, 0xe5,                     /* mov  sp,bp          */
                     31:        0x5d,                           /* pop  bp             */
                     32:        0xcb,                           /* retf                */
                     33:        '\0'                            /* terminate with null */
                     34: };
                     35: 
                     36: main ()
                     37: {
                     38:        unsigned        DataSelector,   /* selector to data segment    */
                     39:                        CodeSelector;   /* alias code segment selector */
                     40:        char            *DataSeg;       /* pointer to data segemnet    */
                     41:        long            (*proc)(int);   /* pointer to a procedure      */
                     42:        long            SquareOfInt;    /* to hold square of an integer*/
                     43: 
                     44:        /* allocate a segment */
                     45:        DOSALLOCSEG (sizeof(square), &DataSelector, PRIVATE);
                     46: 
                     47:        /* construct a far pointer to the data segment */
                     48:        FP_SEG(DataSeg) = DataSelector; 
                     49:        FP_OFF(DataSeg) = 0;
                     50: 
                     51:        /* copy an assembly language procedure into the data segment */
                     52:        strcpy(DataSeg, square); 
                     53:        
                     54:        /* get a csalias selector for the data segment */
                     55:        DOSCREATECSALIAS (DataSelector, &CodeSelector);
                     56:        
                     57:        /* construct the address to the procedure in the csalias'ed segment */
                     58:        FP_SEG(proc) = CodeSelector;
                     59:        FP_OFF(proc) = 0; 
                     60: 
                     61:        /* execute the code in the csalias'ed data segment */
                     62:        if ((SquareOfInt = (*proc)(SOMENUM)) != (SOMENUM * SOMENUM))
                     63:          printf ("*** error: procedure in CSAlias'ed segment failed ***\n");
                     64: 
                     65:        /* free the alias code selector */
                     66:        DOSFREESEG(CodeSelector);
                     67: 
                     68:        /* free the data segment */
                     69:        DOSFREESEG(DataSelector);
                     70: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.