asm functions and pointers

Posted on 1998-05-07
Last Modified: 2013-11-22
i'm learning to make asm functions inside pascal 7 and
i've figured out that the return value for the function
is the value in AX. but that is at most a word. how do
i send the function a pointer as parameter and read/modify
its value?
Question by:acerola

Accepted Solution

gete earned 20 total points
ID: 1216980
Every parameter passed by reference (var) is actually give its variable address (pointer). In the other hand, parameter passed by value is copied to a local variable created by the func/proc in the beginning of call, except for string passed by value which is actually give its temporary variable address (pointer), too.
Parameter passed by value is stored in the SS. To retrieve its value, if we use string commands (movs, stos, lods), we have to override the DS with SEGSS prefix opcode. See the example below:

procedure asmTest(var i: integer; ival: integer;
                  var l: longint; lval: longint;
                  var p: pointer; pval: pointer;
                  var s: string; sval: string); assembler;
  push ds         { save ds value }

  mov ax, ival    { ival: integer value = 2 byte }
  les di, i       { i: pointer = 4 byte }
  stosw           { mov es:[di], ax }

  lea si, lval    { lval: longint value = 4 byte }
  les di, l       { l: pointer = 4 byte }
  SEGSS lodsw     { mov ax, ss:[si] }
  stosw           { mov es:[di], ax }
  SEGSS lodsw     { mov ax, ss:[si] }
  stosw           { mov es:[di], ax }

  lea si, pval    { pval: pointer value = 4 byte }
  les di, p       { p: pointer = 4 byte }
  SEGSS lodsw     { mov ax, ss:[si] }
  stosw           { mov es:[di], ax }
  SEGSS lodsw     { mov ax, ss:[si] }
  stosw           { mov es:[di], ax }

  lds si, sval    { sval: pointer = 4 byte }
  les di, s       { s: pointer = 4 byte }
  movsb           { al := s[0] := sval[0] (string length) }
  mov cl, al      { cl := al }
  xor ch, ch      { ch := 0 }
  rep movsb       { copy sval[1..length] to s[1..length] }

  pop ds          { restore ds value }

{ global variables }
  Gi: integer;
  Gl: longint;
  Gp: pointer;
  Gs: string;

  asmTest(Gi, 12345, Gl, 1234567890, Gp, Addr(Gi),
          Gs, 'This is only a test.');
  Writeln('Gi = ', Gi);
  Writeln('Gl = ', Gl);
  Writeln('Gp = Addr(Gi) = ', Seg(Gp), ':', Ofs(Gp));
  Writeln('     Addr(Gi) = ', Seg(Gi), ':', Ofs(Gp));
  Writeln('Gs = ', Gs);

As you can see, however, string passed by value is also actually give its temporary variable address. The complex data structure like array and record passed by value, have the others behaviour, except for record containing string (with no [..]).
Hope this will help you.

Expert Comment

ID: 1216981
Hi acerola, you said that you have learned how to return values in the AX but at most you can only return a word.  This isn't fully true.  For larger DWord size object such as pointers and LongInts you can use the DX:AX pair.  The following program should give you a good example of how to do this along with a few other tricks you may find helpful.

{  This Program is a simple demonstration of how to use Pointers from within}
{  assembler functions in Pascal.}

{  Even though NIL in Borland Pascal should always be the value of 0:0 it    }
{  is good to make your code as compatibly with other versions as possible.  }
{  Defining constants for the segment and offset values of NIL and assigning }
{  them formulas will both make it easier to change you code if you need to  }
{  and increase the chances of not needing to change it at all.              }
  kNilOfs = LongInt(NIL) AND $0000FFFF;
  kNilSeg = LongInt(NIL) SHR 16;

{  This function returns the value of P and then clears P by assigning it NIL}
FUNCTION ClearPointer(VAR P: Pointer): Pointer; ASSEMBLER;
        LES     DI,P           {Get the Pointers address}
        MOV     AX,ES:[DI]     {Load it’s offset}
        MOV     DX,ES:[DI+2]   {Load it’s segment}
  {DX:AX now has P loaded and is ready to be returned}
        MOV     CX,kNilOfs     {Load CX with the offset of Nil}
        MOV     ES:[DI],CX     {Store the offset}
        MOV     CX,kNilSeg     {Load CX with the segment of Nil}
        MOV     ES:[DI+2],CX   {Store the segment}
  {We're done!}

  OriginalPtr, ReturnedPtr: Pointer;

{This point on is an example of how to use ClearPointer}
  OriginalPtr:= Ptr(1234, 5678);
  ReturnedPtr := ClearPointer(OriginalPtr);
  IF OriginalPtr<>NIL THEN
    WriteLn('ERROR: ClearPointer failed to place the value of NIL in OriginalPtr!')
    WriteLn('SUCCESS: ClearPointer successfully place the value of NIL in OriginalPtr!');
  WriteLn('The value of ReturnedPtr is ',Seg(ReturnedPtr^),':',Ofs(ReturnedPtr^),'.');


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 94
proper way to parse url in delphi 2 165
code issue 8 100
Tembedded WB animatid gifs not animated on some pcs 2 73
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now