t34_arith.pas 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. program t34;
  2. { t34 -- the operators no other fixture uses.
  3. Every one of `div', `mod', `and', `or', unary `-' and a variable `*' is
  4. emitted by this compiler and has never been executed by any test: t08 was
  5. the only fixture that multiplied and it multiplied two CONSTANTS, which
  6. BinOpEmit folds without emitting anything. So `EmIDivAxCx', `EmXchgAxDx',
  7. `EmAndAxCx', `EmOrAxCx' and `EmNegAx' were all untested, and so is every
  8. instruction the Exec86 interpreter implements for them.
  9. The operands are deliberately VARIABLES, for the same reason: with two
  10. constants `p div q' folds at compile time and the runtime never runs.
  11. The four sign combinations of div and mod are the point. Pascal's `div'
  12. truncates toward zero and `mod' takes the sign of the dividend:
  13. a div b = truncation of a/b toward zero
  14. a mod b = a - b * (a div b)
  15. so -17 div 5 = -3 and -17 mod 5 = -2, where a floor-division machine would
  16. answer -4 and 1. ISO Modula-2, which Exec86 is written in, has Euclidean
  17. DIV/MOD (measured: (-7) MOD 2 = 1, 7 MOD (-2) = 1), so the interpreter has
  18. to convert; these eight lines are what says whether it converts the right
  19. way.
  20. `not' is absent here on purpose: it has its own fixture, t35_not, which
  21. is where the boolean/int split in TPSRC9 neglevel is checked. }
  22. var
  23. p : integer;
  24. q : integer;
  25. begin
  26. p := 17;
  27. q := 5;
  28. writeln (p * q);
  29. writeln (p + q);
  30. writeln (p - q);
  31. writeln (p div q);
  32. writeln (p mod q);
  33. writeln (-p);
  34. writeln ((p > q) and (q > p));
  35. writeln ((p > q) or (q > p));
  36. p := -17;
  37. q := 5;
  38. writeln (p div q);
  39. writeln (p mod q);
  40. writeln (-p);
  41. p := 17;
  42. q := -5;
  43. writeln (p div q);
  44. writeln (p mod q);
  45. p := -17;
  46. q := -5;
  47. writeln (p div q);
  48. writeln (p mod q)
  49. end.