Lazarus

Programming => General => Topic started by: 440bx on May 01, 2021, 01:24:16 am

Title: Referencing a label
Post by: 440bx on May 01, 2021, 01:24:16 am
Hello,

I want to get the address associated with a label but, I don't seem to get the syntax right.  I tried the following:
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2.  
  3. {$TYPEDADDRESS ON}
  4.  
  5. program _LabelReference;
  6.  
  7. uses
  8.   sysutils
  9.   ;
  10.  
  11. {$ifdef VER90}
  12. type
  13.   DWORD    = longint;
  14.   PDWORD   = ^DWORD;
  15.  
  16.   ptruint  = DWORD;
  17. {$endif}
  18.  
  19.  
  20. label
  21.   Mark;
  22.  
  23. begin
  24.   writeln;
  25.   writeln;
  26.  
  27.   Mark:
  28.  
  29.   writeln('  address of mark is : ', IntToHex(ptruint(Mark), 0));
  30.  
  31.   writeln;
  32.   writeln;
  33.   writeln('press ENTER/RETURN to end this program');
  34.   readln;
  35. end.      
which produces a compiler error. I also tried a few variations but none provided any joy.

Is there a way to get the address the label references ? and if the answer is yes, how it's done will be greatly appreciated.

Thank you.
Title: Re: Referencing a label
Post by: engkin on May 01, 2021, 01:34:43 am
Try @Mark.
Title: Re: Referencing a label
Post by: 440bx on May 01, 2021, 01:43:12 am
Try @Mark.
Thank you Engkin.  That works with FPC.  Unfortunately, it doesn't work with Delphi 2 (I don't know if it works or not with later versions.)

With Delphi 2, the only way I found was to use assembler.

Thank you again.
Title: Re: Referencing a label
Post by: engkin on May 01, 2021, 01:51:52 am
Unfortunately I don't known much about Delphi, but I see it works with $mode Delphi. I know it doesn't help as you already tested with Delphi 2.
Title: Re: Referencing a label
Post by: 440bx on May 01, 2021, 02:27:38 am
Unfortunately I don't known much about Delphi, but I see it works with $mode Delphi. I know it doesn't help as you already tested with Delphi 2.
I appreciate the help.  FPC works as it should.  There is nothing esoteric about a label but, Delphi 2 seems to treat it as a "sacred object".

Hmm, I find that hard to believe the @ does not work with D2 ?

anyways, did you try Addr(The Item of Interest ):Pointer;
Hard to believe but, believe it.  I hadn't tried "Addr" but, tried it after you suggested it and, no luck. 

Thank you for the suggestion.

Title: Re: Referencing a label
Post by: PascalDragon on May 01, 2021, 04:37:25 pm
Current Delphi still doesn't allow to take the address of a label... 🤷‍♀️
Title: Re: Referencing a label
Post by: 440bx on May 01, 2021, 07:56:14 pm
Current Delphi still doesn't allow to take the address of a label... 🤷‍♀️
Thank you for providing that information.  I tried everywhich way I could think of to no avail. 

I'm rather pleased that FPC allows taking the address without fuss (there really isn't any reason for a fuss in that case anyway.)

Title: Re: Referencing a label
Post by: 440bx on May 18, 2021, 01:50:41 pm
Hi Bx, did you try with $mode Delphi?
Hi Connie.  No, I didn't try $mode Delphi because I want my executables to compile with both FPC and Delphi 2 _without_ setting any mode (which means, I always use FPC's default mode - except in somewhat rare cases.)
Title: Re: Referencing a label
Post by: Thaddy on May 18, 2021, 03:20:07 pm
Well, FPC's default mode is not delphi compatible...
Title: Re: Referencing a label
Post by: 440bx on May 18, 2021, 03:27:37 pm
Well, FPC's default mode is not delphi compatible...
But, since I don't use objects and/or classes, there are only very few instances when I need to write code that is specific to either compiler.

The only time I code to FPC is when the program is going to be 64bit only, otherwise, it's fairly easy to make both compilers happy with a single source with just a very few "$ifdef" to keep them happy (very often, just one small group of definitions are the beginning of the source in a single "$ifdef".)
Title: Re: Referencing a label
Post by: PascalDragon on May 19, 2021, 09:08:03 am
Well, FPC's default mode is not delphi compatible...
But, since I don't use objects and/or classes, there are only very few instances when I need to write code that is specific to either compiler.

That's why I refrained from making the exact same remark that Thaddy had done (I did think about it, though :-[ ), cause I assumed that you know what you're doing. :)
TinyPortal © 2005-2018