SECTION "CG8"

GET "CGHDR"

LET OUTPUTSECTION() BE
$(1 LET RL = REFLIST
    LET R = 0

    UNTIL RL=0 DO      // FILL IN LABEL REFS
    $( LET L = H2!RL
       AND A = H3!RL
       LET LABVAL = 0
       TEST L>0
         THEN R := R+1
         ELSE L := -L
       LABVAL := LABV!L
       IF LABVAL=-1 DO
          CGERROR("LABEL L%N UNSET", FALSE, L)
       ADDABO(STV, A, LABVAL)
       RL := !RL  $)
    SELECTOUTPUT(CODESTREAM)

    $( OBJWORD(T.HUNK)
       OBJWORD((STVP+1)>>1)
       OBJWORD(DSIZE)
//*<3032
       WRITEWORDS(STV, (STVP+1)>>1)
/*3032>*/
/*<8086
      MWRITEWORDS(STV, (STVP+1)>>1)
/*8086>*/
       IF R>0 DO               // OUTPUT RELOCB BLOCK
       $( OBJWORD(T.RELOCB)
          OBJWORD(R)
          RL := REFLIST
          UNTIL RL=0 DO
          $( IF H2!RL>0 DO OBJWORD(H3!RL)
             RL := !RL $)
       $)
       OBJWORD(T.END)
    $)
    SELECTOUTPUT(VERSTREAM)
$)1

 /*<8086
AND MWRITEWORDS(V, N) BE
$(
  FOR I = 0 TO N-1 DO
    V!I := ((V!I)<<8) | ((V!I)>>8)
  WRITEWORDS(V, N)
$)
 /*8086>*/

 //*<3032
 AND ASKII(C) = C!TABLE
 /*3032>*/
/*<8086 /* EBCDIC 00-07 */  #000, #001, #002, #003,    0, #011,    0, #177,
/*<8086 /* EBCDIC 08-0F */     0,    0,    0, #013, #014, #015, #016, #017,
/*<8086 /* EBCDIC 10-17 */  #020, #021, #022, #023,    0,    0, #010,    0,
/*<8086 /* EBCDIC 18-1F */  #030, #031,    0,    0, #034, #035, #036, #037,
/*<8086 /* EBCDIC 20-27 */     0,    0,    0,    0,    0, #012, #027, #033,
/*<8086 /* EBCDIC 28-2F */     0,    0,    0,    0,    0, #005, #006, #007,
/*<8086 /* EBCDIC 30-37 */     0,    0, #026,    0,    0,    0,    0, #004,
/*<8086 /* EBCDIC 38-3F */     0,    0,    0,    0, #024, #025,    0, #032,
/*<8086 /* EBCDIC 40-47 */  #040,    0,    0,    0,    0,    0,    0,    0,
/*<8086 /* EBCDIC 48-4F */     0,    0,    0, #056, #074, #050, #053, #174,
/*<8086 /* EBCDIC 50-57 */  #046,    0,    0,    0,    0,    0,    0,    0,
/*<8086 /* EBCDIC 58-5F */     0,    0, #041, #044, #052, #051, #073, #176,
/*<8086 /* EBCDIC 60-67 */  #055, #057,    0,    0,    0,    0,    0,    0,
/*<8086 /* EBCDIC 68-6F */     0,    0,    0, #054, #045, #137, #076, #077,
/*<8086 /* EBCDIC 70-77 */     0, #136,    0,    0,    0,    0,    0,    0,
/*<8086 /* EBCDIC 78-7F */     0, #140, #072, #043, #100, #047, #075, #042,
/*<8086 /* EBCDIC 80-87 */     0, #141, #142, #143, #144, #145, #146, #147,
/*<8086 /* EBCDIC 88-8F */  #150, #151,    0, #173,    0,    0,    0,    0,
/*<8086 /* EBCDIC 90-97 */     0, #152, #153, #154, #155, #156, #157, #160,
/*<8086 /* EBCDIC 98-9F */  #161, #162,    0, #175,    0,    0,    0,    0,
/*<8086 /* EBCDIC A0-A7 */     0,    0, #163, #164, #165, #166, #167, #170,
/*<8086 /* EBCDIC A8-AF */  #171, #172,    0,    0,    0, #133,    0,    0,
/*<8086 /* EBCDIC B0-B7 */     0,    0,    0,    0,    0,    0,    0,    0,
/*<8086 /* EBCDIC B8-BF */     0,    0,    0,    0,    0, #135,    0,    0,
/*<8086 /* EBCDIC C0-C7 */     0, #101, #102, #103, #104, #105, #106, #107,
/*<8086 /* EBCDIC C8-CF */  #110, #111,    0,    0,    0,    0,    0,    0,
/*<8086 /* EBCDIC D0-D7 */     0, #112, #113, #114, #115, #116, #117, #120,
/*<8086 /* EBCDIC D8-DF */  #121, #122,    0,    0,    0,    0,    0,    0,
/*<8086 /* EBCDIC E0-E7 */  #134,    0, #123, #124, #125, #126, #127, #130,
/*<8086 /* EBCDIC E8-EF */  #131, #132,    0,    0,    0,    0,    0,    0,
/*<8086 /* EBCDIC F0-F7 */  #060, #061, #062, #063, #064, #065, #066, #067,
/*<8086 /* EBCDIC F8-FF */  #070, #071,    0,    0,    0,    0,    0, #134



AND ISEVEN() = (STVP&1)=0


AND GETVBYTE(V, P) = VALOF
$( LET OT = P/2
   IF (P&1)=0 RESULTIS (V!OT&#177400)>>8
   RESULTIS V!OT & 255
$)

AND PUTVBYTE(V, P, W) BE
$( LET OT=P/2
   TEST (P&1)=0 DO V!OT := (V!OT&255) + ((W&255)<<8)
     OR V!OT := (V!OT&#177400) + (W&255)
$)

 //*<3032
AND OBJWORD(W) BE
$( LET X = (W>>8) | ((W&255)<<8)
   WRITEWORDS(@X, 1)
$)
 /*3032>*/



/*<8086
AND OBJWORD(W) BE WRITEWORDS(@W, 1)
/*8086>*/


 //*<3032
AND WRITEWORDS(V, N) BE
     FOR I = 0 TO N-1 DO
     $( IF WRITEWCOUNT >= 16 DO
        $( NEWLINE()
           WRITEWCOUNT := 0
        $)

        WRITEHEX(V!I, 4)
        WRITEWCOUNT := WRITEWCOUNT + 1
     $)

AND DBOUTPUT() BE
$(1 LET NL = "*N      "
    WRITEF("OP=%N PNDOP=%N SSP=%N LOC=%X4*NSTACK ",
           OP,PENDINGOP,SSP,STVP)
    FOR P=ARG1 TO TEMPV BY -3 DO
    $( IF (ARG1-P) REM 30 = 27 DO WRITES(NL)
       WRKN(H1!P,H2!P) $)
    WRITES("*NBREFS ")
    FOR P=BREFV TO BREFP-2 BY 2 DO
    $( IF P-BREFV REM 10 = 8 DO WRITES(NL)
       WRITEF("L%N %X4 ",P!0,P!1) $)
    WRITES("*NREGS  ")
    FOR R=R0 TO R4 DO UNLESS REG.K!R=K.NONE DO
    $( WRITEF("R%N=",R)
       WRKN(REG.K!R,REG.N!R) $)
    NEWLINE()
$)1


AND WRKN(K,N) BE
$(1 LET S = VALOF
       SWITCHON K INTO
       $( DEFAULT: RESULTIS "?"
          CASE K.NUMB:   RESULTIS "N"
          CASE K.LOC:    RESULTIS "P"
          CASE K.GLOB:   RESULTIS "G"
          CASE K.LAB:    RESULTIS "L"
          CASE K.MLOC:   RESULTIS "ML"
          CASE K.MGLOB:  RESULTIS "MG"
          CASE K.MLAB:   RESULTIS "ML"
          CASE K.LVLOC:  RESULTIS "@P"
          CASE K.LVGLOB: RESULTIS "@G"
          CASE K.LVLAB:  RESULTIS "@L"
          CASE K.REG:    RESULTIS "R"
          CASE K.XBX:     RESULTIS "XBX "
          CASE K.XSI:     RESULTIS "XSI "
       $)
    WRITEF("%S%N  ",S,N)
$)1

    // LIST INSTRUCTIONS
AND LISTGEN(F, K1, N1, K2, N2) BE IF CGLISTING DO
$(   LISTCHAN := TRUE
     WRITEF("*N%X4          %S", STVP, F)
     FOR I = 0 TO 6-GETBYTE(F, 0) DO WRCH('*S')
     UNLESS K1=0 DO
     $(   WRITEOP(K1, N1)
          UNLESS K2=0 DO
          $(   WRCH(',')
               WRITEOP(K2, N2)
          $)
     $)
$)



AND WRITEOP(M, V) BE
  SWITCHON M INTO
  $(   CASE M.REG:
       WRITES(REGSTRING(V))
       ENDCASE

       CASE M.GLOB:  M := TRANR(R.DI); GOTO PO
       CASE M.LOC:   M := TRANR(R.BP); GOTO PO
       CASE M.BX:    M := TRANR(R.BX); GOTO PO
       CASE M.SI:    M := TRANR(R.SI)
PO:    WRITEF("%N(", V)
       WRITEOP(M.REG, M)
       WRCH(')')
       ENDCASE

       CASE M.IMM:
       WRITEF("!#X%X4", V)
       ENDCASE

       CASE M.LAB:
       WRITEF("L%N", V)
       ENDCASE

       DEFAULT:
       CGERROR("BAD TYPE IN WRITEOP %N", FALSE, M)
  $)



AND LISTAT(T, V) BE IF CGLISTING DO
$(   LISTCHAN := TRUE

     SWITCHON T INTO
     $(   CASE S.DATALAB:
          LISTLAB(V)
          ENDCASE

          CASE S.ITEML:
          WRITEF("*N%X4          DW L%N", STVP, V)
          ENDCASE

       CASE S.ITEMN:
          WRITEF("*N%X4          DW %X4", STVP, V)
          ENDCASE

          DEFAULT:
          CGERROR("BAD TYPE IN LISTAT %N", FALSE, T)
     $)
$)



AND LISTL(S, A, B, C, D, E) BE IF CGLISTING DO
$(   LISTCHAN := TRUE
     WRITEF("*N%X4          ", STVP)
     WRITEF(S, A, B, C, D, E)
$)



AND STRINGRB(T) = VALOF
  SWITCHON T INTO
  $( CASE 0: RESULTIS "AL"
     CASE 1: RESULTIS "CL"
     CASE 2: RESULTIS "DL"
     CASE 3: RESULTIS "BL"
     CASE 4: RESULTIS "AH"
     CASE 5: RESULTIS "CH"
     CASE 6: RESULTIS "DH"
     CASE 7: RESULTIS "BH"
     DEFAULT: RESULTIS "BADREG"
  $)



AND SF(F) = VALOF
  SWITCHON F INTO
  $( CASE F.XOR:    RESULTIS "XOR"
     CASE F.OR:    RESULTIS "OR"
     CASE F.AND:    RESULTIS "AND"
     CASE F.ADD:    RESULTIS "ADD"
     CASE F.SUB:    RESULTIS "SUB"
     CASE F.CMP:    RESULTIS "CMP"

     CASE F.MOVIMMTRM:
     CASE F.MOVRTRM:
     CASE F.MOVIMMTR:    RESULTIS "MOV"

     CASE F.LEA:     RESULTIS "LEA"
     CASE F.CWD:     RESULTIS "CWD"

     CASE F.JMP:     RESULTIS "JMP"
     CASE F.JGE:     RESULTIS "JGE"
     CASE F.JG:      RESULTIS "JG"
     CASE F.JLE:     RESULTIS "JLE"
     CASE F.JL:      RESULTIS "JL"
     CASE F.JE:      RESULTIS "JE"
     CASE F.JNE:     RESULTIS "JNE"

     CASE F.NEG:     RESULTIS "NEG"
     CASE F.NOT:     RESULTIS "NOT"

     CASE F.SHL:     RESULTIS "SHL"
     CASE F.SHR:     RESULTIS "SHR"
     CASE F.SAR:     RESULTIS "SAR"

     CASE F.NOP:     RESULTIS "NOP"

     DEFAULT: RESULTIS "UNKNOWN"
  $)



AND LISTLAB(L) BE IF CGLISTING DO
$( LISTCHAN := TRUE
  WRITEF("*N%X4  L%N EQU $", STVP, L)
$)



AND REGSTRING(R) = VALOF
  SWITCHON R INTO
  $( CASE 3: RESULTIS "BX"
     CASE 0: RESULTIS "AX"
     CASE 1: RESULTIS "CX"
     CASE 2: RESULTIS "DX"
     CASE 6: RESULTIS "SI"
     CASE 5: RESULTIS "BP"
     CASE 4: RESULTIS "SP"
     CASE 7: RESULTIS "DI"
     DEFAULT: RESULTIS "BADREG"
  $)
 /*3032>*/