[fpc-pascal]Var parameter passing to asm procedure

Tomas Hajny XHajT03 at mbox.vol.cz
Fri Aug 6 12:29:12 CEST 2004


On Thu, 05 Aug 2004 16:56:28 +0200, Florian Klaempfl wrote
> Ivan Stamenkovic wrote:
> 
> > Hi,
> > 
> > I have a problem when trying to pass some variable parameters to a
> > procedure writen using integrated assembler. I have reduced the
> > problem to the following code:
> > 
> > program IncrementDemo;
> > {$R+}
> > {$asmmode intel}
> > 
> > var
> >    a: dword;
> > 
> > procedure increment(var x: dword); assembler;
> >    asm
> >       push ds { Is DS saved by default? }
> > 
> >       lds ebx, x
> >       mov eax, [ebx]     { Line with the problem }
> >       inc eax
> >       mov [ebx], eax
> > 
> >       pop ds
> >    end;
> > 
> > begin
> >    write('Enter a number: ');
> >    readln(a);
> >    increment(a);
> >    writeln('Result is: ', a, '.')
> > end.
> > 
> > Line with the problem is marked above. If I tried using
> > mov eax, [bx]
> > integrated environment would report that 16-bit references are not
> > supported. With EBX instead of BX program reports run-time error 216.
> > 
> > I have tried using both DOS and Win32 targets. FPC 1.0.10, Win32.
> > 
> > Another Q: Is it necessary to save the segment registers' contents
> > when entering an assembler procedure?
> 
> FPC is 32 bit (flat memory modell) compiler so don't mess with 
> segment registers!
> 
> So:
>   procedure increment(var x: dword); assembler;
>      asm
>        mov ebx, x
>        mov eax, [ebx]     { Line with the problem }
>        inc eax
>        mov [ebx], eax
>      end;

In addition, please, note that ebx needs to be preserved in assembler
procedures in later FPC versions (although 1.0.10 is OK because of a different
default calling convention), so it would be better to either use indirect
addressing as mentioned in another message in this thread, or use a different
register (to avoid having to save ebx at the beginning and restore it at the
end) in order to ensure compatibility of your code with later FPC versions.

Tomas





More information about the fpc-pascal mailing list