[comp.sys.ibm.pc] C routines to access memory

tjr@ihnet.ATT.COM (Tom Roberts) (11/10/87)

To reference memory on an IBM-compatible PC, using C pointers,
you must be sure that you know which compiler you are using;
THEY DIFFER IN IMPORTANT WAYS!!!

Turbo C:
	/* in L model, you can omit the "far" keyword */

	char far *p;

	p = MK_FP(0xB800,0);
	*p = 'A'; /* sets B800:0000 to 'A' */

	p = (char far *)0xB8000000L; /* equivalent to the MK_FP() above */

Lattice C:
	/* Lattice C has no "far" keyword, use L model (newer versions might) */

	char *p;

	p = 0xB8000L;
	*p = 'A'; /* sets B800:0000 to 'A' */

NOTE: these two compilers use a different mapping between longs and
far pointers: Turbo C simply copies them; Lattice C treats longs as
a flat, uniform address (which I consider much more useful).
If you mix them up, you will clobber random locations in memory.
When I converted from Lattice C (v 2.15) to Turbo C (v 1.00), I did
not know this, AND MY HARD DISK GOT TRASHED SEVERAL TIMES BEFORE I
DISCOVERED THIS DIFFERENCE.

Note also that "far pointer" in Lattice C L model, is similar to the
Turbo C "huge pointer", NOT Turbo C "far pointer" (huge pointers are
normalized after arithmetic, far pointers ARE NOT).

Tom Roberts
ihnp4!ihnet!tjr

ching@amd.AMD.COM (Mike Ching) (11/12/87)

There have been a lot of replies that discuss how to use pointers to
access memory but every C compiler that I have has peek and poke library
routines (C86, Turbo C, DeSmet C) which are adequate for simple
access. The pointer technique is more convenient for multiple
sequential accesses but isn't necessary.

mike ching