Solved

asm functions and pointers

Posted on 1998-05-07
2
306 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

9 Experts available now in Live!

Get 1:1 Help Now