home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vp21beta.zip / ARTLSRC.RAR / LONG32.PAS < prev    next >
Pascal/Delphi Source File  |  2000-08-15  |  3KB  |  95 lines

  1. //█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
  2. //█                                                       █
  3. //█      Virtual Pascal Runtime Library.  Version 2.1.    █
  4. //█      Unsigned 32-bit integer manipulation             █
  5. //█      ─────────────────────────────────────────────────█
  6. //█      Copyright (C) 19992000 vpascal.com               █
  7. //█                                                       █
  8. //▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  9.  
  10. {$S-,R-,Q-,I-,X+,T-,Cdecl-,OrgName-,AlignRec-,Use32+}
  11.  
  12. unit Long32;
  13.  
  14. // This unit is intended for use with Virtual Pascal, and provides
  15. // some very basic functions for dividing, multiplying and displaying
  16. // Longints/cardinals interpreted as 32-bit unsigned integers.
  17.  
  18. interface
  19.  
  20. type
  21.   PCardinal = ^Cardinal;
  22.  
  23. function udiv(Operand, Divisor: Cardinal): Cardinal;
  24. function umod(Operand, Divisor: Cardinal): Cardinal;
  25. function umul(Op1, Op2: Cardinal; HighLong: PCardinal): Cardinal;
  26. function udivmod(Operand, Divisor: Cardinal; var Modulus: Cardinal): Cardinal;
  27. function ulong2str(Value: Cardinal): ShortString;
  28.  
  29. implementation
  30.  
  31. // Unsigned long division
  32. function udiv(Operand, Divisor: Cardinal): Cardinal; {&frame-} {&uses none}
  33. asm
  34.       mov   eax, Operand
  35.       xor   edx, edx
  36.       div   dword ptr Divisor
  37. end;
  38.  
  39. // Unsigned long modulus
  40. function umod(Operand, Divisor: Cardinal): Cardinal; {&frame-} {&uses none}
  41. asm
  42.       mov   eax, Operand
  43.       xor   edx, edx
  44.       div   dword ptr Divisor
  45.       mov   eax, edx
  46. end;
  47.  
  48. // Unsigned long multiplication; optionally returns high DWord in HighLong
  49. function umul(Op1, Op2: Cardinal; HighLong: PCardinal): Cardinal; {&frame-} {&uses none}
  50. asm
  51.       mov   eax, Op1
  52.       mul   dword ptr Op2
  53.       mov   ecx,HighLong
  54.       jecxz @@1           // if HighLong <> nil, assign value
  55.       mov   [ecx],edx
  56.     @@1:
  57. end;
  58.  
  59. // Unsigned long division and modulus.  Returns Modulus in the Var parameter
  60. function udivmod(Operand, Divisor: Cardinal; var Modulus: Cardinal): Cardinal; {&frame-} {&uses none}
  61. asm
  62.       mov   eax, Operand
  63.       mov   ecx, Divisor
  64.       xor   edx, edx
  65.       div   ecx
  66.       mov   ecx,Modulus
  67.       mov   [ecx],edx
  68. end;
  69.  
  70. // Convert unsigned 32-bit integer to string format, like the Str() function
  71. function ulong2str(Value: Cardinal): ShortString; {&frame+} {&uses ebx,edi}
  72. asm
  73.       mov   eax, Value
  74.       mov   ebx, 0Ah
  75.       xor   ecx, ecx
  76.    @@1:
  77.       xor   edx, edx
  78.       div   ebx
  79.       add   dl, 30h        // convert to ascii
  80.       push  edx
  81.       inc   ecx
  82.       test  eax, eax
  83.       jne   @@1
  84.       mov   edi, @Result
  85.       mov   eax, ecx
  86.       stosb               // set the pascal string length byte
  87.     @@2:
  88.       dec   ecx
  89.       pop   eax
  90.       stosb               // now finish the string
  91.       jne   @@2
  92. end;
  93.  
  94. end.
  95.