Forum > General

Accessing local variables from an asm block of a nested procedure


A similar construction can be seen in outline.pas from the original Borland Turbo Vision package:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$IFDEF FPC}{$ASMMODE INTEL}{$ENDIF}  procedure Test;  var I: Integer;     procedure Nested;    {var J: Integer;}    begin      asm       {$IFDEF VER70}         mov bx, word ptr [bp]  {get parent BP}         mov word ptr ss:[bx + OFFSET I], 12345       {$ELSE}         mov eax, dword ptr [ebp]  {get parent EBP}         mov dword ptr [eax + OFFSET I], 12345         //mov dword ptr [eax - 4], 12345       {$ENDIF}      end;    end;   begin    I := 0;    Nested;    WriteLn(I);  { outputs '12345' }  end; begin  Test;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.

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.

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:


[0] Message Index

Go to full version