
      HED FORMATTED OUTPUT ROUTINE
      ORG 54000B
.X    OCT 130 
S     OCT 123 
D     OCT 104 
.A    OCT 101 
M46   DEC -46 
..73  DEC 73
* 
*  THE ADDRESS OF THE FIRST WORD OF THE FORMAT
*  STRING IS IN (B) UPON ENTRY.    THE FORMATTER
*  EXTRACTS THE NUMBER OF CHARACTERS IN THE STRING
*  AND THEN EXTRACTS THE FORMAT SPECIFICATIONS
*  ONE BY ONE.  AS EACH SPECIFICATION IS EXTRACTED, 
*  IT IS LOADED INTO A STACK, ONE CHARACTER PER 
*  WORD AND CHECKED FOR SYNTAX ERRORS.  THE 
*  TYPE OF SPECIFICATION IS DETERMINED AT THIS
*  TIME AND THE SPECIFICATION IS THEN EXECUTED
*  FROM THE STACK.
* 
#FRMT STB EC        SAVE POINTER TO STRING
      INB           MAKE INTO 
      CLE,ELB         CHARACTER POINTER 
      ADB A         ADD IN STARTING CHARACTER 
      CMA,INA       SAVE STARTING 
      STA CC          CHARACTER 
      STB IFSTR     SAVE IN FORMAT STRING ADDRESS 
      STB DP          AND DELIMITER POINTER 
      LDB NCH       MAYBE 
      SZB 
      JMP FM0       YES 
      LDA EC,I      NO, COMPUTE # 
      AND B377        OF CHARACTERS 
      ADA CC            IN FORMAT 
      STA NCH             STRING
      SZA,RSS       NULL STRING?
      JMP FMEND+1   YES, IGNORE IT
FM0   CLA           INITIALIZE
      STA CC          CHARACTER COUNTER 
      STA CONTR       CONTROL CHARACTER 
      STA EC          EXPRESSION COUNTER
      STA CC1         START OF PARENTHESIS LEVEL 1
      STA CC2         START OF PARENTHESIS LEVEL 2
      STA PC1         REPETITION COUNT FOR LEVEL 1
      STA PC2         REPETITION COUNT FOR LEVEL 2
      STA SFLG        STRING FLAG 
FMT2  JSB DSRCH     DELIMITER SEARCH
      STO           IGNORE BLANKS 
      LDA FST       GET THE 
      JSB MCHAR       FIRST CHARACTER 
      CPA DP        DELIMITER FOUND ? 
      JMP FMEND     YES 
      CPA .+53B     IS CHARACTER A PLUS ? 
      JMP FMT1      YES 
      CPA .+55B     IS IT A MINUS ? 
      JMP FMT1      YES 
      CPA .+43B     NO, IS IT A NUMBER SIGN 
      RSS           YES 
      JMP FMT3      NO
FMT1  LDB CC        END OF
      CPB NCH         STRING ?
      JSB FERRS,I   YES, ERROR
      STA CONTR     SAVE CARR. CONTROL CHARACTER
      ISZ FST       INCREMENT STRING POINTER
      LDA FST 
      STO           IGNORE BLANKS 
      JSB MCHAR     GET NEXT CHARACTER
      CPA DP        DELIMITER FOUND ? 
      RSS           YES 
      JMP FMT01     NO, CHARACTER FOUND ? 
      CLO           GET 
      LDA DP          THE 
      JSB MCHAR         DELIMITER 
FMT01 CPA .+54B     IS IT A COMMA ? 
      RSS           YES 
      JSB FERRS+1,I NO, ERROR 
      ISZ DP        INCREMENT DELIMITER POINTER 
      ISZ CC        AND CHARACTER COUNTER 
      LDB CC
      CPB NCH       ALL CHARACTERS USED ? 
      JSB FERRS,I   YES, ERROR
      LDA DP        NO
      JSB DSRCH     FIND NEXT DELIMITING CHARACTER
FMT3  CCA           INITIALIZE
      STA DPFLG       FIXED FLAG
      STA EFLAG       FLOATING FLAG 
      INA 
      STA NUM1        PRE-DECIMAL POINT D COUNTER 
      STA NUM2        POST-DECIMAL POINT D COUNTER
      STA SBD         S BEFORE D COUNTER
      STA SAD         S AFTER D COUNTER 
      STA SNFLG       SIGN FLAG 
      STA NAD         POST-DECIMAL ZERO COUNTER 
      STA NBD         PRE-DECIMAL POINT DIGIT COUNTER 
      INA 
      STA REPCT       REPETITION COUNT
      LDA IFSS        FORMAT STACK
      STA FSP           POINTER 
      LDA FST       GET NON-DELIMITING
      STO             CHARACTER 
      JSB MCHAR         IGNORING BLANKS 
      CPA DP        IS IT A DELIMITER ? 
      JSB FERRS,I   YES 
      CPA .+42B     IS IT A QUOTE?
      RSS           YES 
      JMP FMT0      NO
      LDB DP
      CMB,INB       RESET 
      ADB FST         CHARACTER 
      ADB CC            COUNTER 
      STB CC
FMT16 ISZ FST       INCREMENT STRING POINTER
      LDA CC        ALL 
      CPA NCH        CHARACTERS USED ?
      JSB FERRS+2,I YES, ERROR
      ISZ CC        INCREMENT CHARACTER COUNTER 
      LDA FST 
      CLO           DON'T IGNORE BLANKS 
      JSB MCHAR     GET NEXT CHARACTER
      STA FSP,I     LOAD CHARACTER ONTO STACK 
      ISZ FSP       INCREMENT STACK POINTER 
      CPA .+42B     IS IT A " ? 
      RSS           YES 
      JMP FMT16     NO
      ISZ FST       INCREMENT STRING POINTER
      ISZ CC              AND CHARACTER COUNTER 
      LDA CC        ALL 
      CPA NCH        CHARACTERS USED ?
      JMP FMT46     YES 
      LDA FST       RESET 
      STA DP          DELIMITER 
      JSB DSRCH         POINTER 
      LDA FST       NEXT CHARACTER
      CPA DP          A DELIMITER ? 
      JMP FMT46     YES 
      STO           IGNORE BLANKS 
      JSB MCHAR     FETCH A FORMAT STRING CHARACTER 
      LDA FST       WOULD IT BE 
      CPB DP          A DELIMITER ? 
      JMP FMT46     YES 
      JSB FERRS+1,I NO, ERROR 
FMT0  CPA S         IS IT AN S ?
      JMP FMT14     YES 
      CPA .+56B     IS IT A . ? 
      JMP FMT9      YES 
      CPA E         IS IT AN E? 
      JMP FMT13     YES 
      JSB DIGCK     IS IT A DIGIT ? 
      JMP FMT6      NO
      STA REPCT     YES, STORE IN REPCT 
      ISZ FST       INCREMENT STRING POINTER
      LDA FST       NEXT CHARACTER
      CPA DP          A DELIMITER ? 
      JSB FERRS+3,I YES, ERROR
      STO           IGNORE BLANKS 
      JSB MCHAR     GET NEXT CHARACTER
      CPA DP        IS IT A DELIMITER ? 
      JSB FERRS+3,I 
      JSB DIGCK     IS IT A DIGIT ? 
      JMP FMT5      NO
      LDA REPCT     YES,
      STB REPCT       MULTIPLY PREVIOUS 
      MPY .+12B         DIGIT BY 10 
      ADA REPCT     ADD IN ONES DIGIT 
      STA REPCT 
      ISZ FST       INCREMENT STRING POINTER
      LDA FST       NEXT CHARACTER
      CPA DP        A DELIMITER ? 
      JSB FERRS+3,I YES, ERROR
      STO           IGNORE BLANKS 
      JSB MCHAR     GET NEXT CHARACTER
      CPA DP        IS IT A DELIMITER ? 
      JSB FERRS+3,I 
      JSB DIGCK     THIRD DIGIT ? 
      RSS 
      JSB FERRS+4,I YES, ERROR
FMT5  LDB REPCT 
      SZB,RSS       REPCT ZERO ?
      JSB FERRS+5,I YES 
      ADB M73        NO. GREATER
      SSB,RSS            THAN 72? 
      JSB FERRS+4,I YES 
      ADB ..73      RESTORE REPCT 
      CMB,INB       SET NUMBER FLAG 
      STB FSP,I     LOAD ONTO FORMAT STACK
      ISZ FSP       INCREMENT STACK POINTER 
FMT6  CPA .X        IS NEXT CHARACTER AN X ?
      JMP FMT8      YES 
      CPA .A        IS IT AN A ?
      JMP FMT10     YES 
      CPA D         IS IT A D ? 
      RSS           YES 
      JMP FMT15     NO
      LDB DPFLG     DPFLG = -1? 
      SZB 
      JMP FMT7      YES 
      LDB NUM2      ADD REPCT TO
      ADB REPCT       POST-DECIMAL
      STB NUM2          DIGIT COUNTER 
      JMP FMT8
FMT7  LDB NUM1      ADD REPCT TO
      ADB REPCT       PRE-DECIMAL 
      STB NUM1          DIGIT COUNTER 
FMT8  CLB,INB       REINITIALIZE
      STB REPCT       REPCT 
      STA FSP,I     LOAD CHARACTER ONTO STACK 
      ISZ FST       INCREMENT STRING POINTER
      ISZ FSP       AND STACK POINTER 
      LDA FST       NEXT CHARACTER
      CPA DP          A DELIMITER ? 
      JMP FMT08     YES 
      STO           IGNORE BLANKS 
      JSB MCHAR     GET NEXT CHARACTER
      CPA DP        IS IT A DELIMITER ? 
      RSS           YES 
      JMP FMT0
FMT08 LDB FSP 
      STB EST       SET END OF STACK MARK 
      JMP FMT18 
FMT9  ISZ DPFLG     DPFLG = -1 ?
      JSB FERRS+6,I NO
      JMP FMT8+2    YES 
FMT10 LDB SFLG      IS SFLG 
      SZB,RSS 
      ISZ SFLG      YES, INCREMENT IT 
      JMP FMT8      NO
FMT13 ISZ EFLAG     EFLAG= -1?
      JSB FERRS+7,I NO
      JMP FMT8+2    YES 
FMT14 LDB NUM1
      ADB NUM2      ANY D'S FOUND ? 
      SZB,RSS 
      ISZ SBD       NO, INCREMENT BEFORE COUNTER
      LDB SBD       ANY S'S BEFORE A D ?
      SZB,RSS 
      ISZ SAD       NO, INCREMENT AFTER COUNTER 
      JMP FMT8+2
FMT15 CPA .+50B     IS CHARACTER A ( ?
      JMP FMT95     YES 
      JSB FERRS+8,I NO, ILLEGAL CHARACTER 
FMT18 LDA IFSS      REINITIALIZE
      STA FSP         STACK POINTER 
      CCA               AND 
      STA REPCT           REPCT 
      ADA SFLG      SFLG = 1 ?
      SZA,RSS 
      JMP FMT24     YES 
      ADA .-2       SFLG = 3 ?
      SSA,RSS 
      JMP FMT25     YES 
      LDA NUM1      NO, ANY 
      ADA NUM2        D'S 
      SZA,RSS           FOUND ? 
      JMP FMT20     NO
      JSB EVEXP     EVALUATE EXPRESSION 
      JMP FMEND     NONE FOUND
      JSB FERRS+9,I STING--ERROR
      STA MANT1     IF NUMBER 
      STA NUMW1       SAVE HIGN MANTISSA
      JSB .FLUN     UNPACK NUMBER 
      STA EXP         AND SAVE THE EXPONENT 
      LDA MANT1     IS THE NUMBER NEGATIVE ?
      SSA,RSS 
      JMP FMT31     NO
      LDA .+55B     YES, SET SIGN TO MINUS
      STA SIGN        AND 
      CMB,CLE,INB       COMPLEMENT
      LDA MANT1 
      CMA           OVERFLOW FROM 
      SEZ,RSS         LOW MANTISSA ?
      JMP FMT31-3   NO
      INA           YES, OVERFLOW FROM
      SOS             HIGH MANTISSA ? 
      JMP FMT31-3   NO
      CLE,ERA       YES, SHIFT RIGHT
      ERB             AND 
      ISZ EXP           BUMP EXPONENT 
      NOP 
      STA MANT1 
      STA NUMW1     SAVE HIGH MANTISSA
      JMP *+3 
FMT31 LDA .+53B     SET SIGN
      STA SIGN        TO PLUS 
      STB MANT2 
      STB NUMW2     SAVE LOW MANTISSA 
      LDA EXP 
      STA EXPW        AND EXPONENT
      CLB,INB       SET EXPRESSION
      STB EC          FOUND FLAG
      LDA IHB         HOLDING BUFFER
      STA HBP           POINTER 
      LDA EFLAG     EFLAG 
      SZA,RSS         SET  ?
      JMP FMT62     YES 
      LDA DPFLG     DPFLG 
      SZA,RSS         SET 
      JMP FMT45     YES 
      JMP FMT30     NO
**                             ** 
***  OUTPUT A LITERAL STRING  *** 
**                             ** 
FMT46 LDA IFSS      RESET 
      STA FSP         STACK POINTER 
FMT47 LDA FSP,I     TOP OF STACK                 [B]
      CPA .+42B       A " ? 
      JMP FMT90     YES, DONE WITH THIS SPEC
      CPA .+16B     IS IT A PSEUDO-LINEFEED ? 
      LDA .+12B     YES, MAKE IT A LINEFEED 
      CPA .+17B     IS IT A PSEUDO CARRIAGE RETURN ?
      RSS           YES 
      JMP FMT48                                  [B]
      LDA .+23B     OUTPUT X-OFF AND             [B]
      JSB OUTCR                                  [B]
      LDA .+15B       CARRIAGE RETURN            [B]
FMT48 EQU *                                      [B]
      JSB OUTCR     NO, OUTPUT THE CHARACTER
      ISZ FSP       INCREMENT STACK POINTER 
      JMP FMT47     NO                           [B]
**                                  **
***  OUTPUT A BLANK SPECIFICATION  ***
**                                  **
FMT20 LDA FSP,I     LOAD TOP OF STACK 
      SSA,RSS       IS IT A NUMBER ?
      JMP FMT21     NO
      STA REPCT     YES, STORE NUMBER IN REPCT
      ISZ FSP       INCREMENT STACK POINTER 
      LDA FSP,I     LOAD NEW TOP OF STACK 
FMT21 CPA .X        IS IT AN X ?
      RSS           YES 
      JSB FERRS+8,I NO, ERROR 
      JSB OUTBL 
      CCA           REINITIALIZE
      STA REPCT       REPCT 
      LDA FSP       END 
      CPA EST         OF STACK
      JMP FMT90     YES 
      LDA FSP,I     LOAD NEW TOP OF STACK 
      JMP FMT20 
**                     ** 
***  OUTPUT A STRING  *** 
**                     ** 
FMT24 EQU * 
      JSB EVEXP     EVALUATE NEXT EXPRESSION
      JMP FMEND     NONE FOUND
FMT25 EQU * 
      CLB,INB,RSS   SET THE EXPRESSION
      JSB FERRS+14,I
      STB EC          FOUND FLAG
      LDA FSP,I     LOAD TOP OF STACK 
      SSA,RSS       IS IT A NUMBER ?
      JMP FMT26     NO
      STA REPCT     YES 
      ISZ FSP       INCREMENT STACK POINTER 
      LDA FSP,I     LOAD NEW TOP OF STACK 
FMT26 CPA .X        IS IT AN X ?
      RSS           YES 
      JMP FMT27     NO
      JSB OUTBL 
      JMP FMT28 
FMT27 CPA .A        IS IT AN A ?
      RSS           YES 
      JSB FERRS+9,I NO, ERROR 
      ISZ FSP       INCREMENT STACK POINTER 
FMT05 EQU * 
      JSB FSCH      FETCH STRING CHARACTER
      LDA BLANK     NO, FETCH A BLANK 
      CPA .+16B     IS IT A PSEUDO-LINEFEED ? 
      LDA .+12B     YES, MAKE IT A LINEFEED 
      CPA .+17B     IS IT A PSEUDO CARRIAGE RETURN ?
      RSS           YES 
      JMP FMT29     NO
      LDA .+23B     OUTPUT X-OFF AND             [B]
      JSB OUTCR                                  [B]
      LDA .+15B       CARRIAGE RETURN            [B]
FMT29 EQU * 
      JSB OUTCR     OUTPUT CHARACTER
      ISZ REPCT     REPCT USED UP ? 
      JMP FMT05     NO
FMT28 CCA           REINITIALIZE
      STA REPCT       REPCT 
      LDA FSP       END OF
      CPA EST         STACK ? 
      JMP FMT90 
      JMP FMT25     NO
**                                   ** 
***  PREPARE AN INTEGER FOR OUTPUT  *** 
**                                   ** 
FMT30 CLA           INITIALIZE PRE-DECIMAL POINT
      STA EXPON       DIGIT COUNTER 
      CCA 
      ADA EXP       EXPONENT ZERO OR NEGATIVE ? 
      SSA,RSS 
      JMP FMT32     NO
      LDA .+60B     YES, LOAD A 
      STA HBP,I       ZERO
      ISZ HBP       INCREMENT BUFFER POINTER
      CCA           NUMBER OF BUFFER WORDS
      STA NHBW        IS ONE
      JMP FMT33 
FMT32 JSB DTL1
      STA EXPON     SAVE NUMBER 
      STA NHBW        OF DIGITS 
      JSB GETDG     GET DIGIT 
      ADA .+60B     CONVERT TO ASCII
      STA HBP,I     STORE IN HOLD BUFFER
      ISZ HBP       ALL DIGITS
      ISZ EXPON       FOUND ? 
      JMP FMT32+3   NO
FMT33 LDA NUM1      COMPUTE NUMBER OF 
      ADA NHBW        LEADING BLANKS
      LDB SBD       ANY S'S 
      ADB SAD         FOUND ? 
      SZB 
      JMP FMT43     YES 
      LDB SIGN      NO, NUMBER POSITIVE ? 
      CPB .+53B 
      JMP FMT43     YES 
      ADA .-1       NO, SAVE ROOM 
      CLB,INB         FOR 
      STB SNFLG         PRINTING SIGN 
FMT43 SSA           NUMBER OF BLANKS NEGATIVE ? 
      JMP FMT80     YES 
      STA NBLK      NO
      JSB ROUND     ROUND NUMBER IN BUFFER
      RSS 
      JMP FMT80     NO ROOM FOR CARRY FROM ROUND
      LDB IHB       REINITIALIZE
      STB HBP         HOLD BUFFER POINTER 
**                                       ** 
***  OUTPUT NUMBER FROM HOLDING BUFFER  *** 
**                                       ** 
FMT34 LDA FSP,I     LOAD TOP OF FORMAT STACK
      CPA S         IS IT AN S ?
      RSS           YES 
      JMP FMT36     NO
      ISZ FSP       INCREMENT STACK POINTER 
      LDA SNFLG 
      SZA           SNFLG = 0 ? 
      JMP FMT59     NO, IGNORE THE S
      LDB SBD       YES, ANY S'S BEFORE A D ? 
      SZB 
      JMP FMT35     YES 
      LDA SIGN      NO, OUTPUT SIGN 
      JSB OUTCR       IMMEDIATELY 
      LDA .+2       SET SNFLG TO 2
      STA SNFLG 
      JMP FMT59 
FMT35 CCB 
      STB SNFLG     SET SNFLG TO -1 
      JMP FMT34 
FMT36 SSA,RSS       TOP OF STACK A NUMBER ? 
      JMP FMT06     NO
      STA REPCT     YES, STORE IN REPCT 
      ISZ FSP       INCREMENT STACK POINTER 
      LDA FSP,I     LOAD NEW TOP OF STACK 
FMT06 CPA .X        IS TOP AN X ? 
      RSS           YES 
      JMP FMT37     NO
      JSB OUTBL 
      CCA           REINITIALIZE
      STA REPCT       REPCT 
      JMP FMT59 
FMT37 CPA D         TOP OF STACK A D ?
      RSS           YES 
      JMP FMT57     NO
      ISZ FSP       INCREMENT STACK POINTER 
      CCA 
      ADA NBLK      NUMBER OF BLANKS > 0 ?
      SSA 
      JMP FMT07     NO
      LDA BLANK     YES, OUTPUT A 
      JSB OUTCR       BLANK 
      CCB           DECREMENT 
      ADB NBLK        BLANK 
      STB NBLK          COUNT 
      JMP FMT40 
FMT07 LDA NBLK      NUMBER OF BLANKS
      SSA             LESS THAN ZERO ?
      JMP FMT56     YES 
      CCA           NO, DECREMENT 
      STA NBLK        BLANK COUNT 
      CCB 
      CPB SNFLG     SNFLG = - 1 ? 
      JMP FMT02     YES 
      ADB SNFLG     SNFLG = 1 ? 
      SZB,RSS 
      JMP FMT40     YES 
      JMP FMT58     NO
FMT56 CCB 
      ADB SNFLG     SNFLG = 1 ? 
      SZB 
      JMP FMT58     NO
FMT02 LDA SIGN      YES, OUTPUT 
      JSB OUTCR       SIGN AND
      LDA .+2       SET SNFLG 
      STA SNFLG     TO 2
FMT58 LDA IHB       END 
      ADA .+46        OF
      CPA HBP           BUFFER ?
      JMP FMT59 
      LDA HBP,I     OUTPUT A
      JSB OUTCR       DIGIT 
      ISZ HBP       INCREMENT HOLD BUFFER POINTER 
      LDA EFLAG     IS THIS A 
      SZA             FLOATING POINT SPECIFICATION ?
      JMP FMT40     NO
      CLA,INA       YES, HAS THE DECIMAL POINT
      CPA DPFLG       BEEN FOUND YET ?
      JMP FMT40     YES 
      CCA           NO, DECREMENT 
      ADA EXPON       DECIMAL 
      LDB IHB,I     IS THE
      CPB .+60B       NUMBER ZERO?
      CLA           YES, ZERO EXPONENT
      STA EXPON         EXPONENT
