marshek@ut-ngp.UUCP (The Genie) (08/26/87)
Anybody out there have any stuff to do a peek & poke function in C ? (The peek & poke of BASIC). Further, do you have any idea where I can get a C code to do simple calculator functions (+,-,*,/,log) and the trigonometric functions on an IBM PC..... Thanx in advance.
tr@wind.bellcore.com (tom reingold) (08/27/87)
In article <6050@ut-ngp.UUCP> marshek@ut-ngp.UUCP (The Genie) writes: $ $ Anybody out there have any stuff to do a peek & poke function in C ? $ (The peek & poke of BASIC). $ Further, do you have any idea where I can get a C code to do $ simple calculator functions (+,-,*,/,log) and the trigonometric $ functions on an IBM PC..... $ Thanx in advance. 1) Here is a sample function that uses a "poke" to the time of day counter in the PC. It is not guaranteed by Microsoft, as they recommend that we use DOS calls instead. I gather that the DOS corresponding to this accesses this memory address but that address may change one day. Enjoy. ------------------------------------------------------------ #define TICKADDR 0x46C long gettick(void) { long far* tick=MK_FP(0, TICKADDR); /* absolute pointer to tick * counter (18.2/sec) */ return(*tick); } ------------------------------------------------------------ 2) I don't have any calculator source code. Tom Reingold INTERNET: tr@bellcore.bellcore.com UUCP: {seismo,ihnp4,ucbvax,decvax}!bellcore!tr {ulysses,allegra,clyde,princeton}!bellcore!tr
dleigh@hplabsz.HPL.HP.COM (Darren Leigh) (08/27/87)
In article <6050@ut-ngp.UUCP> marshek@ut-ngp.UUCP (The Genie) writes: > >Anybody out there have any stuff to do a peek & poke function in C ? >(The peek & poke of BASIC). C does lots of interesting things with pointers so that is a good way to start. All you have to do is stuff an absolute address into a pointer. The real problem here is the brain-damaged Intel/IBM architecture, but it can be done. From Microsoft C 4.0 you can do the following: Say you want to deal with a byte at location 0040:0049 that controls the video mode. A BASIC-like videomode = peek(address) would be done like this: videomode = *(char far *) 0x400049L; This takes the absolute value 0040:0049 and casts it to a far pointer to a character (or single byte). The code for poke operates similarly. The BASIC command poke(address,videomode) could be set up as: *(char far *) 0x400049L = videomode; This again casts the absolute address 0040:0049 to a pointer to char and stuffs the value in there. This works from MSC 4.0, and may or may not work from other C's. Try playing around by setting far pointers to the absolute addresses that you need. If your C does not support far pointers and does not have a way of changing the segment registers, you are on your own and will have to play with assembly language. BTW, I pulled these little examples from page 37 of the December 1986 issue of the Microsoft Systems Journal. Darren Leigh dleigh@hplabs.hp.com
jru@etn-rad.UUCP (John Unekis) (08/27/87)
In article <6050@ut-ngp.UUCP> marshek@ut-ngp.UUCP (The Genie) writes: > >Anybody out there have any stuff to do a peek & poke function in C ? >(The peek & poke of BASIC). ... Simple, use a pointer variable, and the large model switch on the compiler. char *p; declares a pointer to a byte int *p; declares a pointer to a word to access a physical address, you must construct it in the form of a segment and offset. The segment is put in the most significant word of the pointer, the offset is put in the least significant part. The physical address you will reach is the addition of the two with the segment left shifted by four bits (one hex digit) i.e. segment YYYY0 +offset + xxxx ------- -------- physical Ynnnx address setting the pointer to a constant such as p = (char *) 0xA1112225; would reach physical address (hex) A3335 to 'peek' you would simply say variable = *p; to 'poke' you would say *p = value; -------------------------------------------------------------- hope this helps ihnp4!wlbr!etn-rad!jru