00C7
00 | STACKADDR: word 0F000           ; beginning of stack area
F0 | 
00 | STR:       word 0E800
E8 | 
00 | GETS:      LDBI R0 0A           ; void gets (char *str)
11 | 
0A | 
20 | L_GETS:    INB R2 0001
81 | 
01 | 
00 | 
00 |            TSTI 0001     
84 | 
01 | 
00 | 
F6 |            JMPNZ L_GETS
C3 | 
21 |            STBR R2 R1    
33 | 
02 |            SUB R0 R2
41 | 
04 |            JMPZ END_GETS   
C2 | 
10 |            INC R1
48 | 
EC |            JMP L_GETS
C1 | 
20 | END_GETS:  LDBI R2 0
11 | 
00 | 
21 |            STBR R2 R1    
33 | 
00 |            RET
C9 | 
21 | PUTS:      LDBR R2 R1           ; void puts (char *str)
31 | 
0E |            JMPZ END_PUTS
C2 | 
20 | L_PUTS:    OUTB R2 0000
83 | 
00 | 
00 | 
00 |            TSTO 0000     
85 | 
00 | 
00 | 
F6 |            JMPNZ L_PUTS
C3 | 
10 |            INC R1
48 | 
EE |            JMP PUTS
C1 | 
20 | END_PUTS:  LDBI R2 0A
11 | 
0A | 
20 | LEND_PUTS: OUTB R2 0000
83 | 
00 | 
00 | 
00 |            TSTO 0000     
85 | 
00 | 
00 | 
F6 |            JMPNZ LEND_PUTS
C3 | 
00 |            RET
C9 | 
4F | OVFLMSG:   byte 4F              ; 'Overflow' (error message)
76 |            byte 76
65 |            byte 65
72 |            byte 72
66 |            byte 66
6C |            byte 6C
6F |            byte 6F
77 |            byte 77
00 |            byte 00
10 | OVERFLOW:  LDWI R1 OVFLMSG
10 | 
43 | 
00 | 
00 |            CALL PUTS
C8 | 
22 | 
00 | 
00 |            HLT
CF | 
F0 | ATOI:      LDBI R15 30          ; '0'
11 | 
30 | 
E0 |            LDBI R14 2D          ; '-'
11 | 
2D | 
00 |            LDWI R0 0
10 | 
00 | 
00 | 
21 | L_ATOI:    LDBR R2 R1
31 | 
2E |            SUB R2 R14           ; negative?
41 | 
02 |            JMPNZ LABS_ATOI
C3 | 
10 |            INC R1
48 | 
21 | LABS_ATOI: LDBR R2 R1
31 | 
20 |            JMPZ SIGN_ATOI
C2 | 
F2 |            SUB R15 R2
41 | 
04 |            MV R0 R4
04 | 
00 |            LSH R0               ; R0 * 10 = R0 + R0 + R0 << 3
4A | 
22 |            JMPC ATOI_OVFL
C6 | 
00 |            LSH R0
4A | 
1E |            JMPC ATOI_OVFL
C6 | 
00 |            LSH R0
4A | 
1A |            JMPC ATOI_OVFL
C6 | 
40 |            ADD R4 R0
40 | 
16 |            JMPC ATOI_OVFL
C6 | 
40 |            ADD R4 R0
40 | 
12 |            JMPC ATOI_OVFL
C6 | 
20 |            ADD R2 R0
40 | 
0E |            JMPC ATOI_OVFL
C6 | 
10 |            INC R1
48 | 
DC |            JMP LABS_ATOI
C1 | 
ED | SIGN_ATOI: MV R14 R13
04 | 
04 |            JMPNZ END_ATOI
C3 | 
0E |            SUB R0 R14           ; R14 here contains zero
41 | 
E0 |            MV R14 R0 
04 | 
00 | END_ATOI:  RET
C9 | 
00 | ATOI_OVFL: CALL OVERFLOW
C8 | 
4C | 
00 | 
00 |            HLT
CF | 
00 | INBUFADDR: word 0E000           ; beginning of input buffer area
E0 | 
10 | READNUM:   LDWA R1 INBUFADDR    ; int readnum (void)
20 | 
9C | 
00 | 
10 |            PUSH R1              
08 | 
00 |            CALL GETS            
C8 | 
04 | 
00 | 
10 |            POP R1
09 | 
00 |            CALL ATOI
C8 | 
56 | 
00 | 
00 |            RET
C9 | 
A1 | STRTRIM:   LDBR R10 R1           ; int strtrim (char *str, int len)
31 | 
11 |            JMPZ E_STRTRIM
C2 | 
23 |            MV R2 R3
04 | 
07 |            JMPNZ CONTINUE
C3 | 
A0 |            LDBI R10 0
11 | 
00 | 
A1 |            STBR R10 R1
33 | 
06 |            JMP E_STRTRIM
C1 | 
10 | CONTINUE:  INC R1
48 | 
20 |            DEC R2
49 | 
EB |            JMP STRTRIM
C1 | 
00 | E_STRTRIM: RET
C9 | 
F0 | START:     LDWA R15 STACKADDR   ; stack initialization
20 | 
00 | 
00 | 
F0 |            SPWR R15             ; NOTE: every function receives input
0E | 
10 |            LDWA R1 STR
20 | 
02 | 
00 | 
00 |            CALL GETS
C8 | 
04 | 
00 | 
00 |            CALL READNUM
C8 | 
9E | 
00 | 
02 |            MV R0 R2
04 | 
10 |            LDWA R1 STR
20 | 
02 | 
00 | 
00 |            CALL STRTRIM
C8 | 
B0 | 
00 | 
10 |            LDWA R1 STR
20 | 
02 | 
00 | 
00 |            CALL PUTS
C8 | 
22 | 
00 | 
00 |            HLT
CF | 
