home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / MISC / ZEN1_10.ZIP / LOGIMATH.SRC < prev    next >
Encoding:
Text File  |  1989-12-31  |  7.3 KB  |  387 lines

  1. \*
  2.  *   ZEN 1.10  Arithmetic and Logical operators
  3.  *     C 1990  by Martin Tracy
  4.  *             Last modified  1.1.90
  5.  *\
  6.  
  7.  0 CONSTANT 0      \ fast zero
  8.  1 CONSTANT 1      \ fast one
  9. -1 CONSTANT TRUE   \ fast Boolean true
  10.  2 CONSTANT CELL   \ address units per cell
  11.  
  12. \ Add n to n2
  13. CODE +  ( n n2 - n3) \ CORE
  14.         pop   ax
  15.         add   bx,ax
  16.         NEXT
  17. END-CODE
  18.  
  19. \ Subtract n2 from n
  20. CODE -  ( n n2 - n3) \ CORE
  21.         pop   ax
  22.         sub   bx,ax
  23.         neg   bx
  24.         NEXT
  25. END-CODE
  26.  
  27. \ Change sign of n
  28. CODE NEGATE ( n - n2) \ CORE
  29.         neg   bx
  30.         NEXT
  31. END-CODE
  32.  
  33. \ Absolute value of n
  34. CODE ABS ( n - +n) \ CORE
  35.         or    bx,bx
  36.         jns   ABS2
  37.         neg   bx
  38. ABS2:   NEXT
  39. END-CODE
  40.  
  41. \ Add n to value at addr
  42. CODE +! ( n addr) \ CORE
  43.         pop   ax
  44.         add   [bx],ax
  45.         pop   bx
  46.         NEXT
  47. END-CODE
  48.  
  49. \ Multiply u by u2
  50. CODE UM* ( u u2 - ud) \ CORE
  51.         pop   ax
  52.         mul   bx
  53.         push  ax
  54.         mov   bx,dx
  55.         NEXT
  56. END-CODE
  57.  
  58. \ Divide ud by u giving quotient u3 remainder u2
  59. CODE UM/MOD ( ud u - u2 u3) \ CORE
  60.         POP   dx
  61.         SUB   ax,ax
  62.         CMP   dx,bx
  63.         jae   UMO2
  64.         pop   ax
  65.         div   bx
  66.         push  dx
  67. UMO2:   mov   bx,ax
  68.         NEXT
  69. END-CODE
  70.  
  71. \ Signed mixed-precision multiply
  72. CODE M* ( n n2 - d) \ DOUBLE
  73.         xchg  ax,bx
  74.         pop   dx
  75.         imul  dx
  76.         push  ax
  77.         mov   bx,dx
  78.         NEXT
  79. END-CODE
  80.  
  81. \ Signed rounded-down mixed-precision divide.
  82. CODE M/MOD ( d n - rem quot) \ EXT CORE
  83.         pop   dx
  84.         pop   ax
  85.         or    bx,bx
  86.         jz    MsM1
  87.         idiv  bx
  88.         mov   bx,ax
  89.         push  dx
  90.         NEXT
  91. MsM1:   mov   dx,ax   ; divide by zero
  92.         mov   bx,0
  93.         push  dx
  94.         NEXT
  95. END-CODE
  96.  
  97. \ Signed multiply
  98. CODE *  ( n n2 - n3) \ CORE
  99.         pop   ax
  100.         imul  bx
  101.         mov   bx,ax
  102.         NEXT
  103. END-CODE
  104.  
  105. \*
  106. \ Signed mixed-precision multiply.
  107. : M* ( n n2 - d) \ DOUBLE
  108.    2DUP XOR >R  ABS SWAP ABS UM*  R> 0< IF NEGATE THEN ;
  109.  
  110. \ Signed rounded-down mixed-precision divide.
  111. : M/MOD ( d n - rem quot) \ EXT CORE
  112.    2DUP XOR >R  OVER >R  ABS >R DABS R> UM/MOD
  113.    SWAP R> 0< IF  NEGATE  THEN
  114.    SWAP R> 0< IF  NEGATE  THEN ;
  115.  
  116. \ Signed floored mixed-precision divide.
  117. : M/MOD ( d n - rem quot) \ EXT CORE
  118.    DUP >R  2DUP XOR >R  DUP >R  ABS >R DABS R> UM/MOD
  119.    SWAP R> 0< IF  NEGATE  THEN
  120.    SWAP R> 0< IF  NEGATE  OVER IF  R@ ROT -  SWAP 1-  THEN THEN
  121.    R> DROP ;
  122.  
  123. \ Signed multiply
  124. : * ( n n2 - n3) \ CORE
  125.    UM* DROP ;
  126. *\
  127.  
  128.  
  129. \ Signed rounded-down divide leaving quotient and remainder
  130. : /MOD ( n n2 - rem quot)   \ CORE
  131.    >R  DUP 0<  R> M/MOD ;
  132.  
  133. \ Signed rounded-down divide
  134. : / ( n n2 - quot) \ CORE
  135.    /MOD NIP ;
  136.  
  137. \ Signed rounded-down remainder
  138. : MOD ( n n2 - rem) \ CORE
  139.    /MOD DROP ;
  140.  
  141. \ Signed multiply n by n2 then signed divide by n3
  142. \ Division leaves quotient and remainder
  143. \ Intermediate product is 32 bits
  144. : */MOD ( n n2 n3 - rem quot) \ CORE
  145.    >R  M*  R> M/MOD ;
  146.  
  147. \ Signed multiply n by n2 then signed divide by n3
  148. \ Intermediate product is 32 bits
  149. : */ ( n n2 n3 - quot) \ CORE
  150.    >R  M*  R> M/MOD NIP ;
  151.  
  152. \ Add one to n
  153. CODE 1+ ( n - n2) \ CORE
  154.         inc   bx
  155.         NEXT
  156. END-CODE
  157.  
  158. \ Subtract one from n
  159. CODE 1- ( n - n2) \ CORE
  160.         dec   bx
  161.         NEXT
  162. END-CODE
  163.  
  164. \ Multiply n by two
  165. CODE 2* ( n - n2) \ CORE
  166.         shl   bx,1
  167.         NEXT
  168. END-CODE
  169.  
  170. \ Shift n right once arithmetically
  171. CODE 2/ ( n - n2) \ CORE
  172.         sar   bx,1
  173.         NEXT
  174. END-CODE
  175.  
  176.  
  177. \ Add byte address units to address a.
  178. CODE BYTE+ ( a - a2) \ CORE
  179.         inc   bx
  180.         NEXT
  181. END-CODE
  182.  
  183. \ Size in address units of n bytes.
  184. CODE BYTES ( n - n2) \ CORE
  185.         NEXT
  186. END-CODE
  187. IMMEDIATE
  188.  
  189. \ Return next aligned address after address a.
  190. CODE REALIGN ( a - a2) \ CORE
  191.         NEXT
  192. END-CODE
  193. IMMEDIATE
  194.  
  195. \ Add cell address units to address a.
  196. CODE CELL+ ( a - a2) \ CORE
  197.         inc   bx
  198.         inc   bx
  199.         NEXT
  200. END-CODE
  201.  
  202. \ Size in address units of n cells.
  203. CODE CELLS ( n - n2) \ CORE
  204.         shl   bx,1
  205.         NEXT
  206. END-CODE
  207.  
  208. \ Add two double numbers
  209. CODE D+ ( d d2 - d3) \ CORE
  210.         pop   ax
  211.         pop   dx
  212.         pop   cx
  213.         add   cx,ax
  214.         push  cx
  215.         adc   bx,dx
  216.         NEXT
  217. END-CODE
  218.  
  219. \ Subtract d2 from d1
  220. CODE D- ( d d2 - d3) \ DOUBLE
  221.         mov   dx,bx
  222.         pop   ax
  223.         pop   bx
  224.         pop   cx
  225.         sub   cx,ax
  226.         push  cx
  227.         sbb   bx,dx
  228.         NEXT
  229. END-CODE
  230.  
  231. \ Change sign of d
  232. CODE DNEGATE ( d - d2) \ DOUBLE
  233.         pop   ax
  234.         neg   ax
  235.         push  ax
  236.         adc   bx,0
  237.         neg   bx
  238.         NEXT
  239. END-CODE
  240.  
  241. \ Absolute value of d
  242. : DABS ( d - +d) \ DOUBLE
  243.    DUP 0< IF  DNEGATE  THEN ;
  244.  
  245. \ Extend n to d
  246. CODE S>D ( n - d) \ CORE
  247.         xchg  ax,bx
  248.         cwd
  249.         push  ax
  250.         xchg  bx,dx
  251.         NEXT
  252. END-CODE
  253.  
  254. \ Truncate d to n
  255. CODE D>S ( d - s) \ CORE
  256.         pop   bx
  257.         NEXT
  258. END-CODE
  259.  
  260. \ Reverse the bytes within a word.
  261. CODE >< ( u - u2)
  262.         xchg  bl,bh
  263.         NEXT
  264. END-CODE
  265.  
  266.  
  267. \ Bitwise AND n and n2
  268. CODE AND ( m m2 - m3) \ CORE
  269.         pop   ax
  270.         and   bx,ax
  271.         NEXT
  272. END-CODE
  273.  
  274. \ Bitwise OR n and n2
  275. CODE OR ( m m2 - m3) \ CORE
  276.         pop   ax
  277.         or    bx,ax
  278.         NEXT
  279. END-CODE
  280.  
  281. \ Bitwise XOR n and n2
  282. CODE XOR ( m m2 - m3) \ CORE
  283.         pop   ax
  284.         xor   bx,ax
  285.         NEXT
  286. END-CODE
  287.  
  288. \ Bitwise NOT of n
  289. CODE INVERT ( w - w2) \ CORE
  290.         not   bx
  291.         NEXT
  292. END-CODE
  293.  
  294. \ Forth-83 NOT
  295. CODE NOT ( w - w2)
  296.         not   bx
  297.         NEXT
  298. END-CODE
  299.  
  300. \ True if n equals zero
  301. CODE 0= ( n - f) \ CORE
  302.         or    bx,bx
  303.         mov   bx,TRUTH
  304.         jz    Ze2
  305.         inc   bx
  306. Ze2:    NEXT
  307. END-CODE
  308.  
  309. \ True if n is less than zero
  310. CODE 0< ( n - f) \ CORE
  311.         or    bx,bx
  312.         mov   bx,TRUTH
  313.         js    zl2
  314.         inc   bx
  315. zl2:    NEXT
  316. END-CODE
  317.  
  318. \ True if n is greater than zero
  319. : 0> ( n - f) \ CORE
  320.    0 > ;
  321.  
  322. \ True if n equals n2
  323. CODE =  ( n n2 - f) \ CORE
  324.         pop   ax
  325.         cmp   bx,ax
  326.         mov   bx,TRUTH
  327.         je    Eq2
  328.         inc   bx
  329. Eq2:    NEXT
  330. END-CODE
  331.  
  332. \ True if n is less than n2
  333. CODE <  ( n n2 - f) \ CORE
  334.         pop   ax
  335.         sub   ax,bx
  336.         mov   bx,TRUTH
  337.         jl    Lt2
  338.         inc   bx
  339. Lt2:    NEXT
  340. END-CODE
  341.  
  342. \ True if n is greater than n2
  343. : > ( n n2 - f) \ CORE
  344.    SWAP < ;
  345.  
  346. \ True if u is less than u2
  347. CODE U< ( u u2 - f) \ CORE
  348.         pop   ax
  349.         sub   ax,bx
  350.         mov   bx,TRUTH
  351.         jb    Ult2
  352.         inc   bx
  353. Ult2:   NEXT
  354. END-CODE
  355.  
  356. \ True if n <= u < n2  given circular comparison.
  357. : WITHIN ( u n n2 - f) \ EXT CORE
  358.    OVER - >R - R> U< ;
  359.  
  360. \ Maximum of two numbers
  361. : MAX ( n n2 - n3) \ CORE
  362.    2DUP < IF  SWAP  THEN  DROP ;
  363.  
  364. \ Minimum of two numbers
  365. : MIN ( n n2 - n3) \ CORE
  366.    2DUP < NOT IF  SWAP  THEN  DROP ;
  367.  
  368. \ True if d is less than d2
  369. : D< ( d d2 - f) \ CORE
  370.    ROT  2DUP = IF  2DROP U<  EXIT THEN
  371.    2SWAP 2DROP  > ;
  372.  
  373.  
  374. \ True and preserves arguments if n is greater than n2
  375. \ Used by  ?DO   Equivalent to  2DUP > IF  TRUE EXIT  THEN  > ;
  376. | CODE ?> ( n n2 - n n2 true | 0)
  377.         mov   di,sp
  378.         cmp   bx,ss:[di]
  379.         jl    Qg1
  380.         pop   bx
  381.         mov   bx,0
  382.         NEXT
  383. Qg1:    push  bx
  384.         mov   bx,TRUTH
  385.         NEXT
  386. END-CODE
  387.