home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / bix / longint.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1986-08-04  |  5.3 KB  |  234 lines

  1. {assorted routines for pseudo-LONGINT types in Turbo}
  2. Program tmul;
  3.  
  4. const h:array[0..15] of char =
  5. ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  6.  
  7. type long = ^byte;
  8.      both = record case integer of
  9.             0:(L:long);
  10.             1:(lsw,msw:integer)
  11.             end;
  12.             anystring = string[255];
  13.  
  14. var i,j:integer;l:long;
  15.  
  16. function uprodii(a,b:integer):long;
  17. {returns long product of two unsigned integers}
  18. var r:long;
  19. begin
  20.  Inline($8B/$46/<a/   {mov  ax,a}
  21.         $F7/$66/<b/   {mul  b   }
  22.         $89/$46/<r/   {mov  r,ax}
  23.         $89/$56/<r+2);{mov  r+2,dx}
  24.  uprodii:=r
  25. end;
  26.  
  27. function sprodii(a,b:integer):long;
  28. {returns long product of two signed integers}
  29. var r:long;
  30. begin
  31.  Inline($8B/$46/<a/   {mov  ax,a}
  32.         $F7/$6E/<b/   {imul b   }
  33.         $89/$46/<r/   {mov  r,ax}
  34.         $89/$56/<r+2);{mov  r+2,dx}
  35.  sprodii:=r
  36. end;
  37.  
  38. function twos(l:long):long;
  39. {returns 2's complement of long, i.e. reverse the sign}
  40. var wrk:both absolute l;
  41.     hw,lw:integer;
  42. begin
  43.  hw:=wrk.msw xor $ffff;
  44.  lw:=wrk.lsw xor $ffff;
  45.  if lw=$ffff then
  46.  twos:=ptr(succ(hw),0) else
  47.  twos:=ptr(hw,succ(lw))
  48. end;
  49.  
  50. function lneg(l:long):boolean;
  51. {returns true if long is negative}
  52. begin lneg:=seg(l^)<0 end;
  53.  
  54. function squo(l:long;i:integer):integer;
  55. {returns signed quotient of l div i}
  56. var q:integer;
  57.     neg:boolean;
  58. begin
  59.  neg:=i<0;
  60.  if neg then i:=-i;
  61.  if lneg(l) then
  62.   begin
  63.    neg:=not neg;
  64.    l:=twos(l)
  65.   end;
  66.  Inline($8B/$46/<l/   {mov  ax,lsw of l}
  67.         $8B/$56/<l+2/ {mov  dx,msw of l}
  68.         $F7/$76/<i/   {idiv i}
  69.         $89/$46/<q);  {mov  q,ax}
  70.  if neg then squo:=-q else squo:=q
  71. end;
  72.  
  73. function uquo(l:long;i:integer):integer;
  74. {returns unsigned quotient of l div i}
  75. var q:integer;
  76. begin
  77.  Inline($8B/$46/<l/   {mov  ax,lsw of l}
  78.         $8B/$56/<l+2/ {mov  dx,msw of l}
  79.         $F7/$7E/<i/   {div  i}
  80.         $89/$46/<q);  {mov  q,ax}
  81.  uquo:=q
  82. end;
  83.  
  84. function srem(l:long;i:integer):integer;
  85. {returns signed remainder of l div i}
  86. var r:integer;
  87.     neg:boolean;
  88. begin
  89.  neg:=i<0;
  90.  if neg then i:=-i;
  91.  if lneg(l) then
  92.   begin
  93.    neg:=not neg;
  94.    l:=twos(l)
  95.   end;
  96.  Inline($8B/$46/<l/   {mov  ax,lsw of l}
  97.         $8B/$56/<l+2/ {mov  dx,msw of l}
  98.         $F7/$76/<i/   {idiv i}
  99.         $89/$56/<r);  {mov  r,dx}
  100.  if neg then srem:=-r else srem:=r
  101. end;
  102.  
  103. function urem(l:long;i:integer):integer;
  104. {returns unsigned remainder of l div i}
  105. var r:integer;
  106. begin
  107.  Inline($8B/$46/<l/   {mov  ax,lsw of l}
  108.         $8B/$56/<l+2/ {mov  dx,msw of l}
  109.         $F7/$7E/<i/   {div  i}
  110.         $89/$56/<r);  {mov  r,dx}
  111.  urem:=r
  112. end;
  113.  
  114. function ssumii(a,b:integer):long;
  115. {returns long sum of two signed integers}
  116. var i,j:integer;
  117. begin
  118.  i:=a+b;
  119.  if (a and $8000) = (b and $8000) then
  120.  j:=-(a shr 15) else
  121.  j:=-(i shr 15);
  122.  ssumii:=ptr(j,i)
  123. end;
  124.  
  125. function usumii(a,b:integer):long;
  126. {returns long sum of two unsigned integers}
  127. var i,j:integer;
  128. begin
  129.  i:=a+b;
  130.  j:=hi(a)+hi(b);
  131.  if j<255 then j:=0 else
  132.  if j>255 then j:=1 else
  133.  j:=hi((lo(a)+lo(b)));
  134.  usumii:=ptr(j,i)
  135. end;
  136.  
  137. function hiword(l:long):integer;
  138. {returns most sig word of 2-word long as integer}
  139. begin hiword:=seg(l^) end;
  140.  
  141. function loword(l:long):integer;
  142. {returns least sig word of 2-word long as integer}
  143. begin loword:=ofs(l^) end;
  144.  
  145. function makel(h,l:integer):long;
  146. {given most & least sig words (h,l), returns 2-word long}
  147. begin makel:=ptr(h,l) end;
  148.  
  149. function prodll(one,two:long):long;
  150. {returns the long product of two longs}
  151. var x,y,z:both;
  152.     a:both absolute one;
  153.     b:both absolute two;
  154.     neg:boolean;
  155. begin
  156.  neg:=lneg(a.l);
  157.  if neg then a.l:=twos(a.l);
  158.  if lneg(b.l) then
  159.   begin
  160.    neg:=not neg;
  161.    b.l:=twos(b.l)
  162.   end;
  163.  x.l:=uprodii(a.lsw,b.lsw);
  164.  y.l:=uprodii(a.lsw,b.msw);
  165.  z.l:=uprodii(b.lsw,a.msw);
  166.  z.l:=usumii(y.lsw,z.lsw);
  167.  z.l:=usumii(z.lsw,x.msw);
  168.  if neg then
  169.  prodll:=twos(ptr(z.lsw,x.lsw))
  170.  else
  171.  prodll:=ptr(z.lsw,x.lsw)
  172. end;
  173.  
  174. function i2l(i:integer):long;
  175. {returns signed integer as long, extends the sign}
  176. begin if i<0 then i2l:=ptr(-1,i) else i2l:=ptr(0,i) end;
  177.  
  178. function l2i(l:long):integer;
  179. {returns 'trunc' of long, invalid if long>32767 or <-32768}
  180. begin l2i:=ofs(l^) end;
  181.  
  182. function i2h(i:integer):anystring;
  183. {returns 4 byte string of hex chars for integer}
  184. begin
  185. i2h:=h[hi(i) shr 4]+h[hi(i) and $f]+h[lo(i) shr 4]+h[i and $f]
  186. end;
  187.  
  188. function l2r(l:long):real;
  189. {converts long to real}
  190. var r:real;
  191.     neg:boolean;
  192. begin
  193. neg:=lneg(l);
  194. if neg then l:=twos(l);
  195. r := 65536.0 * Hiword(l) + Loword(l);
  196. if loword(l)<0 then r:=r+65536.0;
  197. if neg then r:=-r;
  198. l2r:=r;
  199. end;
  200.  
  201. Type longstring=string[11];
  202.  
  203. Function L2S(L:Long):LongString;
  204. {converts long to string for printing}
  205. var s:LongString;
  206. begin
  207.  STR(L2R(L):11:0,S);
  208.  While S[1]=' ' do delete(s,1,1); {remove leading blanks}
  209.  L2S:=S
  210. end;
  211.  
  212. function l2h(l:long):anystring;
  213. {returns 8 byte string of hex for long}
  214. begin
  215. l2h:=i2h(hiword(l))+i2h(loword(l))
  216. end;
  217.  
  218. begin
  219. repeat
  220. write('#1...');readln(i);
  221. writeln('#1=',i2h(i));
  222. write('#2...');readln(j);
  223. writeln('#2=',i2h(j));
  224. l:=uprodii(i,j);
  225. writeln('prod of 2 unsigned ints = ',l2h(l));
  226. l:=sprodii(i,j);
  227. writeln('prod of 2 signed ints = ',l2h(l));
  228. l:=prodll(i2l(i),i2l(j));
  229. writeln('prod of 2 longs = ',l2h(l));
  230. if i<>0 then if j<>0 then
  231. writeln('the original 2 numbers were ',squo(l,j),' and ',squo(l,i));
  232. until i+j=0;
  233. end.
  234.