Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!lll-winken!lll-lcc!ames!fxgrp!ljz From: ljz@fxgrp.UUCP (Lloyd Zusman) Newsgroups: comp.sys.ibm.pc,comp.lang.c Subject: Re: Looking for C functions to access PC memory Message-ID: <151@fxgrp.UUCP> Date: Sun, 8-Nov-87 14:55:09 EST Article-I.D.: fxgrp.151 Posted: Sun Nov 8 14:55:09 1987 Date-Received: Tue, 10-Nov-87 04:17:27 EST References: <24261F3U@PSUVMB> <2312@emory.uucp> Reply-To: ljz@fxgrp.UUCP (Lloyd Zusman) Followup-To: <2312@emory.uucp> platt@emory.UUCP (Dan Platt) Organization: Master Byte Software Lines: 86 Xref: mnetor comp.sys.ibm.pc:9888 comp.lang.c:5328 In article <2312@emory.uucp> platt@emory.UUCP (Dan Platt) writes: >In article <24261F3U@PSUVMB> F3U@PSUVMB.BITNET writes: >>I am looking for some functions when called from a C program, will: >> 1) read (peek) memory directly when supplied with >> an address. Returns with the value at specified address. >> and >> 2) write (poke) memory directly when supplied with an >> address and a byte value. >> >>I am using Microsoft C 4.0 on an AT&T 6300 PLUS. Routines >>in C or Assembler OK. There already functions that do this dort of thing in Microsoft C version 4.0 ... void movedata(srcseg, srcoff, destseg, destoff, nbytes) unsigned int srcseg; unsigned int srcoff; unsigned int destseg; unsigned int destoff; unsigned int nbytes; This function copies 'nbytes' bytes of data from srcseg:srcoff to destseg:destoff. If you're in a small- or medium-model program, the 'destseg' parameter can be gotten via the segread() function (it will be the DS register value). But if your really MUST have something like peek and poke, here's a quickly-hacked (i.e., while I'm typing here right now) attempt at these. They're written to work in any memory model. There might be a bug or two, but this is the general idea ... unsigned char peek(segment, offset) unsigned int segment; unsigned int offset; { unsigned char result; unsigned long address = (unsigned long)((char far *)&result); movedata(segment, offset, address >> 16, address & 0x0000ffffL, 1); return (result); } void poke(segment, offset, byte) unsigned int segment; unsigned int offset; unsigned char byte; { unsigned long address = (unsigned long)((char far *)&byte); movedata(address >> 16, address & 0x0000ffffL, segment, offset, 1); } Shorter versions of these could be written ... unsigned char peek(segment, offset) unsigned int segment; unsigned int offset; { unsigned char result; movedata(segment, offset, (char far *)&result, 1); return (result); } void poke(segment, offset, byte) unsigned int segment; unsigned int offset; unsigned char byte; { movedata((char far *)&byte, segment, offset, 1); } 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. I don't remember if the cast should be "char far *" or "char * far", so if this doesn't work, try it the other way. -- Lloyd Zusman, Master Byte Software, Los Gatos, California "We take things well in hand." ...!ames!fxgrp!ljz