absolute 
Author Message
 absolute

Hello,

I have a program with the followings commands :

    var
        P600D_port         : word = 584;
        P600D_bytes       : array[0..1] of byte;
        P600D                 : word absolute P600D_bytes;

    ....

    for m:=1 downto 0 do
    begin
        port[P600D_port] := 32 + (adresse shl 2) + m;
        P600D_bytes[m] := port[P600D_port];
    end.

Could you tell me what is the value in "P600D" at the end of the
program.

Thanks for your assistance

Alexandre



Wed, 18 Jun 1902 08:00:00 GMT  
 absolute

Quote:

>     for m:=1 downto 0 do
>     begin
>         port[P600D_port] := 32 + (adresse shl 2) + m;
>         P600D_bytes[m] := port[P600D_port];
>     end.

> Could you tell me what is the value in "P600D" at the end of the
> program.

Use writeln to output the value to the screen.


Wed, 18 Jun 1902 08:00:00 GMT  
 absolute


Quote:
> Hello,

> I have a program with the followings commands :

>     var
>         P600D_port         : word = 584;
>         P600D_bytes       : array[0..1] of byte;
>         P600D                 : word absolute P600D_bytes;

...

> Could you tell me what is the value in "P600D" at the end of the
> program.

Not sure what you are asking here. Hopefully not the numeric value of P600D
because that would depend on the values read in and I doubt that anyone here
could predict that. From the message title, "Absolute", I guess that you
want to know what the absolute declaration means. The declaration
"P600D:word absolute P600D_bytes;" means that P600D is a word variable but
starts at the same address as P600D_bytes. That means it occupies the same
memory as P600D_bytes, in other words you can fiddle with its low and high
bytes by manipulating the two elements of the P600D_bytes array. Intel
processors hold the low byte at the lower address, so the value of P600D is
P600D_bytes[0]+256*P600D_bytes[1] -- always, not just at the end of the
program.

This is the same effect as you get by using Record Case, e.g.

Var
  v : Record
    case Integer of
    0: (P600D_bytes: Array[0..1] of Byte);
    1: (P600D: word);
  end;

AFAIK this variant record is standard but Absolute is not.

Be careful with data sizes. What happens if you absolute a variable onto
something smaller than it? For example:

Var
  v1 : byte;
  v2 : LongInt absolute v1;
Var
  v3 : byte;

Probably something unpredictable. Does v2 overlay v3 (and subsequent
variables) or not? It might be documented or it might not, but it's not
obvious and therefore to be avoided.

FP



Wed, 18 Jun 1902 08:00:00 GMT  
 absolute

Quote:

> Be careful with data sizes. What happens if you absolute a variable onto
> something smaller than it? For example:

> Var
>   v1 : byte;
>   v2 : LongInt absolute v1;
> Var
>   v3 : byte;

> Probably something unpredictable. Does v2 overlay v3 (and subsequent
> variables) or not?

It does in TP7/BP7.

Quote:
> It might be documented or it might not, but it's not
> obvious and therefore to be avoided.

Agreed. "Absolute" can be very useful at times, but don't use it unless things
would be less clear or impossible with something else.

--
Jay

Jason Burgon - Author of Graphic Vision
New version 2 now available from:
http://www.jayman.demon.co.uk



Wed, 18 Jun 1902 08:00:00 GMT  
 absolute


Quote:


> > Be careful with data sizes. What happens if you absolute a variable onto
> > something smaller than it? For example:

> > Var
> >   v1 : byte;
> >   v2 : LongInt absolute v1;
> > Var
> >   v3 : byte;

> > Probably something unpredictable. Does v2 overlay v3 (and subsequent
> > variables) or not?

> It does in TP7/BP7.

You think so? Well, it might and it might not. I've just tried it (in BP7)
and it depends on where the declarations are made. Assignments to v2 affect
v3 if the declarations are in global scope, but putting the whole thing into
a procedure changed it so that v3 was untouched by assignments to v2. V3 is
also in a different Var declaration, I would have hoped that the linker
would remove it if the program didn't access it, but for some reason that
doesn't seem to happen and I can watch v3 in a watch window either in the
IDE or in TD. Also, the state of the $A switch might affect what bits of v2
get to overlay which byte variables, although given the above declarations
it has no effect because it works on variables larger than 1 byte. Also
there is a 1-byte hole between v1 and v3 because the second "var"
declaration forces a word alignment regardless of the state of $A.

Anyway, the point is that you can't assume too much about what variables go
where. About all you could do is to overlay a variable onto an array, making
sure that the array is big enough to cover the whole of the absoluted
variable; making the array too small would be an error but the compiler
won't catch it. Absoluting onto an array is reasonably predictable because
the Programmer's Reference states that there is no padding between array
elements or fields of record structures. Although I suspect that current
versions of Delphi may no longer honour this unless they are declared to be
packed.

IMHO the variant record technique is more useful if you want to overlay one
variable onto another. The compiler will then allocate memory for the larger
of the two overlayed structures, instead of overwriting whatever happens to
come after. I would use Absolute to access data whose address is fixed, e.g.
BIOS variables, and use variant records or a cast such as WordRec from
OBJECTS.PAS if I just want to access individual bytes in a Word or LongInt
variable.

FP



Wed, 18 Jun 1902 08:00:00 GMT  
 absolute

Quote:





> > > Be careful with data sizes. What happens if you absolute a variable onto
> > > something smaller than it? For example:

> > > Var
> > >   v1 : byte;
> > >   v2 : LongInt absolute v1;
> > > Var
> > >   v3 : byte;

> > > Probably something unpredictable. Does v2 overlay v3 (and subsequent
> > > variables) or not?

> > It does in TP7/BP7.

> You think so? Well, it might and it might not. I've just tried it (in BP7)
> and it depends on where the declarations are made. Assignments to v2 affect
> v3 if the declarations are in global scope, but putting the whole thing into
> a procedure changed it so that v3 was untouched by assignments to v2.

Yes I know, I just didn't want to confuse the issue. It's because local vars
are stored in reverse order of decaration on the stack. ie the last declared
local variable has the lowest address, v3 is "below" v1, and v2 will
overrwrite the 1st 3 bytes of any variable preceeding v1, or the stack frame
if there are none.

Quote:
> V3 is also in a different Var declaration, I would have hoped that the
linker
> would remove it if the program didn't access it, but for some reason that
> doesn't seem to happen and I can watch v3 in a watch window either in the
> IDE or in TD.

How odd!

Quote:
> Also, the state of the $A switch might affect what bits of v2
> get to overlay which byte variables, although given the above declarations
> it has no effect because it works on variables larger than 1 byte. Also
> there is a 1-byte hole between v1 and v3 because the second "var"
> declaration forces a word alignment regardless of the state of $A.

Yes you're quite right. I didn't spot the inbetween "var" statement. Using
"absolute" accross var blocks is never justifyible.

Quote:
> Anyway, the point is that you can't assume too much about what variables go
> where. About all you could do is to overlay a variable onto an array,

record or pointer

Quote:
> making
> sure that the array is big enough to cover the whole of the absoluted
> variable; making the array too small would be an error but the compiler
> won't catch it. Absoluting onto an array is reasonably predictable because
> the Programmer's Reference states that there is no padding between array
> elements or fields of record structures.

Yes. When you use Absolute to overlay a variable, you are saying the the
compiler "Shut up, I know best", rather like when dropping down to assembler.
There are times though when it's a godsend, such as using is to typecast a
generic pointer variable to typed pointer variable. This is probably its most
useful function.

Quote:
> Although I suspect that current
> versions of Delphi may no longer honour this unless they are declared to be

packed.

Correct.

Quote:
> IMHO the variant record technique is more useful if you want to overlay one
> variable onto another. The compiler will then allocate memory for the larger
> of the two overlayed structures, instead of overwriting whatever happens to
> come after. I would use Absolute to access data whose address is fixed, e.g.
> BIOS variables, and use variant records or a cast such as WordRec from
> OBJECTS.PAS if I just want to access individual bytes in a Word or LongInt
> variable.

Agreed, though there is a big use for absolute with pointers. Suppose for
example you have a protected mode program that needs a real-mode accessable
block of memory to transfer data to and from O/S and BIOS functions. This
buffer would probably be allocated and a generic pointer type (type Pointer or
PByteArray - whatever) assigned to point to it. Now the contents of the buffer
will depend on which DOS/BIOS interface function was using it at the time, so
you would use the appropriate structured pointer type as an alias for the
generic buffer.

Another use is for untyped variables passed to functions and procedures, where
you don't know the type of the variable on entry. Again you could declare
several absolute "aliases" to the variable and use the appropriate one when
its type is known. This could be done with a variant record, but often isn't
practical when dealing with large structures or objects.

--
Jay

Jason Burgon - Author of Graphic Vision
New version 2 now available from:
http://www.jayman.demon.co.uk



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Absolute Basics on Delphi and ADO

2. Absolute Basics on Delphi and ADO

3. Use of absolute clause.

4. How to get Absolute value in SQL?

5. Var: Byte absolute CSeg:$...

6. which pascal for an absolute beginner ?

7. an absolute beginner : HEX question

8. Free Pascal and absolute memory

9. absolute

10. GetTime directly (by address - absolute)

11. An absolute beginners question

12. An absolute beginners question

 

 
Powered by phpBB® Forum Software