[comp.lang.c] Microsoft "C" How to Peek and Poke?

ammons@cod.NOSC.MIL (Tempest P. Ammons) (07/09/88)

Question:

   How can a Microsoft "C" programmer read and write the contents of
   a particular memory location in an IBM-PC compatble (ie. Zenith Z-248)?
   That is, how can you peek and poke in Microsoft C?

                                Thank You 
                                  Pat


Pat Ammons
Ammons@cod.nosc.mil
(619) 553-3544

gwyn@brl-smoke.ARPA (Doug Gwyn ) (07/09/88)

In article <1148@cod.NOSC.MIL> ammons@cod.NOSC.MIL (Tempest P. Ammons) writes:
>   How can a Microsoft "C" programmer read and write the contents of
>   a particular memory location in an IBM-PC compatble (ie. Zenith Z-248)?
>   That is, how can you peek and poke in Microsoft C?

If you really must stoop to such a disgusting habit, the following
approach should work on any reasonable C implementation on any system
that does not use memory mapping.  (For 80x86 implementations, you may
need to resort to kludgery such as using a "far" keyword; I don't
program those critters so I'm not 100% familiar with all the crocks
that typical 80x86 implementations seem to force on you.)

	#define	PEEK_8( byte_loc )	(*(char *)(byte_loc))
	#define	PEEK_16( word_loc )	(*(short *)(word_loc))
	#define	POKE_8( loc, byte )	((void)(*(char *)(loc) = byte))
	#define	POKE_16( loc, word )	((void)(*(short *)(loc) = word))

Usage:
	if ( PEEK_8( 0xFF00 ) != 0x80 )
		POKE_16( 0xF080, 0x1000 );

dave@westmark.UUCP (Dave Levenson) (07/11/88)

In article <1148@cod.NOSC.MIL>, ammons@cod.UUCP writes:
> Question:
...
>    How can a Microsoft "C" programmer read and write the contents of
>    a particular memory location in an IBM-PC compatble (ie. Zenith Z-248)?
>    That is, how can you peek and poke in Microsoft C?


While you could write peek() and poke() functions in MS-C, you can
more efficiently de-reference a far pointer to the absolute memory
location inline.

For example:  Suppose we want to update absolute location B800:0000
(which is the upper left character position on the screen, with the
video adaptor I'm using) and put a character there.  We can write:

void showit(c)
char c;
{
	char far *video;
	FP_SEG(video) = 0xb800;		/* initialize the pointer */
	FP_OFF(video) = 0;
	
	*video = c;			/* update the location */

	return;
}

The above function uses a Microsoft extension of C (it's available
on some other Intel-based compilers, as well): the far pointer. 
This is a pointer consisting of a segment and offset, that is a
"natural object" for the Intel architecture.

-- 
Dave Levenson
Westmark, Inc.		The Man in the Mooney
Warren, NJ USA
{rutgers | att}!westmark!dave