Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!hao!oddjob!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Looking for C functions to access PC memory Message-ID: <9307@mimsy.UUCP> Date: Wed, 11-Nov-87 19:55:47 EST Article-I.D.: mimsy.9307 Posted: Wed Nov 11 19:55:47 1987 Date-Received: Sat, 14-Nov-87 05:58:48 EST References: <24261F3U@PSUVMB> <2312@emory.uucp> <151@fxgrp.UUCP> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 54 In article <151@fxgrp.UUCP> ljz@fxgrp.UUCP (Lloyd Zusman) writes: [example `peek' and `poke' routines using `movedata' deleted] >Shorter versions of these could be written ... [shorter examples deleted] >However, these shorter versions violate strict typing conventions, as >they make use of the fact that a "char far *" takes up the same space >on the stack as two unsigned int's (on the PC). A syntax checker like >'lint' wouldn't like this code, nor would many strict-typing purists. `peek' and `poke' are already utterly machine dependent, hence it does not matter how machine-dependently you code them. Were I to use an IBM PC, I might write something like this: #define peek(addr) (*(unsigned char far *)(addr)) #define poke(addr, value) (*(char far *)(addr) = (value)) If I had to construct addresses from segment+offset, I might use some variation on this: #define buildaddr(seg, off) (((long)(seg) << 16) + (off)) /* I thought segments were << 4 ...? */ Using `movedata' to copy one byte from one location is overkill. Now then, as to style and machine-dependency: the routines that actually *use* peek and poke should be hidden away inside other routines that do whatever is really desired. Someone mentioned controlling DTR on an on-board modem: #define DTRADDR ((char far *)0x12345678) /* note that the L suffix is not strictly necessary */ /* the value above is certain to be wrong */ #define DTR_BIT 0x20 /* also likely wrong */ /* * Turn DTR on (if onoff!=0) or off (if onoff==0). */ set_dtr(onoff) int onoff; { if (onoff) *DTRADDR |= DTR_BIT; else *DTRADDR &= ~DTR_BIT; } => Hide machine dependent operations inside machine-independent routines. <= -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690) Domain: chris@mimsy.umd.edu Path: uunet!mimsy!chris