Board index » delphi » Inline Assembly

Inline Assembly

I need to implement a function that calls interrupt 21, function 5C to
lock a region of a file.  The sample that I have goes something like
this:

mov bx, FileHandle

mov cx, HIoffset
mov dx, LOoffset

mov si, HIlength
mov di, LOlength

mov al, LockFlag
mov ah, 5Ch
int 21H

jc ErrorHandler

The FileHandle is the return from an OpenFile call, the LockFlag will
be 1 to lock the file, and 0 to unlock the file.  Here are the
problems:

1) How to get the high and low bytes for the offset and length?
Offset will be a LongInt, and length will be an Integer.

2) The function needs to return True if the file lock/unlock process
was successful.  I guess I can check the value of ErrorHandler, but
this line won't compile as it's listed above.

The environment is Delphi 1.02, and the file in question is a very
large file created by a DOS based program.  In C, I used the lock and
unlock functions, but I've been unable to find a Pascal equivalent.

Help?!

 

Re:Inline Assembly


Quote
Brian Vivian wrote:

> I need to implement a function that calls interrupt 21, function 5C to
> lock a region of a file.  The sample that I have goes something like
> this:

> mov bx, FileHandle

> mov cx, HIoffset
> mov dx, LOoffset

> mov si, HIlength
> mov di, LOlength

> mov al, LockFlag
> mov ah, 5Ch
> int 21H

> jc ErrorHandler

> The FileHandle is the return from an OpenFile call, the LockFlag will
> be 1 to lock the file, and 0 to unlock the file.  Here are the
> problems:

> 1) How to get the high and low bytes for the offset and length?
> Offset will be a LongInt, and length will be an Integer.

> 2) The function needs to return True if the file lock/unlock process
> was successful.  I guess I can check the value of ErrorHandler, but
> this line won't compile as it's listed above.

> The environment is Delphi 1.02, and the file in question is a very
> large file created by a DOS based program.  In C, I used the lock and
> unlock functions, but I've been unable to find a Pascal equivalent.

> Help?!

Note:  share.exe must be loaded (under Win 3.1) to use f-locking.
       Checking this under DOS multiplex interrupt doesn't work
       well under Windows, so using undocumented but presumably
       more safe method  -  simply try using the [censored] thing.

--------------------------------------------
const
  std_input     = 0;

function ShareIsLoaded: boolean;
var
  err_info      : err_info_type;
begin
FLock( std_input, 0, 0, true );
GetErrorInfo( err_info );
ShareIsLoaded := (err_info.error_code <> err_invalid_function_number);
end;    { ShareIsLoaded }

procedure GetErrorInfo( var info: err_info_type );
var
  regs          : tRegisters;
begin
FillChar( regs, SizeOf( regs ), 0 );
with regs, info do
  begin
  ah := $59;    bx := 0;
  MsDos( regs );
  error_code := ax;
  error_class := bh;
  suggested_action := bl;
  locus := ch;
  end;
end;    { GetErrorInfo }

type
  long_wordrec          = record lo, hi: word end;

function FLock( f               : hFile;
                pos, size       : longint;
                set_lock        : boolean ): boolean;
var
  regs          : tRegisters;
begin
FillChar( regs, SizeOf( regs ), 0 );
with regs do
  begin
  if set_lock then ax := $5C00 else ax := $5C01;
  bx := word(f);
  cx := long_wordrec(pos).hi;
  dx := long_wordrec(pos).lo;
  si := long_wordrec(size).hi;
  di := long_wordrec(size).lo;
  MsDos( regs );
  FLock := (flags and fCarry = 0);
  end;
end;    { FLock }
--------------------------------------------------

Hope this helps (it's for BP 7.0)

- Alf

Re:Inline Assembly


Quote
In article <4hiq9r$...@nnrp1.news.primenet.com> bviv...@primenet.com (Brian Vivian) writes:
>I need to implement a function that calls interrupt 21, function 5C to
>lock a region of a file.  The sample that I have goes something like
>this:
>mov bx, FileHandle
>mov cx, HIoffset
>mov dx, LOoffset
>mov si, HIlength
>mov di, LOlength
>mov al, LockFlag
>mov ah, 5Ch
>int 21H
>jc ErrorHandler
>The FileHandle is the return from an OpenFile call, the LockFlag will
>be 1 to lock the file, and 0 to unlock the file.  Here are the
>problems:
>1) How to get the high and low bytes for the offset and length?
>Offset will be a LongInt, and length will be an Integer.
>2) The function needs to return True if the file lock/unlock process
>was successful.  I guess I can check the value of ErrorHandler, but
>this line won't compile as it's listed above.

function Lock (Handle: word; Length: integer; Offset: longint): boolean;
                                                                  assembler;
asm
 mov bx, Handle
 mov cx, word ptr ss:[Offset]
 mov dx, word ptr ss:[Offset][2]
 xor si, si
 mov di, Length
 mov ah, 5Ch
 int 21h
 mov al, false
 jc @1
 inc al
@1:
end;

Hope it's OK.

                                    Tomas Hajny, XHaj...@vse.cz on Internet

Re:Inline Assembly


"Alf P. Steinbach" <al...@telepost.no> wrote:

Quote
>Note:  share.exe must be loaded (under Win 3.1) to use f-locking.
>       Checking this under DOS multiplex interrupt doesn't work
>       well under Windows, so using undocumented but presumably
>       more safe method  -  simply try using the [censored] thing.
>--------------------------------------------
>const
>  std_input = 0;
>function ShareIsLoaded: boolean;
>var
>  err_info      : err_info_type;
>begin
>FLock( std_input, 0, 0, true );
>GetErrorInfo( err_info );
>ShareIsLoaded := (err_info.error_code <> err_invalid_function_number);
>end;    { ShareIsLoaded }
>procedure GetErrorInfo( var info: err_info_type );
>var
>  regs          : tRegisters;
>begin
>FillChar( regs, SizeOf( regs ), 0 );
>with regs, info do
>  begin
>  ah := $59;    bx := 0;
>  MsDos( regs );
>  error_code := ax;
>  error_class := bh;
>  suggested_action := bl;
>  locus := ch;
>  end;
>end;    { GetErrorInfo }
>type
>  long_wordrec          = record lo, hi: word end;
>function FLock( f               : hFile;
>                pos, size       : longint;
>                set_lock        : boolean ): boolean;
>var
>  regs          : tRegisters;
>begin
>FillChar( regs, SizeOf( regs ), 0 );
>with regs do
>  begin
>  if set_lock then ax := $5C00 else ax := $5C01;
>  bx := word(f);
>  cx := long_wordrec(pos).hi;
>  dx := long_wordrec(pos).lo;
>  si := long_wordrec(size).hi;
>  di := long_wordrec(size).lo;
>  MsDos( regs );
>  FLock := (flags and fCarry = 0);
>  end;
>end;    { FLock }
>--------------------------------------------------
>Hope this helps (it's for BP 7.0)

Massive thanks for the response.  I'll play with it tonight and see if
I can get everything working in Delphi.

Other Threads