INF: How to Do "Peeks" and "Pokes" in a C Program

ID Number: Q23868

3.00 4.00 5.00 5.10 6.00 6.00a 6.00ax 7.00

MS-DOS

Summary:

The sample code below contains two functions that simulate what are

commonly known as "peek" and "poke" functions. The peek() function

allows you to look at the contents of any memory location, and the

poke() function allows you to place a value into any memory location.

Sample Code

-----------

/* The following function will stuff a value into any location in

addressable memory. seg:ofs = val.

*/

void poke(unsigned int seg, unsigned int ofs, char val)

{

unsigned char far *ptr;

ptr = (unsigned char far *) (((long)seg<<16)|(long)ofs);

*ptr = val;

}

/* The following function will return the contents of any location in

addressable memory. return(seg:ofs).

*/

unsigned char peek(unsigned int seg, unsigned int ofs)

{

unsigned char far *ptr;

ptr = (unsigned char far *) (((long)seg<<16)|(long)ofs);

return(*ptr);

}

Additional reference words: 5.0 5.00 5.1 5.10 6.0 6.00 6.0a 6.00a

6.0ax 6.00ax 7.0 7.00