home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzCE2_src.ZIP / FrotzCE / Frotz / MATH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-14  |  3.6 KB  |  248 lines

  1. /*
  2.  * math.c
  3.  *
  4.  * Arithmetic, compare and logical opcodes
  5.  *
  6.  */
  7.  
  8. #include "frotz.h"
  9.  
  10. /*
  11.  * z_add, 16bit addition.
  12.  *
  13.  *    zargs[0] = first value
  14.  *    zargs[1] = second value
  15.  *
  16.  */
  17.  
  18. void z_add (void)
  19. {
  20.  
  21.     store (ZWORD ((short) zargs[0] + (short) zargs[1]));
  22.  
  23. }/* z_add */
  24.  
  25. /*
  26.  * z_and, bitwise AND operation.
  27.  *
  28.  *    zargs[0] = first value
  29.  *    zargs[1] = second value
  30.  *
  31.  */
  32.  
  33. void z_and (void)
  34. {
  35.  
  36.     store (ZWORD (zargs[0] & zargs[1]));
  37.  
  38. }/* z_and */
  39.  
  40. /*
  41.  * z_art_shift, arithmetic SHIFT operation.
  42.  *
  43.  *    zargs[0] = value
  44.  *    zargs[1] = #positions to shift left (positive) or right
  45.  *
  46.  */
  47.  
  48. void z_art_shift (void)
  49. {
  50.  
  51.     if ((short) zargs[1] > 0)
  52.     store (ZWORD ((short) zargs[0] << (short) zargs[1]));
  53.     else
  54.     store (ZWORD ((short) zargs[0] >> - (short) zargs[1]));
  55.  
  56. }/* z_art_shift */
  57.  
  58. /*
  59.  * z_div, signed 16bit division.
  60.  *
  61.  *    zargs[0] = first value
  62.  *    zargs[1] = second value
  63.  *
  64.  */
  65.  
  66. void z_div (void)
  67. {
  68.  
  69.     if (zargs[1] == 0)
  70.     runtime_error ("Division by zero");
  71.  
  72.     store (ZWORD ((short) zargs[0] / (short) zargs[1]));
  73.  
  74. }/* z_div */
  75.  
  76. /*
  77.  * z_je, branch if the first value equals any of the following.
  78.  *
  79.  *    zargs[0] = first value
  80.  *    zargs[1] = second value (optional)
  81.  *    ...
  82.  *    zargs[3] = fourth value (optional)
  83.  *
  84.  */
  85.  
  86. void z_je (void)
  87. {
  88.  
  89.     branch (zargc > 1 && (zargs[0] == zargs[1] || (
  90.         zargc > 2 && (zargs[0] == zargs[2] || (
  91.         zargc > 3 && (zargs[0] == zargs[3]))))));
  92.  
  93. }/* z_je */
  94.  
  95. /*
  96.  * z_jg, branch if the first value is greater than the second.
  97.  *
  98.  *    zargs[0] = first value
  99.  *    zargs[1] = second value
  100.  *
  101.  */
  102.  
  103. void z_jg (void)
  104. {
  105.  
  106.     branch ((short) zargs[0] > (short) zargs[1]);
  107.  
  108. }/* z_jg */
  109.  
  110. /*
  111.  * z_jl, branch if the first value is less than the second.
  112.  *
  113.  *    zargs[0] = first value
  114.  *    zargs[1] = second value
  115.  *
  116.  */
  117.  
  118. void z_jl (void)
  119. {
  120.  
  121.     branch ((short) zargs[0] < (short) zargs[1]);
  122.  
  123. }/* z_jl */
  124.  
  125. /*
  126.  * z_jz, branch if value is zero.
  127.  *
  128.  *     zargs[0] = value
  129.  *
  130.  */
  131.  
  132. void z_jz (void)
  133. {
  134.  
  135.     branch ((short) zargs[0] == 0);
  136.  
  137. }/* z_jz */
  138.  
  139. /*
  140.  * z_log_shift, logical SHIFT operation.
  141.  *
  142.  *     zargs[0] = value
  143.  *    zargs[1] = #positions to shift left (positive) or right (negative)
  144.  *
  145.  */
  146.  
  147. void z_log_shift (void)
  148. {
  149.  
  150.     if ((short) zargs[1] > 0)
  151.     store (ZWORD (zargs[0] << (short) zargs[1]));
  152.     else
  153.     store (ZWORD (zargs[0] >> - (short) zargs[1]));
  154.  
  155. }/* z_log_shift */
  156.  
  157. /*
  158.  * z_mod, remainder after signed 16bit division.
  159.  *
  160.  *     zargs[0] = first value
  161.  *    zargs[1] = second value
  162.  *
  163.  */
  164.  
  165. void z_mod (void)
  166. {
  167.  
  168.     if (zargs[1] == 0)
  169.     runtime_error ("Division by zero");
  170.  
  171.     store (ZWORD ((short) zargs[0] % (short) zargs[1]));
  172.  
  173. }/* z_mod */
  174.  
  175. /*
  176.  * z_mul, 16bit multiplication.
  177.  *
  178.  *     zargs[0] = first value
  179.  *    zargs[1] = second value
  180.  *
  181.  */
  182.  
  183. void z_mul (void)
  184. {
  185.  
  186.     store (ZWORD ((short) zargs[0] * (short) zargs[1]));
  187.  
  188. }/* z_mul */
  189.  
  190. /*
  191.  * z_not, bitwise NOT operation.
  192.  *
  193.  *     zargs[0] = value
  194.  *
  195.  */
  196.  
  197. void z_not (void)
  198. {
  199.  
  200.     store (ZWORD (~zargs[0]));
  201.  
  202. }/* z_not */
  203.  
  204. /*
  205.  * z_or, bitwise OR operation.
  206.  *
  207.  *    zargs[0] = first value
  208.  *    zargs[1] = second value
  209.  *
  210.  */
  211.  
  212. void z_or (void)
  213. {
  214.  
  215.     store (ZWORD (zargs[0] | zargs[1]));
  216.  
  217. }/* z_or */
  218.  
  219. /*
  220.  * z_sub, 16bit substraction.
  221.  *
  222.  *    zargs[0] = first value
  223.  *    zargs[1] = second value
  224.  *
  225.  */
  226.  
  227. void z_sub (void)
  228. {
  229.  
  230.     store (ZWORD ((short) zargs[0] - (short) zargs[1]));
  231.  
  232. }/* z_sub */
  233.  
  234. /*
  235.  * z_test, branch if all the flags of a bit mask are set in a value.
  236.  *
  237.  *    zargs[0] = value to be examined
  238.  *    zargs[1] = bit mask
  239.  *
  240.  */
  241.  
  242. void z_test (void)
  243. {
  244.  
  245.     branch ((zargs[0] & zargs[1]) == zargs[1]);
  246.  
  247. }/* z_test */
  248.