Discussion:
printing an address
(too old to reply)
a***@drrob1.com
2015-03-13 23:54:09 UTC
Permalink
Hi. I decided to see if I can get the LinkList code example in the
tutorial to work using Stony Brook M2 under WinXP. And I wanted to
print out the dynamic addresses. This is what I wrote, assuming all
needed IMPORT statements are there:

PROCEDURE AdrToHexStr(adr : ADDRESS;
VAR INOUT OutStr : ARRAY OF CHAR);

CONST ASCZERO = ORD('0');
ascA = ORD('A');

VAR i,j,h,L : CARDINAL;
Str20 : STR20TYP;
adrcard : ADRTOCARD;

BEGIN
i := 0;
L := CARDINAL(adr); (* won't compile *)
L := ORD(adr); (* won't compile *)
L := TRUNC(adr); (* won't compile *)
L := VAL(CARDINAL,adr); (* won't compile *)

REPEAT (* until L = 0 *)
h := L MOD 16;
IF (h <= 9) THEN
Str20[i] := CHR(h + ASCZERO)
ELSE
Str20[i] := CHR(h -10 + ascA)
END;
INC(i);
L := L DIV 16;
UNTIL L = 0;
j := 1; (* first posn is a space to leave room for sign char *)
OutStr[0] := ' ';
REPEAT (* until i = 0 *)
DEC(i);
OutStr[j] := Str20[i];
INC(j);
UNTIL i = 0;
OutStr[j] := 0C;
END AdrToHexStr;


As my first attemtp to cast the address to a cardinal failed
miserably, I used a variant record, as below.


PROCEDURE AdrToHexStr(adr : ADDRESS;
VAR INOUT OutStr : ARRAY OF CHAR);

CONST ASCZERO = ORD('0');
ascA = ORD('A');

TYPE ADRTOCARD = RECORD
CASE :BOOLEAN OF
TRUE: A : ADDRESS;
| FALSE: C : CARDINAL;
END; (* CASE *)
END; (* RECORD *)

VAR i,j,h,L : CARDINAL;
Str20 : STR20TYP;
adrcard : ADRTOCARD;

BEGIN
i := 0;
adrcard.A := adr;
L := adrcard.C;
REPEAT (* until L = 0 *)
h := L MOD 16;
IF (h <= 9) THEN
Str20[i] := CHR(h + ASCZERO)
ELSE
Str20[i] := CHR(h -10 + ascA)
END;
INC(i);
L := L DIV 16;
UNTIL L = 0;
j := 1; (* first posn is a space to leave room for sign char *)
OutStr[0] := ' ';
REPEAT (* until i = 0 *)
DEC(i);
OutStr[j] := Str20[i];
INC(j);
UNTIL i = 0;
OutStr[j] := 0C;
END AdrToHexStr;


The variant record solution works. My question is this: why did all
of my attempts to cast the address to a cardinal fail?

Thanks,
Rob
j***@gmail.com
2015-03-14 09:08:29 UTC
Permalink
Linked list examples

http://fruttenboel.verhoeven272.nl/mocka/linklist.html
http://fruttenboel.verhoeven272.nl/obc/obc4.html
http://fruttenboel.verhoeven272.nl/obc/fac1.html
http://fruttenboel.verhoeven272.nl/ack/lili.html

ADDRESS vs CARDINAL: both are separate TYPEs so you need to type-convert between the two and there is no standard type conversion method in Modula-2. Wirth did not include it because he doesn't want to user pointer arithmatic....

One method would be, probably, never tried it,

TYPE adrconv = RECORD c1, c2 : CARDINAL END

declare a suitable variable 'k', load it with an address and then access the parts with k.c1 and k.c2

Perhaps XDS Modula has a built-in procedure for it. You may check.
Chris Burrows
2015-03-14 22:57:57 UTC
Permalink
Post by j***@gmail.com
Wirth did not include it because he doesn't want to user pointer arithmatic....
Nonsense! Quoting from the Low-level Facilities chapter of "Programming in Modula-2" by Niklaus Wirth:

"In addition, arithmetic operators can be applied to operands of type ADDRESS as if they were of type CARDINAL."

Chris Burrows
CFB Software
http://www.cfbsoftware.com/modula2

tbreeden
2015-03-14 15:35:30 UTC
Permalink
Post by a***@drrob1.com
Hi. I decided to see if I can get the LinkList code example in the
tutorial to work using Stony Brook M2 under WinXP. And I wanted to
print out the dynamic addresses. This is what I wrote, assuming all
...
L := CARDINAL(adr); (* won't compile *)
Try using this

L := CAST(CARDINAL, adr);

Stony Brook generally follow the ISO M2 standard, though you may be able to
override that with a compiler switch.

A few, like INT(), TRUNC(), FLOAT(), and ORD() were retained, but only for converting
between "ordinal" types (plus REAL), not for a type like ADDRESS.

Type casts via the TYPENAME() syntax is not accepted but will
work via the CAST(TYPE, ) syntax. You will have to IMPORT CAST from
pseudo module SYSTEM and it will act as a marker for any code written that could be
dependent on specific machine architecture. eg, if the size of a CARDINAL is
not equal to the size of ADDRESS, this type conversion will not work well.

Tom
Loading...