🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.sys.atari.st
Article #33069

Memory Access in Modula 2

#33069
From: Eric R. Terrell
Date: Wed, 29 Jun 1988 15:07
55 lines
1941 bytes
Reading and writing from/to an absolute memory address in Modula 2:

I know of a "dirty trick" to accomplish this in Pascal, which will probably work
with Modula 2 as well.  It's based on the fact that variant records
allow Pascal's normally strong typing to be circumvented.

Declare a variant record with a long (4 byte) integer field and a pointer to a
character field in the variant part).  Recall that fields in the variant part
are placed in the same region of memory.

Then to peek from memory, set the long integer field to the desired address,
and get the desired value by dereferencing the pointer field.

Similarly, to poke a value into memory, set the long integer field to the
desired address, and assign the value that you want to poke to the 
dereferenced pointer.

For example:



program peek_poke(input, output);

type
  mem_rec = record

              case tag : boolean of

                true:  (addr : integer);
                false: (ptr  : ^char);

              end; 

var
  dirtytrick : mem_rec;
  value      : char;

begin

  (* Poke value 128 into address 12345. *)
  dirtytrick.addr := 12345;
  dirtytrick.ptr^ := 128;

  (* Fetch the value from address 12345. *)
  dirtytrick.addr := 12345;
  value := dirtytrick.ptr^;

end.


Note that the addr field of the variant record must be a 4 byte integer.

Of course, code written with this trick will tend to be non-portable, so if
you can dig up a library routine that accomplishes this, that would be
preferable.
Technical Details
Message-ID: <3188@druhi.ATT.COM>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!seismo!sundc!pitstop!sun!decwrl!labrea!eos!ames!pacbell!att!whuts!homxb!mtuxo!mtgzz!drutx!druhi!terrell

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!seismo!sundc!pitstop!sun!decwrl!labrea!eos!ames!pacbell!att!whuts!homxb!mtuxo!mtgzz!drutx!druhi!terrell
From: terrell@druhi.ATT.COM (Eric R. Terrell)
Newsgroups: comp.sys.atari.st
Subject: Memory Access in Modula 2
Message-ID: <3188@druhi.ATT.COM>
Date: 29 Jun 88 15:07:23 GMT
Reply-To: terrell@druhi.UUCP (TerrellE)
Organization: AT&T, Denver, CO
Lines: 55