
      HED RENUMBER
* THE RENUMBER ROUTINE IS CALLED BY A USER TO ASSIGN NEW
* SEQUENCE NUMBERS TO ALL OR A PART OF A PROGRAM.  THE FORMAT OF
* THE COMMAND IS: 
*     REN-M,N,P,Q 
* M IS THE NEW INITIAL SEQUENCE NUMBER
* N IS THE NEW INCREMENT
* P IS THE FIRST STATEMENT TO BE RENUMBERED 
* Q IS THE LAST STATEMENT TO BE RENUMBERED. 
* DEFAULT VALUES ARE ASSIGNED AS FOLLOWS: 
*     M=10, N=10, P=FIRST STEATMENT, Q=LAST STATEMENT 
* 
      SPC 2 
      ORG LIBRA 
      LDA MLINK+1 
      ADA .+?PROG-?LINK 
      LDB 0,I 
      CPB PBUFF     NULL PROGRAM? 
      JMP LLEND     YES 
      JSB RDPRG     READ IN USER PROGRAM. 
      LDB MLINK+1   COMPUTE 
      ADB .-?LINK 
      LDA CFLAG       PROGRAM 
      AND 1,I 
      LDB PBPTR         STATE AND 
      STA RENC
      SZA                 END OF PROGRAM
      LDB SYMTB 
      STB RENEN             POINTER 
* 
*  SET UP INITIAL SEQUENCE NUMBER AND INCREMENT 
* 
      LDA SPROG 
      STA RENBA     INITIALIZE
      STB RENLA 
      LDA .+10        DEFAULT 
      STA RENM
      STA RENN          VALUES
      JSB RENUM     PARAMETER?
      JMP REN1      NO
      STB RENM      YES, RECORD IT
      CPA .+15B     END OF RECORD?
      JMP REN1      YES 
      CPA .+54B     NO, COMMA?
      JSB RENUM     DEMAND SECOND 
      JMP RENF1     NONE FOUND
      STB RENN      RECORD IT 
      CPA .+15B     END OF RECORD?
      JMP REN1      YES 
      CPA .+54B     NO, COMMA?
      JSB RENUM     YES, DEMAND THIRD 
      JMP RENF1     NOT FOUND 
      STB RENBA     RECORD IT 
      CPA .+15B     END OF RECORD?
      JMP REN22     YES 
      CPA .+54B     NO, COMMA?
      JSB RENUM     DEMAND FOURTH 
      JMP RENF1     NONE FOUND
      STB RENLA     RECORD IT 
      CPA .+15B     END OF RECORD?
      RSS 
      JMP RENF1     NO, ERROR 
      CMB           RENLA GREATER 
      ADB RENBA       THAN OR EQUAL 
      SSB,RSS           TO RENBA? 
      JMP RENF1     NO, FAIL
* 
*  FIND THE LAST SEQUENCE NUMBER LESS THAN
*  OR EQUAL TO RENLA, AND SET RENLA TO POINT TO IT
* 
      LDB SPROG     START WITH FIRST PROGRAM WORD 
      STB RENL
REN20 EQU * 
      CPB RENEN     PROGRAM EXHAUSTED?
      JMP REN21     YES 
      LDA B,I       CURRENT SEQUENCE
      CMA,INA         NUMBER GREATER
      ADA RENLA         THAN RENLA? 
      SSA 
      JMP REN21     YES 
      STB RENL      NO, SAVE POINTER TO STATEMENT 
      LDA B 
      INA           SET (B) TO
      ADB A,I         NEXT PROGRAM
      JMP REN20         STATEMENT 
REN21 EQU * 
      LDB RENL
      CPB SPROG     IF RENLA < FIRST STATEMENT
      JMP LLEND       NUMBER OF PROGRAM, WE ARE DONE
      STB RENLA 
* 
*  FIND THE FIRST SEQUENCE NUMBER GREATER THAN
*  OR EQUAL TO RENBA, AND SET RENBA TO POINT TO IT
* 
REN22 EQU * 
      LDA RENBA 
      CMA,INA 
      STA RENBA 
      LDB SPROG     START WITH FIRST WORD OF PROGRAM
      STB RENL
REN23 EQU * 
      CPB RENEN     PROGRAM EXHAUSTED?
      JMP REN24     YES 
      LDA B,I       CURRENT SEQUENCE
      ADA RENBA       NUMBER GREATER THAN 
      SSA,RSS           OR EQUAL TO RENBA?
      JMP REN24     YES 
      LDA B         NO, 
      INA             SET (B) 
      STB RENL          TO NEXT 
      ADB A,I             PROGRAM 
      JMP REN23             STATEMENT 
REN24 EQU * 
      CPB RENEN     SEQ NBR BEYOND PROGRAM? 
      JMP REN13     YES, SO FINISHED
      STB RENBA 
      CPB SPROG     1ST STATEMENT 
      JMP REN29     YES                          [B]
* 
*  TEST FOR SEQUENCE NUMBER OVERLAP AT RENBA
* 
      LDA RENM      LOAD NEW BA SEQ NBR 
      CMA,INA       GET OLD 
      ADA RENL,I      SEQUENCE NUMBER 
      SSA,RSS       OVERLAP?
      JMP RENF2     YES 
REN29 LDA RENBA                                  [B]
      CMA,INA       RENLA GREATER                [B]
      ADA RENLA       THAN OR EQUAL              [B]
      SSA               TO RENBA?                 [B
      JMP RENF1     NO, FAIL                     [B]
* 
*  GET NUMBER OF STATEMENTS IN PROGRAM
* 
REN1  EQU * 
      CLA           INITIALIZE
      STA RENNS       COUNTER 
      LDB SPROG 
REN27 EQU * 
      CPB RENEN     DONE? 
      JMP REN2      YES 
      LDA B         ADVANCE 
      INA             TO
      ADB A,I           NEXT
      ISZ RENNS           STATEMENT 
      JMP REN27 
* 
*  GET THE NUMBER OF STATEMENTS FROM RENBA TO RENLA 
* 
REN2  EQU * 
      CLA           INITIALIZE
      STA RENL        STATEMENT COUNTER 
      LDB RENBA     STARTING STATEMENT
REN25 EQU * 
      CPB RENLA     PROGRAM EXHAUSTED?
      JMP REN26     YES 
      LDA B         NO
      INA           SET (B) 
      ADB A,I         TO NEXT 
      ISZ RENL          PROGRAM 
      JMP REN25           STATEMENT 
* 
*  TEST FOR SEQUENCE NUMBER OVERLAP AT RENLA
* 
REN26 EQU * 
      LDA RENL      COMPUTE 
      LDB RENLA       NEW 
      CPB RENEN         HIGHEST 
      ADA .-1             SEQUENCE
      MPY RENN              NUMBER
      SSA,RSS 
      SZB 
      JMP RENF2     OVERFLOW
      ADA RENM
      STA RENL      SAVE IT 
      ADA MAXSN     IN
      SSA,RSS         LEGAL RANGE?
      JMP RENF2     NO
      LDA RENLA     YES 
      CPA RENEN 
      JMP REN3
      LDB A         MOVE
      INB             POINTER TO
      ADA B,I           NEXT STATEMENT
      CPA RENEN     BEYOND PROGRAM? 
      JMP REN3      YES, DON'T BOTHER ABOUT OVERLAP 
      LDB A,I       GET FOLLOWING 
      CMB,INB         OLD SEQ NBR 
      ADB RENL      TEST FOR OVERLAP
      SSB,RSS 
      JMP RENF2     FAIL
REN3  LDB RENC      PROGRAM 
      SZB            COMPILED?
      JMP REN9      YES 
* 
      LDA RENNS     GET # OF STATEMENTS IN PROGRAM. 
      DIV .+32      DIVIDE INTO 32 PARTS. 
      CMB           SET B=-1-#OF OVERSIZE GROUPS, 
      SZA,RSS        BUT IF <32 STATEMENTS USE -#.
      INB 
      INA           SET A TO SIZE OF LARGER GROUP.
      STA RENC1     SET COUNTER.
      STB RENC2 
      LDB ERSCA     SET INITIAL POINTER TO ERSEC. 
      STB RENSN 
* 
* NOW BUILD A TABLE IN ERSEC HAVING TH FOLLOWING STRUCTURE: 
*     ERSEC(0:31) ARE SEQUENCE NOS. OF STATEMENTS WHICH DIVIDE
* THE PROGRAM INTO 32 ALMOST EQUAL PARTS. ERSEC(32:63) ARE THE ABSO-
* LUTE ADDRESSES OF THESE STATEMENTS. ERSEC(0) IS THE SEQ.NO. OF THE
* FIRST STATEMENTS. 
* 
      LDA SPROG 
REN15 ADB .+32
      STA 1,I       SET ABSOLUTE ADDRESS INTO TABLE.
      LDB 0,I       SET SEQUENCE # INTO TABLE.
      STB RENSN,I 
      ISZ RENSN     BUMP POINTERS.
* 
      ISZ RENC2     TEST FOR ANY MORE OVERSIZE
      CLB,RSS        GROUPS.
      CCB           COMPUTE SIZE OF NEXT GROUP. 
      ADB RENC1 
      STB RENC1 
      CMB 
      STB RENC3 
* 
      LDB RENSN     TEST FOR DONE.
      CPB ERS32 
      JMP REN14 
* 
      ISZ RENC3     COMPUTE 1ST STATEMENT IN
      INA,RSS        NEXT GROUP.
      JMP REN15 
      ADA 0,I 
      ADA .-1 
      JMP *-5 
* 
* SCAN THROUGH PROGRAM FOR SEQUENCE NUMBER REFERENCES.
* FOR EACH ONE, DETERMINE ABSOLUTE ADDRESS OF THE LABEL 
* AND REPLACE IT WITH THAT ADDRESS. IF LABEL IS NONEXISTENT,
* PLACE THE NEGATIVE LABEL IN TO INDICATE THIS FACT.
* 
REN14 LDA SPROG     INITIALIZE POINTERS 
      STA RENQ       FOR RENSK. 
      ADA .-1 
      STA RENP
* 
REN4  JSB RENSK     GET NEXT STATEMENT REFERENCE. 
      JMP REN9      NONE LEFT.
* 
      LDB ERSCA 
REN7  LDA 1,I       FIRST STATEMENT >=SOUGHT STATE.?
      CMA 
      ADA RENP,I
      SSA,INA 
      JMP REN5      FOUND ONE.
      INB 
      CPB ERS32     TEST FOR DONE.
      RSS 
      JMP REN7
* 
      ADB .+31
      LDA PBPTR     STATEMENT IS IN LAST GROUP. 
      JMP REN6
REN5  ADB .+32      B=>FIRST STATEMENT IN GROUP.
      SZA,RSS       TEST FOR FOUND. 
      JMP REN18 
      CPB ERS32     TEST FOR NOT THERE. 
      JMP REN17 
      LDA 1,I       SET A=>FIRST WORD BEYOND GROUP. 
      ADB .-1         AT FIRST WORD BEYOND GROUP. 
REN6  LDB 1,I 
      STA RENC1     SAVE END TEST.
      LDA RENP,I    GET SEQUENCE NUMBER.
REN16 INB           BUMP STATEMENT POINTER. 
      ADB 1,I 
      ADB .-1 
      CPB RENC1     TEST FOR DONE.
      JMP REN17 
      CPA 1,I       TEST FOR FOUND. 
      JMP REN8
      JMP REN16 
* 
REN18 LDB 1,I 
      JMP *+3 
REN17 LDB RENP,I    SET BIT15 FOR 
      ADB BIT15      UNDEFINED LABELS.
REN8  STB RENP,I
      JMP REN4
* 
* NOW CHANGE ALL LABELS TO THEIR NEW VALUES.
* 
REN9  EQU * 
      LDA RENBA 
      LDB RENM
REN10 EQU * 
      CPA RENLA     DONE? 
      JMP REN28     YES 
      STB A,I       NO, RECORD NEW SEQUENCE NUMBER
      ADB RENN      INCREMENT TO NEXT LINE NUMBER 
      INA           MOVE
      ADA A,I         TO
      ADA .-1           NEXT
      JMP REN10           STATEMENT 
REN28 EQU * 
      CPA RENEN 
      RSS 
      STB A,I       RECORD LAST SEQ NBR 
* 
* SCAN REFERENCES AGAIN. IF ABSOLUTE ADDRESS IS THERE,
* REPLACE BY NEW LABEL. OTHERWISE REPLACE BY OLD LABEL. 
* 
      LDA RENC      IS PROGRAM
      SZA             COMPILED? 
      JMP REN13     YES 
      LDA SPROG 
      STA RENQ
      ADA .-1 
      STA RENP      SEEK EMBEDDED 
REN12 JSB RENSK       STATEMENT REFERENCES
      JMP REN13     NONE LEFT 
      LDA RENP,I    IF REFERENCE IS 
      RAL,CLE,SLA,ERA    NEGATIVE, CLEAR BIT 15.
      RSS                OTHERWISE, REPLACE IT. 
      LDA 0,I             WITH THE NEW
      STA RENP,I            LINE NUMBER 
      JMP REN12 
REN13 LDA MLINK+1   SET FLAG TO SAY 
      ADA .-?LINK     'USER PROGRAM TO CORE'
      STA MAIN
      JMP LLEND 
RENF2 EQU * 
      LDA LENT
      LDB REN0
      JMP LIBER 
REN0  DEF *+1 
      OCT 5123      LF-S
      ASC 16,EQUENCE NUMBER OVERFLOW/OVERLAP
* 
* RENUM SCANS INPUT BUFFER FOR A NUMBER. IF IT FINDS NOTHING
* IT RETURNS WITHOUT SKIPPING.IF IT FINDS A LEGAL LABEL 
* (1-9999) IT RETURNS WITH IT IN B AND THE NEXT CHARACTER IN A, 
* AND SKIPS ON THE WAY BACK. IF IT FINDS AN ERROR IT GOES TO RENF.
* 
RENUM NOP 
      CLA           INITIALIZE INTEGER
      STA RENL        TO ZERO 
      JSB LCHAR     ANY CHARACTERS? 
      JMP RENUM,I   NO
RENU1 ADA M72B      YES, IS IT
      SSA,RSS         < ASCII 72 ?
      JMP RENU2     NO
      ADA .+10      YES,
      SSA             > ASCII 57 ?
      JMP RENU2     NO
      LDB RENL      YES, PREVIOUS 
      ADB .1000       INTEGER 
      SSB,RSS           < 1000 ?
      JMP RENF1     NO
      LDB RENL      YES 
      RBL,RBL       MULTIPLY
      ADB RENL        BY
      RBL               10
      ADA 1         ADD IN
      STA RENL        NEW DIGIT 
      JSB LCHAR     FETCH NEXT
      JMP *+3         CHARACTER 
      JMP RENU1 
RENU2 ADA .+60B     RESTORE ANY LEGAL CHARACTER 
      LDB RENL
      ISZ RENUM 
      SZB           NON-ZERO INTEGER? 
      JMP RENUM,I   YES 
RENF1 LDA .-14
      LDB *+2 
      JMP LIBER 
      DEF *+1 
      OCT 5102      LF-B
      ASC 6,AD PARAMETER
RENSK NOP 
      ISZ RENP      INCREMENT POINTER 
      LDB RENQ
      ISZ RENR      <USING STATEMENT>?
      RSS           NO
      JMP RENS2     YES 
      CPB RENP      STATEMENT FINISHED? 
      JMP RENS2     YES 
RENS1 ISZ RENSK     NO, RETURN WITH RENP
      JMP RENSK,I     SET TO NEXT REFERENCE 
      STB RENQ      UPDATE TO NEXT STATEMENT
RENS2 CPB PBPTR     PROGRAM EXHAUSTED?
      JMP RENSK,I   YES 
      CLA           CLEAR USING 
      STA RENR        FLAG
      ISZ RENQ
      LDB RENQ
      ISZ RENQ      EXTRACT 
      LDA RENQ,I      STATEMENT 
      AND OPMSK         TYPE
      ADB 1,I       SET (B) TO
      ADB .-1         NEXT STATEMENT
      CPA RESOP     <RESTORE STATEMENT> ? 
      JMP RENS5     YES 
      CPA GOTOP     NO, <GOTO STATEMENT> ?
      JMP RENS3     YES 
      CPA GOSOP     NO, <GOSUB STATEMENT> ? 
      JMP RENS3     YES 
      CPA IFOP      NO, <IF STATEMENT>? 
      JMP RENS3-1   YES 
      CPA MTOPR     NO, <MAT STATEMENT>?
      RSS           YES 
      JMP RENS6     NO
      ISZ RENQ
      LDA RENQ,I    GET NEXT
      AND OPMSK       OPERATOR
RENS6 EQU * 
      CPA PRTOP     <PRINT STATEMENT>?
      RSS           YES 
      JMP RENS2-1   NO
      ISZ RENQ
      LDA RENQ,I    GET NEXT
      AND OPMSK       OPERATOR
      CPA USEOP     <USING STATEMENT? 
      RSS           YES 
      JMP RENS2-1   NO
      XOR RENQ,I    GET OPERAND 
      CPA INTFL     INTEGER FOLLOWS?
      RSS           YES 
      JMP RENS2-1   NO
      CCA           SET USING 
      STA RENR        FLAG
      LDA RENQ      SET 
      INA             REFERENCE 
      STA RENP          POINTER 
      STB RENQ      SET POINTER TO NEXT STATEMENT 
      JMP RENS1 
      LDA THNOP     LOAD 'THEN' 
RENS3 IOR INTFL     CREATE REFERENCE HEADER 
      STB RENQ      SET POINTER TO NEXT STATEMENT 
      ADB .-1       SET POINTER TO
RENS4 STB RENP        PROSPECTIVE REFERENCE 
      ADB .-1       PRECEDED BY 
      CPA 1,I         REFERENCE HEADER? 
      JMP RENS1     YES 
      LDA OFOP      NO, LOAD HEADER FOR 
      CPA 1,I 
      JMP RENS1 
      JMP RENS4       REFERENCE LIST
RENS5 CPA RENQ,I    ANY REFERENCE?
      JMP RENS2-1   NO
      JMP RENS3     YES 
RENL  EQU LTEMP 
RENM  EQU LTEMP+1 
RENN  EQU LTEMP+2 
RENC  EQU LTEMP+3 
RENP  EQU LTEMP+4 
RENQ  EQU LTEMP+5 
RENR  EQU LTEMP+6 
RENBA EQU LTEMP+7 
RENEN EQU LTEMP+8 
RENNS EQU LTEMP+9 
RENC1 EQU LTEMP+10
RENC2 EQU LTEMP+11
RENC3 EQU LTEMP+12
RENLA EQU LTEMP+13
RENSN EQU LTEMP+14
RESOP OCT 66000 
GOTOP OCT 52000 
GOSOP OCT 56000 
IFOP  OCT 53000 
MTOPR OCT 67000 
THNOP OCT 75000 
LENT  DEC -33 
.1000 DEC -1000 
$REN  EQU * 
      HED NAME
* THE NAME ROUTINE ALLOWS A USER TO ASSIGN HIS PROGRAM A NAME.
      SPC 1 
      ORG LIBRA 
      SPC 1 
      LDA MLINK+1   SET POINTER TO NAME.
      ADA .+?NAME-?LINK 
      STA LTEMP 
      STA LTEMP+3   SAVE NAME POINTER IN LTEMP+3. 
      LDA 0,I       SAVE RUN-ONLY BIT IN LTEMP+4. 
      AND BIT15 
      STA LTEMP+4 
      LDA .-3       SET COUNTER.
      STA LTEMP+1 
      CLA           SET FLAG TO SAY 
      STA LTEMP+2   FIRST CHARACTER 
NAME1 JSB NAMER     GET A CHAR. 
      ALF,ALF 
      STA LTEMP,I 
      JSB NAMER     GET 2ND CHAR. 
      IOR LTEMP,I 
      STA LTEMP,I 
      ISZ LTEMP     BUMP NAME POINTER.
      ISZ LTEMP+1   TEST FOR DONE.
      JMP NAME1 
      LDA LTEMP+3,I GET FIRST WORD OF NAME
      IOR LTEMP+4   MERGE IN RUN-ONLY BIT.
      STA LTEMP+3,I 
      JSB NAMER     TEST FOR ONLY 6 CHARS.
      CPA .+40B 
      JMP LLEND 
* 
      LDA NAM27 
      LDB *+2 
      JMP LIBER 
      DEF *+1 
      OCT 5117      LF-O
      ASC 13,NLY 6 CHARACTERS ACCEPTED
NAM27 DEC -27 
* 
NAMER NOP           GETS NEXT NAME CHARACTER. 
      JSB LCHAR 
      LDA .+40B     USE BLANK IF END OF LINE. 
      STA 1         HOLD IN B.
      AND .140      TEST FOR CONTROL CHAR.
      SZA 
      CPA .140
      JMP NAMER+1   SKIP CONTROL CHARS. 
      LDA 1         GET IN A AGAIN. 
      LDB LTEMP+2   GET 1ST CHAR FLAG.
      ISZ LTEMP+2 
      CPA .+44B     IF $
      RSS 
      CPA .+52B       OR IF * 
      SZB               AND FIRST CHARACTER 
      JMP NAME2 
* 
      LDA .-24      PRINT ERROR 
      LDB *+2 
      JMP LIBER 
      DEF *+1 
      OCT 5111      LF-I
      ASC 11,LLEGAL FIRST CHARACTER 
NAME2 CPA .+54B     IS CHARACTER A COMMA? 
      JMP NAME3     YES - ILLEGAL 
      CPA .+42B     IS CHARACTER A QUOTE? 
      JMP NAME3     YES - ILLEGAL 
      JMP NAMER,I   NO, RETURN CHARACTER
* 
NAME3 LDA LTEMP+4   ZERO OUT NAME EXCEPT
      STA LTEMP+3,I   FOR THE RUN-ONLY BIT
      JMP ILFER     OUTPUT ERROR MESSAGE
$NAM  EQU * 
