[comp.lang.pascal] Turbo Pascal typecasting and the Mem array.

CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) (02/27/91)

In article <1991Feb27.033001.2520@ccu.umanitoba.ca>,
  yackob@eeserv.ee.umanitoba.ca (Zaifu Kerry Yackoboski) wrote:


[...deleted]

>        I'm reading in integers (i.e., signed 16 bit) from a file
>and trying to store them in memory using the Mem[segment:offset]
>array.  My dilemma is that Mem expects a byte (unsigned 8 bit), so I
>tried to use the MemW[..] array, except this expects a word (i.e.,
>unsigned 16 bit).
>
>        Neither Mem nor MemW seems to give me the correct result,
>although Mem seems to be somewhat close (when I plot out the data,
>it has the right shape but the wrong values).
>
>        I think a possible solution might be a value type-cast of
>the sort
>
>        MemW[seg:offset] := Word (my_integer);
>
>but the explanation in the reference isn't clear; it says the sign
>is always preserved when the value is extended, whatever that means.
>I'll try this, but I hate to just muck about and hope to find the
>correct answer.

Try that, or jump directly to

        Move(my_integer,MemW[segment:offset],SizeOf(my_integer));

>        Another aspect of the problem that I'm hazy about (and
>therefore this might be the real problem) is I'm unsure about how to
>increment the offset with the MemW[seg:offset] array; should I be
>increasing by two's, since there are 2 bytes per array element, or
>does the compiler do that for me, allowing me to increment by one's
>only ?

        By 2. Better, by SizeOf(my_integer). But watch out for the
end of the segment.

As an alternative approach, declare a type that is a pointer to an
array of integer, then assign to it the address of the first element
of interest in MemW, then just deal with that array (pointed to by
your pointer), thus only worrying about the index of the array
rather than segment arithmetic. E.g., if seg0 and ofs0 are the
segment and offset of that first element:

type
  tIntArrayPtr = ^tIntArray;
  tIntArray = array[0..0]of integer;
var
  p : tIntArrayPtr;
  seg0,ofs0 : word;
  i : integer;
begin
  p := Ptr(seg0,ofs0);
  p^[0] := i;

(Note the declaration of tIntArray as array[0..0]. You can do this
and turn range checking off where you reference elements beyond the
first, or you can assign some different bounds for the index, as you
prefer.)

yackob@eeserv.ee.umanitoba.ca (Zaifu Kerry Yackoboski) (02/28/91)

	After solving my problem I tried to cancel the article
explaining it, but "rn" told me I couldn't cancel someone else's
article.  Sorry about that, but since I have cleared up my problem,
here is the solution.


In article <1991Feb27.033001.2520@ccu.umanitoba.ca> I wrote :
>	I'm having trouble casting an integer type variable to
>a word type.  Basically, I just don't understand what's going on
>in the TP Reference Guide (Version 5.0, pages 42, 64, and 208).
>Hopefully some net.god can clear a few things up for me.
>
>	I'm reading in integers (i.e., signed 16 bit) from a file
>and trying to store them in memory using the Mem[segment:offset]
>array.  My dilemma is that Mem expects a byte (unsigned 8 bit),
>so I tried to use the MemW[..] array, except this expects a word
>(i.e., unsigned 16 bit).
>Kerry Yackoboski 	<yackob@eeserv.ee.umanitoba.ca>

	The solution is to do a type cast when I USE the value in the
MemW array.  e.g., if temp_value is an integer,

	temp_value := integer (MemW[seg:offset]);

works.  I also have to increment the offset twice to advance to the next
data value, as I suspected.

	sorry if I've wasted bandwidth on a well-known concept, Kerry

--
Kerry Yackoboski 	<yackob@eeserv.ee.umanitoba.ca>
The Scanning Tunneling Microscopy Laboratory in the Cellar
U of Manitoba, Winnipeg, Canada