//ÛßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßÛ
//Û                                                       Û
//Û      Virtual Pascal Runtime Library.  Version 2.1.    Û
//Û      Unsigned 32-bit integer manipulation             Û
//Û      ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÛ
//Û      Copyright (C) 19992000 vpascal.com               Û
//Û                                                       Û
//ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß

{$S-,R-,Q-,I-,X+,T-,Cdecl-,OrgName-,AlignRec-,Use32+}

unit Long32;

// This unit is intended for use with Virtual Pascal, and provides
// some very basic functions for dividing, multiplying and displaying
// Longints/cardinals interpreted as 32-bit unsigned integers.

interface

type
  PCardinal = ^Cardinal;

function udiv(Operand, Divisor: Cardinal): Cardinal;
function umod(Operand, Divisor: Cardinal): Cardinal;
function umul(Op1, Op2: Cardinal; HighLong: PCardinal): Cardinal;
function udivmod(Operand, Divisor: Cardinal; var Modulus: Cardinal): Cardinal;
function ulong2str(Value: Cardinal): ShortString;

implementation

// Unsigned long division
function udiv(Operand, Divisor: Cardinal): Cardinal; {&frame-} {&uses none}
asm
      mov   eax, Operand
      xor   edx, edx
      div   dword ptr Divisor
end;

// Unsigned long modulus
function umod(Operand, Divisor: Cardinal): Cardinal; {&frame-} {&uses none}
asm
      mov   eax, Operand
      xor   edx, edx
      div   dword ptr Divisor
      mov   eax, edx
end;

// Unsigned long multiplication; optionally returns high DWord in HighLong
function umul(Op1, Op2: Cardinal; HighLong: PCardinal): Cardinal; {&frame-} {&uses none}
asm
      mov   eax, Op1
      mul   dword ptr Op2
      mov   ecx,HighLong
      jecxz @@1           // if HighLong <> nil, assign value
      mov   [ecx],edx
    @@1:
end;

// Unsigned long division and modulus.  Returns Modulus in the Var parameter
function udivmod(Operand, Divisor: Cardinal; var Modulus: Cardinal): Cardinal; {&frame-} {&uses none}
asm
      mov   eax, Operand
      mov   ecx, Divisor
      xor   edx, edx
      div   ecx
      mov   ecx,Modulus
      mov   [ecx],edx
end;

// Convert unsigned 32-bit integer to string format, like the Str() function
function ulong2str(Value: Cardinal): ShortString; {&frame+} {&uses ebx,edi}
asm
      mov   eax, Value
      mov   ebx, 0Ah
      xor   ecx, ecx
   @@1:
      xor   edx, edx
      div   ebx
      add   dl, 30h        // convert to ascii
      push  edx
      inc   ecx
      test  eax, eax
      jne   @@1
      mov   edi, @Result
      mov   eax, ecx
      stosb               // set the pascal string length byte
    @@2:
      dec   ecx
      pop   eax
      stosb               // now finish the string
      jne   @@2
end;

end.
