Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-08

Var parameter passing to asm procedure

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Ivan Stamenkovic

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?

Thanks,
Ivan

Reply by email

Florian Klaempfl
to Ivan Stamenkovic
Ivan Stamenkovic wrote 41 lines
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;

Reply by email

Nelson M. Sicuro
to Ivan Stamenkovic: I have a problem when trying to pass some variable parameters to a procedure writen using…

The asm block can be this way:

procedure increment(var x: dword); assembler;
asm
   inc [eax]
end;

You don't need to worry about segment registers, but don't mess with them! I assume that the parameter x is passed on the eax register as Delphi does. Please correct me if I'm wrong...

Show quoted text 52 lines
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? Thanks, Ivan _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Marco van de Voort
to Nelson M. Sicuro
Show quoted text 10 lines
The asm block can be this way: procedure increment(var x: dword); assembler; asm inc [eax] end; You don't need to worry about segment registers, but don't mess with them! I assume that the parameter x is passed on the eax register as Delphi does. Please correct me if I'm wrong...

You are wrong.

He is using 1.0.x, which doesn't have register parameters. That is 1.9.2 or 1.9.4+

Reply by email

Nelson M. Sicuro
to Marco van de Voort

Ok, then this can be this way:

procedure increment(var x: dword); assembler;
asm
mov eax, x
inc [eax]
end;

Is this correct? If the compiler uses registers, the first line is translated to a 'mov eax, eax', otherwise it is something like 'mov eax, [esp-4]'.

Regards,

Nelson

Show quoted text 24 lines
> The asm block can be this way: > > procedure increment(var x: dword); assembler; > asm > inc [eax] > end; > > You don't need to worry about segment registers, but don't mess with > them! > I assume that the parameter x is passed on the eax register as Delphi > does. > Please correct me if I'm wrong... You are wrong. He is using 1.0.x, which doesn't have register parameters. That is 1.9.2 or 1.9.4+ _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Marco van de Voort
to Nelson M. Sicuro
Show quoted text 11 lines
Ok, then this can be this way: procedure increment(var x: dword); assembler; asm mov eax, x inc [eax] end; Is this correct? If the compiler uses registers, the first line is translated to a 'mov eax, eax', otherwise it is something like 'mov eax, [esp-4]'.

Afaik, it is correct, unless there is something with inc [eax] (e.g. not working on older ones)

Reply by email

Tomas Hajny
to Marco van de Voort

On Fri, 6 Aug 2004 11:18:01 +0200 (CEST), Marco van de Voort wrote

Show quoted text 14 lines
> Ok, then this can be this way: > > procedure increment(var x: dword); assembler; > asm > mov eax, x > inc [eax] > end; > > Is this correct? If the compiler uses registers, the first line is > translated to a 'mov eax, eax', otherwise it is something like 'mov eax, > [esp-4]'. Afaik, it is correct, unless there is something with inc [eax] (e.g. not working on older ones)

Almost OK - you need to specify the operand size explicitely ("inc dword ptr [eax]" in Intel syntax, or "incl (%eax)" in AT&T syntax).

Tomas

Reply by email

Tomas Hajny
to Florian Klaempfl: FPC is 32 bit (flat memory modell) compiler so don't mess with segment registers! So:…

On Thu, 05 Aug 2004 16:56:28 +0200, Florian Klaempfl wrote

Show quoted text 55 lines
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

Reply by email