Solved

asm functions and pointers

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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