#include<dos.h>
#define FirstFitLow 0
int main(void)
{
unsigned far *buffer_head=0x0040001A;
unsigned far *buffer_tail=0x0040001C;
unsigned far *buffer_start=0x00400080;
unsigned far *buffer_end=0x00400082;
union REGS inregs,outregs;
unsigned original_strategy;
struct MCB{
char sibniture;/*4dh or 5ah*/
unsigned owner;/*owner psp segment sddress*/
unsigned size;/*in 16-byte paragraphs*/
char reserved[3];/*not used*/
char filename[8];/*starting with dos4*/
}far *mcb;
/*try to allocate memory with the BIOS data
segment (0x40 through 0x103f).set the allocation
strategy to find first low.*/
/*save current memory allocation strategy*/
inregs.x.ax=0x5800;
intdos(&inregs,&outregs);
original_strategy=outregs.x.ax;
/*select first fit low as memory strategy*/
inregs.x.ax=0x5801;
inregs.x.bx=FirstFitLow;
intdos(&inregs,&outregs);
/*allocate a 256 byte buffer*/
inregs.h.ah=0x48;
inregs.x.bx=16;/*sixteen-byte paragraphs*/
intdos(&inregs,&outregs);
if(outregs.x.flags&1)
printf("Insufficient memory for 256 byte buffer\n");
else if((outregs.x.ax>=0x40)&&(outregs.x.ax<=0x103F))
{
mcb=(struct MCB far *)((long)(outregs.x.ax)<<16);
/*set the block's owner to 0xffff (a pid that will
never beused)this prevent dos from freeing the memory
when this program ends.*/
mcb->owner=0xFFFF;
/*update the keyboard buffer pointers*/
*buffer_head=(outregs.x.ax-0x40)*16;
*buffer_start=(outregs.x.ax-0x40)*16;
*buffer_tail=(outregs.x.ax-0x40)*16;
*buffer_end=((outregs.x.ax-0x40)*16)+256;
printf("New keyboard segment %x\n",outregs.x.ax);
}
else
printf("No memory for buffer in BIOS data segment\n");
/*reset original strategy*/
inregs.x.ax=0x5801;
inregs.x.bx=original_strategy;
intdos(&inregs,&outregs);
getch();
return 0;
}