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

  1. /*
  2. ** Small C - 8088/8086 version - modified by R. Grehan, BYTE Magazine
  3. **
  4. ** print all assembler info before any code is generated
  5. */
  6. header()  {
  7.   ol(" INCLUDE PROLOG.H");
  8.   beglab=getlabel();
  9.   }
  10.  
  11. /*
  12. ** print any assembler stuff needed at the end
  13. */
  14. trailer()  {  
  15. #ifndef LINK
  16.   if((beglab == 1)|(beglab > 9000)) {
  17.     /* implementation dependent trailer code goes here */
  18.     }
  19. #else
  20.   char *ptr;
  21.   cptr=STARTGLB;
  22.   while(cptr<ENDGLB) {
  23.     if(cptr[IDENT]==FUNCTION && cptr[CLASS]==AUTOEXT)
  24.      { external(cptr+NAME);
  25.       ol("NEAR");
  26.      }
  27.     cptr+=SYMMAX;
  28.     }
  29. #ifdef UPPER
  30.   if((ptr=findglb("MAIN")) && (ptr[OFFSET]==FUNCTION))
  31. #else
  32.   if((ptr=findglb("main")) && (ptr[OFFSET]==FUNCTION))
  33. #endif
  34. {   external("Uend");    /* link to library functions */
  35.     ol("NEAR");
  36. }
  37.     if(swused) { external("CCSWITCH");  /* RG */
  38.                ol("NEAR"); }
  39. #endif
  40.   ol(" INCLUDE EPILOG.H");
  41.   ol(" END");
  42.   }
  43.  
  44. /*
  45. ** load # args before function call
  46. */
  47. loadargc(val) int val; {
  48.   if(search("NOCCARGC", macn, NAMESIZE+2, MACNEND, MACNBR, 0)==0) {
  49.     if(val) {
  50.       ot(" MOV AL,");
  51.       outdec(val);
  52.       nl();
  53.       }
  54.     else ol(" XOR AL,AL");
  55.     }
  56.   }
  57.  
  58. /*
  59. ** declare entry point
  60. */
  61. entry() {
  62.  
  63.   /* Need this for 8088 -- RG */
  64.  
  65.   ot(" PUBLIC ");
  66.   outlab(ssname);
  67.   nl();
  68.  
  69.   outlab(ssname);
  70.   col();
  71.  
  72. /* This code removed so MASM won't barf. --RG
  73. **
  74. ** #ifdef LINK
  75. **  col();
  76. ** #endif
  77. */
  78.  
  79.   nl();
  80.   }
  81.  
  82. /*
  83. ** declare external reference
  84. */
  85. external(name) char *name; {
  86. #ifdef LINK
  87.   ot(" EXTRN ");
  88.   outlab(name);    /* Changed to ot() because MASM needs :class */
  89.   ot(":");
  90. #endif
  91.   }
  92.  
  93. /*
  94. ** fetch object indirect to primary register
  95. */
  96. indirect(lval) int lval[]; {
  97.   if(lval[1]==CCHAR) {
  98.               ol(" MOV AL,[BX]");
  99.               ol(" CBW");
  100.               ol(" MOV BX,AX");
  101.   }
  102.   else {
  103.               ol(" MOV BX,[BX]");
  104.   }
  105. }
  106.  
  107. /*
  108. ** fetch a static memory cell into primary register
  109. */
  110. getmem(lval)  int lval[]; {
  111.   char *sym;
  112.   sym=lval[0];
  113.   if((sym[IDENT]!=POINTER)&(sym[TYPE]==CCHAR)) {
  114.     ot(" MOV AL,");
  115.     outlab(sym+NAME);
  116.     nl();
  117.     ol(" CBW");
  118.     ol(" MOV BX,AX");
  119.     }
  120.   else {
  121.     ot(" MOV BX,");
  122.     outlab(sym+NAME);
  123.     nl();
  124.     }
  125.   }
  126.  
  127. /*
  128. ** fetch addr of the specified symbol into primary register
  129. */
  130. getloc(sym)  char *sym; {
  131.   const(getint(sym+OFFSET, OFFSIZE)-csp);
  132.   ol(" ADD BX,SP");
  133.   }
  134.  
  135. /*
  136. ** store primary register into static cell
  137. */
  138. putmem(lval)  int lval[]; {
  139.   char *sym;
  140.   ot(" MOV ");
  141.   sym=lval[0];
  142.   outlab(sym+NAME);
  143.   if((sym[IDENT]!=POINTER)&(sym[TYPE]==CCHAR)) {
  144.     ot(",BL");
  145.     }
  146.   else ot(",BX");
  147.   nl();
  148.   }
  149.  
  150. /*
  151. ** put on the stack the type object in primary register
  152. */
  153. putstk(lval) int lval[]; {
  154.   ol(" MOV SI,DX");
  155.   if(lval[1]==CCHAR) {
  156.     ol(" MOV [SI],BL");
  157.     }
  158.   else {
  159.     ol(" MOV [SI],BX");
  160.  }
  161. }
  162.  
  163. /*
  164. ** move primary register to secondary
  165. */
  166. move() {
  167.   ol(" MOV DX,BX");
  168.   }
  169.  
  170. /*
  171. ** swap primary and secondary registers
  172. */
  173. swap() {
  174.   ol(" XCHG BX,DX");
  175.   }
  176.  
  177. /*
  178. ** partial instruction to get immediate value
  179. ** into the primary register
  180. */
  181. immed() {
  182.   ot(" MOV BX,");
  183.   }
  184.  
  185. /*
  186. ** partial instruction to get immediate operand
  187. ** into secondary register
  188. */
  189. immed2() {
  190.   ot(" MOV DX,");
  191.   }
  192.  
  193. /*
  194. ** push primary register onto stack
  195. */
  196. push() {
  197.   ol(" PUSH BX  ;");  /* Extra "  ;" makes required by unpush() */
  198.   csp=csp-BPW;
  199.   }
  200.  
  201. /*
  202. ** unpush or pop as required
  203. */
  204. smartpop(lval, start) int lval[]; char *start; {
  205.   if(lval[5])  pop();        /* secondary was used */
  206.   else unpush(start);
  207.   }
  208.  
  209. /*
  210. ** replace a push with a swap
  211. */
  212. unpush(dest) char *dest; {
  213.   int i;
  214.   char *sour;
  215.   sour=" XCHG DX,BX";
  216.   while(*sour) *dest++ = *sour++;
  217.   sour=stagenext;
  218.   while(--sour > dest) {    /* adjust stack references */
  219.     if(streq(sour," ADD BX,SP")) {
  220.       --sour;
  221.       i=BPW;
  222.       while(isdigit(*(--sour))) {
  223.         if((*sour = *sour-i) < '0') {
  224.           *sour = *sour+10;
  225.           i=1;
  226.           }
  227.         else i=0;
  228.         }
  229.       }
  230.     }
  231.   csp=csp+BPW;
  232.   }
  233.  
  234. /*
  235. ** pop stack to the secondary register
  236. */
  237. pop() {
  238.   ol(" POP DX");
  239.   csp=csp+BPW;
  240.   }
  241.  
  242. /*
  243. ** swap primary register and stack
  244. */
  245. swapstk() {
  246.   ol(" MOV SI,SP");
  247.   ol(" XCHG [SI],BX");
  248.   }
  249.  
  250. /*
  251. ** process switch statement
  252. */
  253. sw() {
  254.   ffcall("CCSWITCH");
  255.  
  256. /* Following added so we know to generate an EXTRN call for */
  257. /* CCSWITCH.  -- RG */
  258.   swused=1;
  259.   }
  260.  
  261. /*
  262. ** call specified subroutine name
  263. */
  264. ffcall(sname)  char *sname; {
  265.   ot(" CALL ");
  266.   outlab(sname);  /* Changed to outlab() --RG */
  267.   nl();
  268.   }
  269.  
  270. /*
  271. ** return from subroutine
  272. */
  273. ffret() {
  274.   ol(" RET");
  275.   }
  276.  
  277. /*
  278. ** perform subroutine call to value on stack
  279. */
  280. callstk() {
  281.   ol(" CALL BX");
  282.   }
  283.  
  284. /*
  285. ** jump to internal label number
  286. */
  287. jump(label)  int label; {
  288.   ot(" JMP ");
  289.   printlabel(label);
  290.   nl();
  291.   }
  292.  
  293. /*
  294. ** test primary register and jump if false
  295. */
  296. testjump(label)  int label; {
  297.   ol(" OR BX,BX");
  298.   ol(" JNZ $+5");
  299.   ot(" JMP ");
  300.   printlabel(label);
  301.   nl();
  302.   }
  303.  
  304. /*
  305. ** test primary register against zero and jump if false
  306. */
  307. zerojump(oper, label, lval) int (*oper)(), label, lval[]; {
  308.   clearstage(lval[7], 0);    /* purge conventional code */
  309.   (*oper)(label);
  310.   }
  311.  
  312. /*
  313. ** define storage according to size
  314. */
  315. defstorage(size) int size; {
  316.   if(size==1) ot(" DB ");
  317.   else        ot(" DW ");
  318.   }
  319.  
  320. /*
  321. ** point to following object(s)
  322. */
  323. point() {
  324.   ol(" DW $+2");
  325.   }
  326.  
  327. /*
  328. ** modify stack pointer to value given
  329. */
  330. modstk(newsp, save)  int newsp, save; {
  331.   int k;
  332.   k=newsp-csp;
  333.   if(k==0)return newsp;
  334.   if(k>=0) {
  335.       ot(" ADD SP,");
  336.       outdec(k);
  337.       nl();
  338.       return newsp;
  339.     }
  340.   if(k<0) {
  341.         ot(" SUB SP,");
  342.         outdec(0-k);
  343.         nl();
  344.         return newsp;
  345.    }
  346. }
  347. /*
  348. ** double primary register
  349. */
  350. doublereg() {ol(" ADD BX,BX");}
  351.