home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PASCAL / NKTOOLS.ZIP / HEX.PAS < prev    next >
Pascal/Delphi Source File  |  1990-04-10  |  14KB  |  347 lines

  1. unit Hex;
  2. (*====================================================================*\
  3. || MODULE NAME:  Hex                                                  ||
  4. || DEPENDENCIES: System                                               ||
  5. || LAST MOD ON:  9004.10                                              ||
  6. || PROGRAMMER:   Naoto Kimura                                         ||
  7. ||                                                                    ||
  8. ||     This unit is used for printing hex values of numbers.          ||
  9. ||                                                                    ||
  10. || MODIFICATION HISTORY:                                              ||
  11. || 8901.07       Naoto Kimura                                         ||
  12. ||               Original release                                     ||
  13. || 9004.10       Naoto Kimura                                         ||
  14. ||               Rewrote same parts as inline statements to try to    ||
  15. ||               reduce size of the unit and to speed up routines.    ||
  16. \*====================================================================*)
  17.  
  18. {$R-}    {Range checking off}
  19. {$S+}    {Stack checking on}
  20. {$D-}    {Debug info off}
  21. {$I-}    {I/O checking off}
  22. {$N-}    {No numeric coprocessor}
  23.  
  24. interface
  25.  
  26. const
  27.     HEXCHARS    : packed array [0..15] of char = '0123456789ABCDEF';
  28.  
  29. (*--------------------------------------------------------------------*\
  30. | NAME: HexByte                                                        |
  31. |                                                                      |
  32. |     This function is returns the hexadecimal representation of an    |
  33. | integer value.                                                       |
  34. \*--------------------------------------------------------------------*)
  35. function HexByte( b : byte ) : string;
  36.  
  37. (*--------------------------------------------------------------------*\
  38. | NAME: HexShort                                                       |
  39. |                                                                      |
  40. |     This function is returns the hexadecimal representation of a     |
  41. | short integer value.                                                 |
  42. \*--------------------------------------------------------------------*)
  43. function HexShort( s : ShortInt ) : string;
  44.  
  45. (*--------------------------------------------------------------------*\
  46. | NAME: HexWord                                                        |
  47. |                                                                      |
  48. |     This function is returns the hexadecimal representation of a     |
  49. | word value.                                                          |
  50. \*--------------------------------------------------------------------*)
  51. function HexWord( w : word ) : string;
  52.  
  53. (*--------------------------------------------------------------------*\
  54. | NAME: HexInt                                                         |
  55. |                                                                      |
  56. |     This function is returns the hexadecimal representation of an    |
  57. | integer value.                                                       |
  58. \*--------------------------------------------------------------------*)
  59. function HexInt( i : integer ) : string;
  60.  
  61. (*--------------------------------------------------------------------*\
  62. | NAME: HexLong                                                        |
  63. |                                                                      |
  64. |     This function is returns the hexadecimal representation of a     |
  65. | long value.                                                          |
  66. \*--------------------------------------------------------------------*)
  67. function HexLong( l : longint ) : string;
  68.  
  69. (*--------------------------------------------------------------------*\
  70. | NAME: HexPtr                                                         |
  71. |                                                                      |
  72. |     This function is returns the hexadecimal representation of a     |
  73. | pointer.                                                             |
  74. \*--------------------------------------------------------------------*)
  75. function HexPtr( l : pointer ) : string;
  76.  
  77. implementation
  78.  
  79. (*--------------------------------------------------------------------*\
  80. | NAME: HexByte                                                        |
  81. |                                                                      |
  82. |     This function is returns the hexadecimal representation of an    |
  83. | integer value.                                                       |
  84. \*--------------------------------------------------------------------*)
  85. function HexByte( b : byte ) : string;
  86.     begin
  87.     inline(    $C4/$7E/$08/        { les   di,[Result]    }
  88.         $FC/            { cld            }
  89.         $B8/$02/$00/        { mov   ax,2        }
  90.         $AA/            { stosb            }
  91.         $33/$DB/        { xor   bx,bx        }
  92.         $8A/$5E/<B/        { mov   bl,[B]        }
  93.         $D0/$EB/        { shr   bl,1        }
  94.         $D0/$EB/        { shr   bl,1        }
  95.         $D0/$EB/        { shr   bl,1        }
  96.         $D0/$EB/        { shr   bl,1        }
  97.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  98.         $8A/$5E/<B/        { mov   bl,[B]        }
  99.         $83/$E3/$0F/        { and   bx,0fh        }
  100.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  101.         $AB)            { stosw            }
  102.     end;    (* HexByte *)
  103.  
  104. (*--------------------------------------------------------------------*\
  105. | NAME: HexShort                                                       |
  106. |                                                                      |
  107. |     This function is returns the hexadecimal representation of a     |
  108. | short integer value.                                                 |
  109. \*--------------------------------------------------------------------*)
  110. function HexShort( s : ShortInt ) : string;
  111.     begin
  112.     inline(    $C4/$7E/$08/        { les   di,[Result]    }
  113.         $FC/            { cld            }
  114.         $B8/$02/$00/        { mov   ax,2        }
  115.         $AA/            { stosb            }
  116.         $33/$DB/        { xor   bx,bx        }
  117.         $8A/$5E/<S/        { mov   bl,[B]        }
  118.         $D0/$EB/        { shr   bl,1        }
  119.         $D0/$EB/        { shr   bl,1        }
  120.         $D0/$EB/        { shr   bl,1        }
  121.         $D0/$EB/        { shr   bl,1        }
  122.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  123.         $8A/$5E/<S/        { mov   bl,[B]        }
  124.         $83/$E3/$0F/        { and   bx,0fh        }
  125.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  126.         $AB)            { stosw            }
  127.     end;    (* HexShort *)
  128.  
  129. (*--------------------------------------------------------------------*\
  130. | NAME: hexword                                                        |
  131. |                                                                      |
  132. |     This function is returns the hexadecimal representation of a     |
  133. | word value.                                                          |
  134. \*--------------------------------------------------------------------*)
  135. function HexWord( w : word ) : string;
  136.     begin                {; get return string location}
  137.     inline(    $C4/$7E/$08/        { les   di,[Result]    }
  138.                     {; forward string op    }
  139.         $FC/            { cld            }
  140.         $B8/$04/$00/        { mov   ax,4        }
  141.                     {; set result string length}
  142.         $AA/            { stosb            }
  143.         $8B/$56/<W/        { mov   dx,[W]        }
  144.         $8B/$CA/        { mov   cx,dx        }
  145.                     {; cx <-- low nibbles    }
  146.                     {; dx <-- low nibbles    }
  147.         $81/$E1/$0F0F/        { and   cx,0f0fh    }
  148.         $81/$E2/$F0F0/        { and   dx,0f0f0h    }
  149.         $D1/$EA/        { shr   dx,1        }
  150.         $D1/$EA/        { shr   dx,1        }
  151.         $D1/$EA/        { shr   dx,1        }
  152.         $D1/$EA/        { shr   dx,1        }
  153.         $33/$DB/        { xor   bx,bx        }
  154.         $8A/$DE/        { mov   bl,dh        }
  155.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  156.         $8A/$DD/        { mov   bl,ch        }
  157.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  158.         $AB/            { stosw            }
  159.         $8A/$DA/        { mov   bl,dl        }
  160.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  161.         $8A/$D9/        { mov   bl,cl        }
  162.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  163.         $AB);            { stosw            }
  164.     end;    (* HexWord *)
  165.  
  166. (*--------------------------------------------------------------------*\
  167. | NAME: HexInt                                                         |
  168. |                                                                      |
  169. |     This function is returns the hexadecimal representation of an    |
  170. | integer value.                                                       |
  171. \*--------------------------------------------------------------------*)
  172. function HexInt( i : integer ) : string;
  173.     begin
  174.     inline(    $C4/$7E/$08/        { les   di,[Result]    }
  175.                     {; forward string op    }
  176.         $FC/            { cld            }
  177.         $B8/$04/$00/        { mov   ax,4        }
  178.                     {; set result string length}
  179.         $AA/            { stosb            }
  180.         $8B/$56/<I/        { mov   dx,[W]        }
  181.         $8B/$CA/        { mov   cx,dx        }
  182.                     {; cx <-- low nibbles    }
  183.                     {; dx <-- low nibbles    }
  184.         $81/$E1/$0F0F/        { and   cx,0f0fh    }
  185.         $81/$E2/$F0F0/        { and   dx,0f0f0h    }
  186.         $D1/$EA/        { shr   dx,1        }
  187.         $D1/$EA/        { shr   dx,1        }
  188.         $D1/$EA/        { shr   dx,1        }
  189.         $D1/$EA/        { shr   dx,1        }
  190.         $33/$DB/        { xor   bx,bx        }
  191.         $8A/$DE/        { mov   bl,dh        }
  192.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  193.         $8A/$DD/        { mov   bl,ch        }
  194.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  195.         $AB/            { stosw            }
  196.         $8A/$DA/        { mov   bl,dl        }
  197.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  198.         $8A/$D9/        { mov   bl,cl        }
  199.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  200.         $AB);            { stosw            }
  201.     end;    (* HexInt *)
  202.  
  203. (*--------------------------------------------------------------------*\
  204. | NAME: HexLong                                                        |
  205. |                                                                      |
  206. |     This function is returns the hexadecimal representation of a     |
  207. | long value.                                                          |
  208. \*--------------------------------------------------------------------*)
  209. function HexLong( l : longint ) : string;
  210.     begin
  211.     inline(    $1E/            { push  ds        }
  212.                     {; fwd string op    }
  213.         $FC/            { cld            }
  214.         $C5/$76/<L/        { lds   si,[L]        }
  215.                     {; es:di <- &(result)    }
  216.         $C4/$7E/$0A/        { les   di,[Result]    }
  217.         $B8/$08/$00/        { mov   ax,8        }
  218.         $AA/            { stosb            }
  219.                 {;-------------------------------------}
  220.                 {; Hi word                   }
  221.                 {;-------------------------------------}
  222.         $8C/$D8/        { mov   ax,ds        }
  223.                     {; restore data seg reg    }
  224.         $1F/            { pop   ds        }
  225.         $92/            { xchg  dx,ax        }
  226.         $8B/$CA/        { mov   cx,dx        }
  227.                     {; cx <-- low nibbles    }
  228.                     {; dx <-- low nibbles    }
  229.         $81/$E1/$0F0F/        { and   cx,0f0fh    }
  230.         $81/$E2/$F0F0/        { and   dx,0f0f0h    }
  231.         $D1/$EA/        { shr   dx,1        }
  232.         $D1/$EA/        { shr   dx,1        }
  233.         $D1/$EA/        { shr   dx,1        }
  234.         $D1/$EA/        { shr   dx,1        }
  235.         $33/$DB/        { xor   bx,bx        }
  236.         $8A/$DE/        { mov   bl,dh        }
  237.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  238.         $8A/$DD/        { mov   bl,ch        }
  239.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  240.         $AB/            { stosw            }
  241.         $8A/$DA/        { mov   bl,dl        }
  242.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  243.         $8A/$D9/        { mov   bl,cl        }
  244.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  245.         $AB/            { stosw            }
  246.                 {;-------------------------------------}
  247.                 {; Lo word                   }
  248.                 {;-------------------------------------}
  249.         $8B/$D6/        { mov   dx,si        }
  250.         $8B/$CA/        { mov   cx,dx        }
  251.                     {; cx <-- low nibbles    }
  252.                     {; dx <-- low nibbles    }
  253.         $81/$E1/$0F0F/        { and   cx,0f0fh    }
  254.         $81/$E2/$F0F0/        { and   dx,0f0f0h    }
  255.         $D1/$EA/        { shr   dx,1        }
  256.         $D1/$EA/        { shr   dx,1        }
  257.         $D1/$EA/        { shr   dx,1        }
  258.         $D1/$EA/        { shr   dx,1        }
  259.         $33/$DB/        { xor   bx,bx        }
  260.         $8A/$DE/        { mov   bl,dh        }
  261.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  262.         $8A/$DD/        { mov   bl,ch        }
  263.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  264.         $AB/            { stosw            }
  265.         $8A/$DA/        { mov   bl,dl        }
  266.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  267.         $8A/$D9/        { mov   bl,cl        }
  268.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  269.         $AB)            { stosw            }
  270.     end;    (* HexLong *)
  271.  
  272. (*--------------------------------------------------------------------*\
  273. | NAME: HexPtr                                                         |
  274. |                                                                      |
  275. |     This function is returns the hexadecimal representation of a     |
  276. | pointer.                                                             |
  277. \*--------------------------------------------------------------------*)
  278. function HexPtr( l : pointer ) : string;
  279.     begin
  280.     inline(    $1E/            { push  ds        }
  281.                     {; fwd string op    }
  282.         $FC/            { cld            }
  283.         $C5/$76/<L/        { lds   si,[L]        }
  284.                     {; es:di <- &(result)    }
  285.         $C4/$7E/$0A/        { les   di,[Result]    }
  286.         $B8/$09/$00/        { mov   ax,8        }
  287.         $AA/            { stosb            }
  288.                 {;-------------------------------------}
  289.                 {; Hi word                   }
  290.                 {;-------------------------------------}
  291.         $8C/$D8/        { mov   ax,ds        }
  292.                     {; restore data seg reg    }
  293.         $1F/            { pop   ds        }
  294.         $92/            { xchg  dx,ax        }
  295.         $8B/$CA/        { mov   cx,dx        }
  296.                     {; cx <-- low nibbles    }
  297.                     {; dx <-- low nibbles    }
  298.         $81/$E1/$0F0F/        { and   cx,0f0fh    }
  299.         $81/$E2/$F0F0/        { and   dx,0f0f0h    }
  300.         $D1/$EA/        { shr   dx,1        }
  301.         $D1/$EA/        { shr   dx,1        }
  302.         $D1/$EA/        { shr   dx,1        }
  303.         $D1/$EA/        { shr   dx,1        }
  304.         $33/$DB/        { xor   bx,bx        }
  305.         $8A/$DE/        { mov   bl,dh        }
  306.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  307.         $8A/$DD/        { mov   bl,ch        }
  308.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  309.         $AB/            { stosw            }
  310.         $8A/$DA/        { mov   bl,dl        }
  311.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  312.         $8A/$D9/        { mov   bl,cl        }
  313.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  314.         $AB/            { stosw            }
  315.                 {;-------------------------------------}
  316.                 {; colon separator               }
  317.                 {;-------------------------------------}
  318.         $B0/$3A/        { mov   al,':'        }
  319.         $AA/            { stosb            }
  320.                 {;-------------------------------------}
  321.                 {; Lo word                   }
  322.                 {;-------------------------------------}
  323.         $8B/$D6/        { mov   dx,si        }
  324.         $8B/$CA/        { mov   cx,dx        }
  325.                     {; cx <-- low nibbles    }
  326.                     {; dx <-- low nibbles    }
  327.         $81/$E1/$0F0F/        { and   cx,0f0fh    }
  328.         $81/$E2/$F0F0/        { and   dx,0f0f0h    }
  329.         $D1/$EA/        { shr   dx,1        }
  330.         $D1/$EA/        { shr   dx,1        }
  331.         $D1/$EA/        { shr   dx,1        }
  332.         $D1/$EA/        { shr   dx,1        }
  333.         $33/$DB/        { xor   bx,bx        }
  334.         $8A/$DE/        { mov   bl,dh        }
  335.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  336.         $8A/$DD/        { mov   bl,ch        }
  337.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  338.         $AB/            { stosw            }
  339.         $8A/$DA/        { mov   bl,dl        }
  340.         $8A/$87/HexChars/    { mov   al,[HexChars+bx]}
  341.         $8A/$D9/        { mov   bl,cl        }
  342.         $8A/$A7/HexChars/    { mov   ah,[HexChars+bx]}
  343.         $AB)            { stosw            }
  344.     end;    (* HexPtr *)
  345.  
  346. end.
  347.