home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / SMALL_C / CC42.C < prev    next >
Text File  |  1987-10-04  |  5KB  |  307 lines

  1. /*
  2. ** Small C - 8088/8086 version - modified by R. Grehan, BYTE Magazine
  3. **
  4. ** add primary and secondary registers (result in primary)
  5. */
  6. ffadd() {ol(" ADD BX,DX");}
  7.  
  8. /*
  9. ** subtract primary from secondary register (result in primary)
  10. */
  11. ffsub() {ol(" SUB DX,BX"); ol(" MOV BX,DX");}
  12.  
  13. /*
  14. ** multiply primary and secondary registers (result in primary)
  15. */
  16. ffmult() {ol(" MOV AX,DX"); ol(" IMUL BX"); ol(" MOV BX,AX");}
  17.  
  18. /*
  19. ** divide secondary by primary register
  20. ** (quotient in primary, remainder in secondary)
  21. */
  22. ffdiv() {ol(" MOV AX,DX"); ol(" SUB DX,DX"); ol(" IDIV BX");
  23.          ol(" MOV BX,AX"); }
  24.  
  25. /*
  26. ** remainder of secondary/primary
  27. ** (remainder in primary, quotient in secondary)
  28. */
  29. ffmod() {ffdiv();swap();}
  30.  
  31. /*
  32. ** inclusive "or" primary and secondary registers
  33. ** (result in primary)
  34. */
  35. ffor() {ol(" OR BX,DX");}
  36.  
  37. /*
  38. ** exclusive "or" the primary and secondary registers
  39. ** (result in primary)
  40. */
  41. ffxor() {ol(" XOR BX,DX");}
  42.  
  43. /*
  44. ** "and" primary and secondary registers
  45. ** (result in primary)
  46. */
  47. ffand() {ol(" AND BX,DX");}
  48.  
  49. /*
  50. ** logical negation of primary register
  51. */
  52. lneg() {
  53.   ol(" OR BX,BX");
  54.   ol(" MOV BX,CX");
  55.   ol(" JNZ $+3");
  56.   ol(" INC BX");
  57. }
  58.  
  59. /*
  60. ** arithmetic shift right secondary register
  61. ** number of bits given in primary register
  62. ** (result in primary)
  63. ** 8088 version: Note that I don't check BH to make
  64. ** sure there's nothing in it.  Might be a problem.--RG
  65. */
  66. ffasr() {ol(" MOV CL,BL"); ol(" SAR DX,CL"); ol(" MOV BX,DX");
  67.          ol(" XOR CX,CX"); }
  68.  
  69. /*
  70. ** arithmetic shift left secondary register
  71. ** number of bits given in primary register
  72. ** (result in primary)
  73. */
  74. ffasl() {ol(" MOV CL,BL"); ol(" SAL DX,CL"); ol(" MOV BX,DX");
  75.          ol(" XOR CX,CX"); }
  76.  
  77. /*
  78. ** two's complement primary register
  79. */
  80. neg() {ol(" NEG BX");}
  81.  
  82. /*
  83. ** one's complement primary register
  84. */
  85. com() {ol(" NOT BX");}
  86.  
  87. /*
  88. ** increment primary register by one object of whatever size
  89. ** 8088 version: I altered this slightly from the original
  90. ** code since it's easier to do 16-bit math on the 8088 --RG
  91. */
  92. inc(n) int n; {
  93.   if (n<=2) {
  94.     while(1) {
  95.      ol(" INC BX");
  96.      if(--n < 1) break;
  97.     }
  98.   }
  99.   else
  100.   {  ot(" ADD BX,");
  101.      outdec(n);
  102.      nl();
  103.   }
  104. }
  105.  
  106. /*
  107. ** decrement primary register by one object of whatever size
  108. ** 8088 version: Same thing as inc(n) --RG
  109. */
  110. dec(n) int n; {
  111.   if (n<=2) {
  112.     while(1) {
  113.      ol(" DEC BX");
  114.      if(--n < 1) break;
  115.     }
  116.   }
  117.   else
  118.   {  ot(" SUB BX,");
  119.      outdec(n);
  120.      nl();
  121.   }
  122. }
  123.  
  124. /*
  125. ** test for equal to
  126. */
  127. ffeq()  {
  128.   ol(" CMP DX,BX");
  129.   ol(" MOV BX,CX");
  130.   ol(" JNZ $+3");
  131.   ol(" INC BX");
  132. }
  133.  
  134. /*
  135. ** test for equal to zero
  136. */
  137. eq0(label) int label; {
  138.   ol(" OR BX,BX");
  139.   ol(" JZ $+5");
  140.   ot(" JMP ");
  141.   printlabel(label);
  142.   nl();
  143.   }
  144.  
  145. /*
  146. ** test for not equal to
  147. */
  148. ffne()  {
  149.   ol(" CMP DX,BX");
  150.   ol(" MOV BX,CX");
  151.   ol(" JE $+3");
  152.   ol(" INC BX");
  153. }
  154.  
  155. /*
  156. ** test for not equal to zero
  157. */
  158. ne0(label) int label; {
  159.   ol(" OR BX,BX");
  160.   ol(" JNZ $+5");
  161.   ot(" JMP ");
  162.   printlabel(label);
  163.   nl();
  164.   }
  165.  
  166. /*
  167. ** test for less than (signed)
  168. */
  169. fflt()  {
  170.   ol(" CMP DX,BX");
  171.   ol(" MOV BX,CX");
  172.   ol(" JGE $+3");
  173.   ol(" INC BX");
  174. }
  175.  
  176. /*
  177. ** test for less than zero
  178. */
  179. lt0(label) int label; {
  180.   ol(" OR BX,BX");
  181.   ol(" JS $+5");
  182.   ot(" JMP ");
  183.   printlabel(label);
  184.   nl();
  185.   }
  186.  
  187. /*
  188. ** test for less than or equal to (signed)
  189. */
  190. ffle()  {
  191.   ol(" CMP DX,BX");
  192.   ol(" MOV BX,CX");
  193.   ol(" JG $+3");
  194.   ol(" INC BX");
  195. }
  196.  
  197. /*
  198. ** test for less than or equal to zero
  199. */
  200. le0(label) int label; {
  201.   ol(" OR BX,BX");
  202.   ol(" JLE $+5");
  203.   ot(" JMP ");
  204.   printlabel(label);
  205.   nl();
  206.   }
  207.  
  208. /*
  209. ** test for greater than (signed)
  210. */
  211. ffgt()  {
  212.   ol(" CMP DX,BX");
  213.   ol(" MOV BX,CX");
  214.   ol(" JLE $+3");
  215.   ol(" INC BX");
  216. }
  217.  
  218. /*
  219. ** test for greater than zero
  220. */
  221. gt0(label) int label; {
  222.   ol(" OR BX,BX");
  223.   ol(" JG $+5");
  224.   ot(" JMP ");
  225.   printlabel(label);
  226.   nl();
  227.   }
  228.  
  229. /*
  230. ** test for greater than or equal to (signed)
  231. */
  232. ffge()  {
  233.   ol(" CMP DX,BX");
  234.   ol(" MOV BX,CX");
  235.   ol(" JL $+3");
  236.   ol(" INC BX");
  237. }
  238.  
  239. /*
  240. ** test for greater than or equal to zero
  241. */
  242. ge0(label) int label; {
  243.   ol(" OR BX,BX");
  244.   ol(" JGE $+5");
  245.   ot(" JMP ");
  246.   printlabel(label);
  247.   nl();
  248.   }
  249.  
  250. /*
  251. ** test for less than (unsigned)
  252. */
  253. ult()  {
  254.   ol(" CMP DX,BX");
  255.   ol(" MOV BX,CX");
  256.   ol(" JAE $+3");
  257.   ol(" INC BX");
  258. }
  259.  
  260. /*
  261. ** test for less than zero (unsigned)
  262. */
  263. ult0(label) int label; {
  264.   ot(" JMP ");
  265.   printlabel(label);
  266.   nl();
  267.   }
  268.  
  269. /*
  270. ** test for less than or equal to (unsigned)
  271. */
  272. ule()  {
  273.   ol(" CMP DX,BX");
  274.   ol(" MOV BX,CX");
  275.   ol(" JA $+3");
  276.   ol(" INC BX");
  277. }
  278.  
  279. /*
  280. ** test for greater than (unsigned)
  281. */
  282. ugt()  {
  283.   ol(" CMP DX,BX");
  284.   ol(" MOV BX,CX");
  285.   ol(" JBE $+3");
  286.   ol(" INC BX");
  287. }
  288.  
  289. /*
  290. ** test for greater than or equal to (unsigned)
  291. */
  292. uge()  {
  293.   ol(" CMP DX,BX");
  294.   ol(" MOV BX,CX");
  295.   ol(" JB $+3");
  296.   ol(" INC BX");
  297. }
  298.  
  299. #ifdef OPTIMIZE
  300. peephole(ptr) char *ptr; {
  301.   while(*ptr) {
  302.   cout(*ptr++,output);
  303.   }
  304. }
  305. #endif
  306.  
  307.