Board index » delphi » am I stupid?

am I stupid?

Hi folks,

here is something for the pros:

var p:^byte;

...

p^:=99;  {or any other byte}
writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

Compiler TP 6, Real Mode, This occurs no mater if data word aligment is
switched on or off.

So, why???? And, of course, where stands my number?

Tomy

Tomy_Neum...@t-online.de

 

Re:am I stupid?


Quote
Thomas Neumann wrote:

> Hi folks,

> here is something for the pros:

> var p:^byte;

> ...

> p^:=99;  {or any other byte}
> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

> Compiler TP 6, Real Mode, This occurs no mater if data word aligment is
> switched on or off.

> So, why???? And, of course, where stands my number?

> Tomy

> Tomy_Neum...@t-online.de

Have you reserved memory for the pointer (getmem())?

Remco
http://yi.com/home/DekorteRemco

Re:am I stupid?


Quote
Thomas Neumann wrote:

> Hi folks,

> here is something for the pros:

> var p:^byte;

> ...

> p^:=99;  {or any other byte}
> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

> Compiler TP 6, Real Mode, This occurs no mater if data word aligment is
> switched on or off.

> So, why???? And, of course, where stands my number?

> Tomy

> Tomy_Neum...@t-online.de

try:

var p:^byte;
begin
  GetMem(p, sizeof(byte));
  p^:=99;  {or any other byte}
  Writeln(p^);
  FreeMem(p, sizeof(byte));
end.

Joe
--
Joe C. Hecht
(Borland Delphi Developer Support)
Join the Delphi Online Discussion Forum at
http://www.borland.com/techsupport/delphi/

Re:am I stupid?


 Remco de Korte wrote in article <3364B58C.3...@xs4all.nl>...

Quote
>Thomas Neumann wrote:

>> Hi folks,

>> here is something for the pros:

>> var p:^byte;

>> ...

>> p^:=99;  {or any other byte}
>> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

>> Compiler TP 6, Real Mode, This occurs no mater if data word aligment is
>> switched on or off.

>> So, why???? And, of course, where stands my number?

>> Tomy

>> Tomy_Neum...@t-online.de

Sorry, I didn't have the original message to respond to... but, try this:

p^ := 99; {or any other byte}
writeln(Mem[seg(p^):ofs(p^)]); {Does write 99!!}

It's because Mem[seg(p):ofs(p)] will read the first byte of the variable p,
which is the pointer to the value 99.  So you're reading the value of p,
not the value that p is pointing to.

Jeremy

Re:am I stupid?


Quote
Thomas Neumann (Tomy_Neum...@t-online.de) writes:
> Hi folks,

> here is something for the pros:

> var p:^byte;

> ...

> p^:=99;  {or any other byte}
> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

> Compiler TP 6, Real Mode, This occurs no mater if data word aligment is
> switched on or off.

> So, why???? And, of course, where stands my number?

Your pointer in the writeln statement above points to the pointer
 itself, and not the address it holds.

var p: ^byte

Begin
 New(p);
 p^ := 99;
 Writeln(mem[seg(p^):ofs(p^)]);
 Dispose(p);
End.

Steve

Re:am I stupid?


Quote
> Hi folks,
> here is something for the pros:
> var p:^byte;
> ...
> p^:=99;  {or any other byte}
> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

> (blah blah blah)

> Tomy_Neum...@t-online.de

You`re setting what p poiint to with the value of 99 (dangerous in it`s
self without a new() or getmem())

But try the following writeln, writeln(mem[seg(p^):ofs(p^)])
That makes pascal show the value AT that memory address (99)
--

Robert Smith.
                          ~ ~
                         (o o)
            ________oOOo__(_)__oOOo______________
           /                                    /\
          /       E-MAIL Robert Smith @        / /
         /  i94robertsm...@bcoll.demon.co.uk  / /
        /____________________________________/ /
        \____________________________________\/
   http://www.beauchamp.leics.sch.uk/students/smith_r

Re:am I stupid?


On Mon, 28 Apr 1997 16:34:52 +0200, Remco de Korte

Quote
<remco...@xs4all.nl> wrote:
>Thomas Neumann wrote:

>> Hi folks,

>> here is something for the pros:

>> var p:^byte;

>> ...

>> p^:=99;  {or any other byte}
>> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

>> Compiler TP 6, Real Mode, This occurs no mater if data word aligment is
>> switched on or off.

>> So, why???? And, of course, where stands my number?

>> Tomy

>> Tomy_Neum...@t-online.de

>Have you reserved memory for the pointer (getmem())?

>Remco
>http://yi.com/home/DekorteRemco

Thomas,

Let's take it from the top. (Most of what I'm going to write now you
already know. Just humor me, if you will.)

        var p: ^byte;

means P is a pointer to a byte somewhere in memory. Therefore, P
itself is a variable that contains an address pointing to whatever is
in P^.

        seg(p)

returns the segment of the variable P, NOT the segment of the byte
pointed to by the contents of P.

In a sense, you code should read:

        writeln(mem[seg(p^):ofs(p^)]);

except I don't think TP will be happy with that. Try it, if it works,
fine, if it doesn't, do it this way:

        var p: ^byte;
              a: array[0..1] of word;
        begin
                getmem(p,1);
                p^:=99;
                move(p,a,4);
                writeln(mem[a[1]:a[0]]);
                freemem(p,1)
        end.

Please let me know if this works out.

--
Maurice Valmont
West Palm Beach, FL

Re:am I stupid?


Quote
Thomas Neumann wrote:

> Hi folks,

> here is something for the pros:

> var p:^byte;

> ...

> p^:=99;  {or any other byte}
> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

I assume that "..." implies that you allocated space or otherwise
initialized p.  The natural way to access the value is to
dereference the pointer, as Writeln(p^);  

But, in answer to your question - you asked to display the byte at
"p".  I say again, you asked to display the low byte of the offset
of the memory location where the pointer "p" is located.  If you
want to use mem to access the 99 you stored, then you have to tell
seg and ofs that they are to use the location of the dereferenced
pointer and not the location of the pointer.

Try using  Mem[Seg(p^),Ofs(p^)]

Quote
>> So, why???? And, of course, where stands my number?

This is one of those "misunderstandings" that usually ends with a
swift slap to the forehead.  Just don't hit too hard.  ;-)

    ...red

Re:am I stupid?


Quote
Thomas Neumann wrote:

> Hi folks,

> here is something for the pros:

> var p:^byte;

> ...

> p^:=99;  {or any other byte}
> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

Hopefully, the "..." includes defining the value of p -- when your
program begins, p will be undefined (under the IDE, will probably be
nil), and writing to an unitialized pointer is a Bad Thing.  In the IDE,
if this is nil, that means you're writing an arbitrary value to
0000:0000h, which is interrupt vector 0.  Doing that is likely to crash
the system.

Quote
> Compiler TP 6, Real Mode, This occurs no mater if data word aligment is
> switched on or off.

> So, why???? And, of course, where stands my number?

It's right where you put it.  The problem, more or less explained by
others, is that your code is giving you what you're asking for, not what
you want.  This should be mem[seg(p^):ofs(p^)].  The seg() and ofs()
return the segment and offset parts of the address of what you specify,
so mem[seg(p):ofs(p)] will return the low-order byte of the offset part
of the pointer as stored in the data segment.

Of course, as also mentioned, "writeln (p^);" will output "99".  Even
then, it's still a useful exercise to help explain how pointers work.
:-)

Quote
> Tomy

> Tomy_Neum...@t-online.de

--
Scott Earnest        | We now return you to our regularly |
set...@ix.netcom.com | scheduled chaos and mayhem. . . .  |

Re:am I stupid?


On 26 Apr 1997 07:38:47 GMT, Tomy_Neum...@t-online.de (Thomas Neumann)
wrote:

Quote
>var p:^byte;
>p^:=99;  {or any other byte}
>writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

WriteLn(mem[seg(p):ofs(p)]) is equal to WriteLn(Byte(p)),
you write out the first byte (lo-byte of lo-word) of the
pointer-variable.

What you need is: WriteLn(mem[seg(p^):ofs(p^)]). Ofs(p^) returns the
offset of the memory-location "pointed" by the adress stored in p.

(I can explain it in german if you don't understand my very bad
english)

--
Christian Tschenett
crts...@bluewin.ch

Re:am I stupid?


On 26 Apr 1997 07:38:47 GMT, Tomy_Neum...@t-online.de (Thomas Neumann)
wrote:

Quote
>Hi folks,

>here is something for the pros:

>var p:^byte;

>...

>p^:=99;  {or any other byte}
>writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

Your p is a pointer, being 4 bytes large.
The code will show you what is the first byte of the pointer address.

seg(p^):ofs(p^) should work (not sure !).

And : no, you're *not* stupid. This pointer {*word*99} can be very
irritating ...

Regards,
Marco
--
Marco Schmidt - Student of Computer Science, Aachen, Germany
mailto:ma...@pool.informatik.rwth-aachen.de
http://www.geocities.com/SiliconValley/Lakes/6686

Re:am I stupid?


On 26 Apr 1997, Thomas Neumann wrote:

Quote

> Hi folks,

> here is something for the pros:

> var p:^byte;

> ...

> p^:=99;  {or any other byte}
> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

        If you want to write 99, use:

var p:byte; { NOT pointer }
...
p:=99;
writeln (mem[seg(p):ofs(p)]);

Your code will give one byte of ADDRESS where number 99 is stored

smi...@afrodita.rcub.bg.ac.yu              Slobodan Mihajlovic

Re:am I stupid?


Quote
ma...@pool.informatik.rwth-aachen.de (Marco Schmidt) wrote:
>And : no, you're *not* stupid. This pointer {*word*99} can be very
>irritating ...

Amen!

    ...red

Re:am I stupid?


Quote
Thomas Neumann wrote:

> Hi folks,

> here is something for the pros:

> var p:^byte;

> ...

> p^:=99;  {or any other byte}
> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

Look, you`re setting an area of memory to the value of 99, then printing
the location of the pointer, not where the pointer points to.  Try this:

writeln(mem[seg(p^):ofs(p^)]);

--

Robert Smith.
                          ~ ~
                         (o o)
            ________oOOo__(_)__oOOo______________
           /                                    /\
          /       E-MAIL Robert Smith @        / /
         /  i94robertsm...@bcoll.demon.co.uk  / /
        /____________________________________/ /
        \____________________________________\/
   http://www.beauchamp.leics.sch.uk/students/smith_r

Re:am I stupid?


In article <3364BD8C.4...@bcoll.demon.co.uk> of Mon, 28 Apr 1997
16:09:00 in comp.lang.pascal.borland, Robert Smith <i94robertsmith@bcoll

Quote
.demon.co.uk> wrote:
>> Hi folks,
>> here is something for the pros:
>> var p:^byte;
>> ...
>> p^:=99;  {or any other byte}
>> writeln(mem[seg(p):ofs(p)]);  {Does not write 99!!}

>> (blah blah blah)

>> Tomy_Neum...@t-online.de

>You`re setting what p poiint to with the value of 99 (dangerous in it`s
>self without a new() or getmem())

You're not necessarily setting it, of course; only if the pointer points
to RAM.  It may point to ROM or in some PCs to nothing at all.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics and links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Before a reply, quote with ">" / "> ", known to good news readers (SoRFC1036)

Go to page: [1] [2]

Other Threads