Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

asm functions and pointers

Posted on 1998-05-07
2
Medium Priority
?
322 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 40 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

885 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