             ;******* Opcode 0x30 
             ;The DECI instruction. 
             ;Input format: Any number of leading spaces or line feeds are 
             ;allowed, followed by '+', '-' or a digit as the first character, 
             ;after which digits are input until the first nondigit is 
             ;encountered. The status flags N,Z and V are set appropriately 
             ;by this DECI routine. The C status flag is not affected. 
             ; 
             oldNZVC: .EQUATE 14         ;Stack address of NZVC on interrupt 
             ; 
             total:   .EQUATE 10         ;Cumulative total of DECI number 
             valAscii:.EQUATE 8          ;Value(asciiCH) 
             isOvfl:  .EQUATE 6          ;Overflow boolean 
             isNeg:   .EQUATE 4          ;Negative boolean 
             state:   .EQUATE 2          ;State variable 
             temp:    .EQUATE 0          
             ; 
             init:    .EQUATE 0          ;Enumerated values for state 
             sign:    .EQUATE 1          
             digit:   .EQUATE 2          
             ; 
FDC4  C000FE opcode30:LDA     0x00FE,i   ;Assert d, n, s, sf, x, sx, sxf 
FDC7  E1FC53          STA     addrMask,d 
FDCA  16FCCA          CALL    assertAd   
FDCD  16FD19          CALL    setAddr    ;Set address of trap operand 
FDD0  68000C          SUBSP   12,i       ;Allocate storage for locals 
FDD3  C00000          LDA     FALSE,i    ;isOvfl := FALSE 
FDD6  E30006          STA     isOvfl,s   
FDD9  C00000          LDA     init,i     ;state := init 
FDDC  E30002          STA     state,s    
FDDF  C00000          LDA     0,i        ;wordBuff := 0 for input 
FDE2  E1FC4F          STA     wordBuff,d 
             ; 
FDE5  49FC50 do:      CHARI   byteBuff,d ;Get asciiCh 
FDE8  C1FC4F          LDA     wordBuff,d ;Set value(asciiCH) 
FDEB  90000F          ANDA    0x000F,i   
FDEE  E30008          STA     valAscii,s 
FDF1  C1FC4F          LDA     wordBuff,d ;A = asciiCh throughout the loop 
FDF4  CB0002          LDX     state,s    ;switch (state) 
FDF7  1D              ASLX               ;An address is two bytes 
FDF8  05FDFB          BR      stateJT,x  
             ; 
FDFB  FE01   stateJT: .ADDRSS sInit      
FDFD  FE5B            .ADDRSS sSign      
FDFF  FE76            .ADDRSS sDigit     
             ; 
FE01  B0002B sInit:   CPA     '+',i      ;if (asciiCh == '+') 
FE04  0CFE16          BRNE    ifMinus    
FE07  C80000          LDX     FALSE,i    ;isNeg := FALSE 
FE0A  EB0004          STX     isNeg,s    
FE0D  C80001          LDX     sign,i     ;state := sign 
FE10  EB0002          STX     state,s    
FE13  04FDE5          BR      do         
             ; 
FE16  B0002D ifMinus: CPA     '-',i      ;else if (asciiCh == '-') 
FE19  0CFE2B          BRNE    ifDigit    
FE1C  C80001          LDX     TRUE,i     ;isNeg := TRUE 
FE1F  EB0004          STX     isNeg,s    
FE22  C80001          LDX     sign,i     ;state := sign 
FE25  EB0002          STX     state,s    
FE28  04FDE5          BR      do         
             ; 
FE2B  B00030 ifDigit: CPA     '0',i      ;else if (asciiCh is a digit) 
FE2E  08FE4C          BRLT    ifWhite    
FE31  B00039          CPA     '9',i      
FE34  10FE4C          BRGT    ifWhite    
FE37  C80000          LDX     FALSE,i    ;isNeg := FALSE 
FE3A  EB0004          STX     isNeg,s    
FE3D  CB0008          LDX     valAscii,s ;total := Value(asciiCh) 
FE40  EB000A          STX     total,s    
FE43  C80002          LDX     digit,i    ;state := digit 
FE46  EB0002          STX     state,s    
FE49  04FDE5          BR      do         
             ; 
FE4C  B00020 ifWhite: CPA     ' ',i      ;else if (asciiCh is not a space 
FE4F  0AFDE5          BREQ    do         
FE52  B0000A          CPA     '\n',i     ;or line feed) 
FE55  0CFF11          BRNE    deciErr    ;exit with DECI error 
FE58  04FDE5          BR      do         
             ; 
FE5B  B00030 sSign:   CPA     '0',i      ;if asciiCh (is not a digit) 
FE5E  08FF11          BRLT    deciErr    
FE61  B00039          CPA     '9',i      
FE64  10FF11          BRGT    deciErr    ;exit with DECI error 
FE67  CB0008          LDX     valAscii,s ;else total := Value(asciiCh) 
FE6A  EB000A          STX     total,s    
FE6D  C80002          LDX     digit,i    ;state := digit 
FE70  EB0002          STX     state,s    
FE73  04FDE5          BR      do         
             ; 
FE76  B00030 sDigit:  CPA     '0',i      ;if (asciiCh is not a digit) 
FE79  08FEC7          BRLT    deciNorm   
FE7C  B00039          CPA     '9',i      
FE7F  10FEC7          BRGT    deciNorm   ;exit normaly 
FE82  C80001          LDX     TRUE,i     ;else X := TRUE for later assignments
FE85  C3000A          LDA     total,s    ;Multiply total by 10 as follows: 
FE88  1C              ASLA               ;First, times 2 
FE89  12FE8F          BRV     ovfl1      ;If overflow then 
FE8C  04FE92          BR      L1         
FE8F  EB0006 ovfl1:   STX     isOvfl,s   ;isOvfl := TRUE 
FE92  E30000 L1:      STA     temp,s     ;Save 2 * total in temp 
FE95  1C              ASLA               ;Now, 4 * total 
FE96  12FE9C          BRV     ovfl2      ;If overflow then 
FE99  04FE9F          BR      L2         
FE9C  EB0006 ovfl2:   STX     isOvfl,s   ;isOvfl := TRUE 
FE9F  1C     L2:      ASLA               ;Now, 8 * total 
FEA0  12FEA6          BRV     ovfl3      ;If overflow then 
FEA3  04FEA9          BR      L3         
FEA6  EB0006 ovfl3:   STX     isOvfl,s   ;isOvfl := TRUE 
FEA9  730000 L3:      ADDA    temp,s     ;Finally, 8 * total + 2 * total 
FEAC  12FEB2          BRV     ovfl4      ;If overflow then 
FEAF  04FEB5          BR      L4         
FEB2  EB0006 ovfl4:   STX     isOvfl,s   ;isOvfl := TRUE 
FEB5  730008 L4:      ADDA    valAscii,s ;A := 10 * total + valAscii 
FEB8  12FEBE          BRV     ovfl5      ;If overflow then 
FEBB  04FEC1          BR      L5         
FEBE  EB0006 ovfl5:   STX     isOvfl,s   ;isOvfl := TRUE 
FEC1  E3000A L5:      STA     total,s    ;Update total 
FEC4  04FDE5          BR      do         
             ; 
FEC7  C30004 deciNorm:LDA     isNeg,s    ;If isNeg then 
FECA  0AFEE3          BREQ    setNZ      
FECD  C3000A          LDA     total,s    ;If total != 0x8000 then 
FED0  B08000          CPA     0x8000,i   
FED3  0AFEDD          BREQ    L6         
FED6  1A              NEGA               ;Negate total 
FED7  E3000A          STA     total,s    
FEDA  04FEE3          BR      setNZ      
FEDD  C00000 L6:      LDA     FALSE,i    ;else -32768 is a special case 
FEE0  E30006          STA     isOvfl,s   ;isOvfl := FALSE 
             ; 
FEE3  DB000E setNZ:   LDBYTEX oldNZVC,s  ;Set NZ according to total result: 
FEE6  980001          ANDX    0x0001,i   ;First initialize NZV to 000 
FEE9  C3000A          LDA     total,s    ;If total is negative then 
FEEC  0EFEF2          BRGE    checkZ     
FEEF  A80008          ORX     0x0008,i   ;set N to 1 
FEF2  B00000 checkZ:  CPA     0,i        ;If total is not zero then 
FEF5  0CFEFB          BRNE    setV       
FEF8  A80004          ORX     0x0004,i   ;set Z to 1 
FEFB  C30006 setV:    LDA     isOvfl,s   ;If not isOvfl then 
FEFE  0AFF04          BREQ    storeFl    
FF01  A80002          ORX     0x0002,i   ;set V to 1 
FF04  FB000E storeFl: STBYTEX oldNZVC,s  ;Store the NZVC flags 
             ; 
FF07  C3000A exitDeci:LDA     total,s    ;Put total in memory 
FF0A  E2FC55          STA     opAddr,n   
FF0D  60000C          ADDSP   12,i       ;Deallocate locals 
FF10  58              RET0               ;Return to trap handler 
             ; 
FF11  50000A deciErr: CHARO   '\n',i     
FF14  C0FF21          LDA     deciMsg,i  ;Push address of message onto stack 
FF17  E3FFFE          STA     -2,s       
FF1A  680002          SUBSP   2,i        
FF1D  16FFE2          CALL    prntMsg    ;and print 
FF20  00              STOP               ;Fatal error: program terminates 
             ; 
FF21  455252 deciMsg: .ASCII  "ERROR: Invalid DECI input\x00"
      ...
