Solved

asm functions and pointers

Posted on 1998-05-07
2
309 Views
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?
0
Comment
Question by:acerola
2 Comments
 
LVL 6

Accepted Solution

by:
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;
asm
  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 }
end;


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

begin
  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);
end.

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.
0
 

Expert Comment

by:snirad
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.}
PROGRAM TestPtr;

{  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.              }
CONST
  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;
ASM
        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!}
END;

VAR
  OriginalPtr, ReturnedPtr: Pointer;

BEGIN
{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!')
  ELSE
    WriteLn('SUCCESS: ClearPointer successfully place the value of NIL in OriginalPtr!');
  WriteLn('The value of ReturnedPtr is ',Seg(ReturnedPtr^),':',Ofs(ReturnedPtr^),'.');
END.

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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