Recent

Author Topic: Referencing a label  (Read 5431 times)

440bx

  • Hero Member
  • *****
  • Posts: 3921
Referencing a label
« 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Referencing a label
« Reply #1 on: May 01, 2021, 01:34:43 am »
Try @Mark.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Referencing a label
« Reply #2 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.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Referencing a label
« Reply #3 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.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Referencing a label
« Reply #4 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.

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Referencing a label
« Reply #5 on: May 01, 2021, 04:37:25 pm »
Current Delphi still doesn't allow to take the address of a label... 🤷‍♀️

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Referencing a label
« Reply #6 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.)

(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Referencing a label
« Reply #7 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.)
« Last Edit: May 18, 2021, 01:52:50 pm by 440bx »
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Referencing a label
« Reply #8 on: May 18, 2021, 03:20:07 pm »
Well, FPC's default mode is not delphi compatible...
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3921
Re: Referencing a label
« Reply #9 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".)
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5444
  • Compiler Developer
Re: Referencing a label
« Reply #10 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