File:  [OS/2 SDKs] / os2sdk / demos / examples / csalias / csalias.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:26:24 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: os2sdk-1988, HEAD
Microsoft OS/2 SDK 03-01-1988

/* This example illustrates the use of the following MS OS/2 function:
 *	DosCreateCSAlias
 *
 * A data segment is allocated by calling DosAllocSeg. A small assembly
 * language routine is created in this data segment. An alias code selector 
 * for this data segment is obtained by calling DosCreateCSAlias. Using the
 * alias code selector, the routine in the data segment is called. After 
 * returning from the routine, the alias code selector and the data segment 
 * are freed by calling DosFreeSeg.
 *
 * This program illustrates the function - it does not do anything useful.
 * A more likely use of aliasing code to data is where the code is actually
 * generated at run time, rather than statically declared in this example.
 *
 * Created by Microsoft Corp. 1986
 */

#define INCL_DOSMEMMGR

#include	<os2def.h>		/* SELECTOROF and OFFSETOF macros    */
#include	<bsedos.h>		/* MS OS/2 function declarations     */
#include	<stdio.h>		/* printf C lib function declaration */


#define	PRIVATE	0		/* segment will not be shared */
#define	SOMENUM	3		/*to be squared by proc in CSAlias'ed segment*/

UCHAR square[] = { /*a routine that computes the square of an integer*/
	0x55,				/* push	bp             */
	0x8b, 0xec,			/* mov	bp,sp	       */
	0x8b, 0x46, 0x06,		/* mov	ax,[bp+6]      */
	0xf7, 0xe0,			/* mul  ax             */	
	0x8b, 0xe5,			/* mov	sp,bp          */
	0x5d,				/* pop	bp             */
	0xcb,				/* retf    	       */
	'\0'				/* terminate with null */
};

main ()
{
	SEL		DataSelector,	/* selector to data segment    */
			CodeSelector;   /* alias code segment selector */
	char 		*DataSeg;	/* pointer to data segemnet    */
	long 		(*proc)(int);	/* pointer to a procedure      */
	long		SquareOfInt;	/* to hold square of an integer*/

	/* allocate a segment */
	DosAllocSeg (sizeof(square), &DataSelector, PRIVATE);

	/* construct a far pointer to the data segment */
	SELECTOROF(DataSeg) = DataSelector;
	OFFSETOF(DataSeg) = 0;

	/* copy an assembly language procedure into the data segment */
	strcpy(DataSeg, square); 
	
	/* get a csalias selector for the data segment */
	DosCreateCSAlias (DataSelector, &CodeSelector);
	
	/* construct the address to the procedure in the csalias'ed segment */
	SELECTOROF(proc) = CodeSelector;
	OFFSETOF(proc) = 0;

	/* execute the code in the csalias'ed data segment */
	if ((SquareOfInt = (*proc)(SOMENUM)) != (SOMENUM * SOMENUM))
	  printf ("*** error: procedure in CSAlias'ed segment failed ***\n");

	/* free the alias code selector */
	DosFreeSeg(CodeSelector);

	/* free the data segment */
	DosFreeSeg(DataSelector);
}

unix.superglobalmegacorp.com

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