Recent

Author Topic: Accessing local variables from an asm block of a nested procedure  (Read 566 times)

Avinash

  • Full Member
  • ***
  • Posts: 118
A similar construction can be seen in outline.pas from the original Borland Turbo Vision package:
Code: Pascal  [Select][+][-]
  1. {$IFDEF FPC}{$ASMMODE INTEL}{$ENDIF}
  2.   procedure Test;
  3.   var I: Integer;
  4.  
  5.     procedure Nested;
  6.     {var J: Integer;}
  7.     begin
  8.       asm
  9.        {$IFDEF VER70}
  10.          mov bx, word ptr [bp]  {get parent BP}
  11.          mov word ptr ss:[bx + OFFSET I], 12345
  12.        {$ELSE}
  13.          mov eax, dword ptr [ebp]  {get parent EBP}
  14.          mov dword ptr [eax + OFFSET I], 12345
  15.          //mov dword ptr [eax - 4], 12345
  16.        {$ENDIF}
  17.       end;
  18.     end;
  19.  
  20.   begin
  21.     I := 0;
  22.     Nested;
  23.     WriteLn(I);  { outputs '12345' }
  24.   end;
  25.  
  26. begin
  27.   Test;
  28. end.

This way it compiles and works with both TP and FPC 3.2.2. However, if you uncomment the declaration of the J variable, then FPC refuses to compile it (Error: You cannot reach I from that code). At the same time, the declaration of the J variable does not affect the offsets at all — if you use a line with [eax - 4] instead of OFFSET, it will work in both cases.

Thaddy

  • Hero Member
  • *****
  • Posts: 14371
  • Sensorship about opinions does not belong here.
Re: Accessing local variables from an asm block of a nested procedure
« Reply #1 on: November 11, 2022, 01:29:24 pm »
May be that OFFSET is now supported, but not that I know of. There are some very old bug reports from many moons ago (at least 4 years, probably more) wherein I asked for OFFSET support. Forgot if it is actually implemented or not.
I usually fix it in the way you describe.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Accessing local variables from an asm block of a nested procedure
« Reply #2 on: November 11, 2022, 02:03:07 pm »
Probably the stackframe from nested gets eliminated, since there are no local variables, and I is not acceded from nested(assembler doesn't count)

 Maybe forcedly enabling stackframes will help:

https://www.freepascal.org/docs-html/prog/progsu78.html

 

TinyPortal © 2005-2018