home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 163_01 / cc42.c < prev    next >
Text File  |  1988-01-31  |  6KB  |  286 lines

  1. /*
  2. ** add primary and secondary registers (result in primary)
  3. */
  4. add() {ol("ADD AX,BX");}
  5.  
  6. /*
  7. ** subtract primary from secondary register (result in primary)
  8. */
  9. sub() {
  10.   ol("SUB AX,BX");
  11.   ol("NEG AX");
  12.   }
  13.  
  14. /*
  15. ** multiply primary and secondary registers (result in primary)
  16. */
  17. mult() {ol("IMUL BX");} /* (note that DX -- arg count -- is clobbered) */
  18.  
  19. /*
  20. ** divide secondary by primary register
  21. ** (quotient in primary, remainder in secondary)
  22. */
  23. div() {
  24.   ol("XCHG AX,BX"); /* get the dividend into AX */
  25.   ol("CWD"); /* extend AX sign into DX */
  26.   ol("IDIV BX");
  27.   ol("MOV BX,DX"); /* get remainder */
  28.   }
  29.  
  30. /*
  31. ** remainder of secondary/primary
  32. ** (remainder in primary, quotient in secondary)
  33. */
  34. mod() {div();swap();}
  35.  
  36. /*
  37. ** inclusive "or" primary and secondary registers
  38. ** (result in primary)
  39. */
  40. or() {ol("OR AX,BX");}
  41.  
  42. /*
  43. ** exclusive "or" the primary and secondary registers
  44. ** (result in primary)
  45. */
  46. xor() {ol("XOR AX,BX");}
  47.  
  48. /*
  49. ** "and" primary and secondary registers
  50. ** (result in primary)
  51. */
  52. and() {ol("AND AX,BX");}
  53.  
  54. /*
  55. ** logical negation of primary register
  56. */
  57. lneg() {
  58.   ol("AND AX,AX");
  59.   ol("MOV AX,1");
  60.   ol("JZ $+3");
  61.   ol("DEC AX");
  62.   }
  63.  
  64. /*
  65. ** arithmetic shift right secondary register
  66. ** number of bits given by primary register
  67. ** (result in primary)
  68. */
  69. asr() {
  70.   ol("MOV CX,AX"); /* get count to count register */
  71.   ol("MOV AX,BX"); /* get shiftee to result reg */
  72.   ol("SAR AX,CL"); /* shift the shiftee */
  73.   /*
  74.   ** (note that the shift is mod 256 -- consistent with K & R)
  75.   */
  76.   }
  77.  
  78. /*
  79. ** arithmetic shift left secondary register
  80. ** number of bits in primary register
  81. ** (result in primary)
  82. */
  83. asl() {
  84.   ol("MOV CX,AX"); /* get count to count register */
  85.   ol("MOV AX,BX"); /* get shiftee to result reg */
  86.   ol("SAL AX,CL"); /* shift the shiftee */
  87.   /*
  88.   ** (note that the shift is mod 256 -- consistent with K & R)
  89.   */
  90.   }
  91.  
  92. /*
  93. ** two's complement primary register
  94. */
  95. neg() {ol("NEG AX");}
  96.  
  97. /*
  98. ** one's complement primary register
  99. */
  100. com() {ol("NOT AX");}
  101.  
  102. /*
  103. ** (beware: the following two functions may get an argument of zero,
  104. ** indicating that the value being incremented or decremented is not
  105. ** an address and should therefore be incremented/decremented by 1)
  106. */
  107.  
  108. /*
  109. ** increment primary register by one object of whatever size
  110. */
  111. inc(n) int n; {
  112.   if(n==2) ol("ADD AX,2");
  113.   else     ol("INC AX");
  114.   }
  115.  
  116. /*
  117. ** decrement primary register by one object of whatever size
  118. */
  119. dec(n) int n; {
  120.   if(n==2) ol("SUB AX,2");
  121.   else     ol("DEC AX");
  122.   }
  123.  
  124. /*
  125. ** test for secondary equal to primary
  126. */
  127. eq()  {
  128.   ol("CMP BX,AX");
  129.   ol("MOV AX,1");
  130.   ol("JE $+3");
  131.   ol("DEC AX");
  132.   }
  133.  
  134. /*
  135. ** test for equal to zero
  136. */
  137. eq0(label) int label; {
  138.   ol("AND AX,AX"); /* set condition code */
  139.   ol("JZ $+5"); /* jump if zero */
  140.   jump(label); /* false condition */
  141.   }
  142.  
  143. /*
  144. ** test for secondary not equal to primary
  145. */
  146. ne() {
  147.   ol("CMP BX,AX");
  148.   ol("MOV AX,1");
  149.   ol("JNE $+5");
  150.   ol("MOV AX,0");
  151.   }
  152.  
  153. /*
  154. ** test for not equal to zero
  155. */
  156. ne0(label) int label; {
  157.   ol("AND AX,AX"); /* set condition code */
  158.   ol("JNZ $+5"); /* jump if not zero */
  159.   jump(label); /* false condition */
  160.   }
  161.  
  162. /*
  163. ** test for secondary less than primary (signed)
  164. */
  165. lt() {
  166.   ol("CMP BX,AX");
  167.   ol("MOV AX,1");
  168.   ol("JL $+3");
  169.   ol("DEC AX");
  170.   }
  171.  
  172. /*
  173. ** test for less than zero
  174. */
  175. lt0(label) int label; {
  176.   ol("AND AX,AX"); /* set condition code */
  177.   ol("JL $+5"); /* jump if negative */
  178.   jump(label); /* false condition */
  179.   }
  180.  
  181. /*
  182. ** test for secondary less than or equal to primary (signed)
  183. */
  184. le() {
  185.   ol("CMP BX,AX");
  186.   ol("MOV AX,1");
  187.   ol("JLE $+3");
  188.   ol("DEC AX");
  189.   }
  190.  
  191. /*
  192. ** test for less than or equal to zero
  193. */
  194. le0(label) int label; {
  195.   ol("AND AX,AX"); /* set condition code */
  196.   ol("JLE $+5"); /* jump if not positive */
  197.   jump(label); /* false condition */
  198.   }
  199.  
  200. /*
  201. ** test for secondary greater than primary (signed)
  202. */
  203. gt() {
  204.   ol("CMP BX,AX");
  205.   ol("MOV AX,1");
  206.   ol("JG $+3");
  207.   ol("DEC AX");
  208.   }
  209.  
  210. /*
  211. ** test for greater than zero
  212. */
  213. gt0(label) int label; {
  214.   ol("AND AX,AX"); /* set condition code */
  215.   ol("JG $+5"); /* jump if greater than zero */
  216.   jump(label); /* false condition */
  217.   }
  218.  
  219. /*
  220. ** test for secondary greater than or equal to primary (signed)
  221. */
  222. ge() {
  223.   ol("CMP BX,AX");
  224.   ol("MOV AX,1");
  225.   ol("JGE $+3");
  226.   ol("DEC AX");
  227.   }
  228.  
  229. /*
  230. ** test for greater than or equal to zero
  231. */
  232. ge0(label) int label; {
  233.   ol("AND AX,AX"); /* set condition code */
  234.   ol("JGE $+5"); /* jump if not negative */
  235.   jump(label); /* false condition */
  236.   }
  237.  
  238. /*
  239. ** test for secondary less than primary (unsigned)
  240. */
  241. ult() {
  242.   ol("CMP BX,AX");
  243.   ol("MOV AX,1");
  244.   ol("JC $+3");
  245.   ol("DEC AX");
  246.   }
  247.  
  248. /*
  249. ** test for less than zero (unsigned)
  250. */
  251. ult0(label) int label; {
  252.   ot("JMP ");
  253.   printlabel(label);
  254.   nl();
  255.   }
  256.  
  257. /*
  258. ** test for secondary less than or equal to primary (unsigned)
  259. */
  260. ule() {
  261.   ol("CMP AX,BX");
  262.   ol("MOV AX,0");
  263.   ol("JC $+3");
  264.   ol("INC AX");
  265.   }
  266.  
  267. /*
  268. ** test for secondary greater than primary (unsigned)
  269. */
  270. ugt() {
  271.   ol("CMP AX,BX");
  272.   ol("MOV AX,1");
  273.   ol("JC $+3");
  274.   ol("DEC AX");
  275.   }
  276.  
  277. /*
  278. ** test for secondary greater than or equal to primary (unsigned)
  279. */
  280. uge() {
  281.   ol("CMP BX,AX");
  282.   ol("MOV AX,0");
  283.   ol("JC $+3");
  284.   ol("INC AX");
  285.   }
  286.