-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.asm
More file actions
218 lines (199 loc) · 3.31 KB
/
parse.asm
File metadata and controls
218 lines (199 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
;
; Various parsing functions
;--------
; Parse up to 4 hex digits into a word.
; HL - points to first ascii digit
; On return HL points to next byte after digit
; Return word in BC
;----
; Carry set on invalid digit, HL points to invalid digit
PARSENUM:
#local
LD BC, 0
LD A, (HL)
CALL HEX2NYB
JR C, INVAL ; Must be at least 1 nybble
LD C, A
INC HL ; Nyb 2?
LD A, (HL)
CALL HEX2NYB
JR C, DONE ; If not a nybble
CALL SHIFTBCNYB ; Make room in BC
OR C ;
LD C, A ; Or in new nybble
INC HL ; Nyb 3?
LD A, (HL)
CALL HEX2NYB
JR C, DONE ; If not a nybble
CALL SHIFTBCNYB ; Make room in BC
OR C ;
LD C, A ; Or in new nybble
INC HL ; Nyb 4?
LD A, (HL)
CALL HEX2NYB
JR C, DONE ; If not a nybble
CALL SHIFTBCNYB ; Make room in BC
OR C ;
LD C, A ; Or in new nybble
SCF
DONE:
CCF
RET
INVAL:
SCF
RET
SHIFTBCNYB:
SLA C
RL B
SLA C
RL B
SLA C
RL B
SLA C
RL B
RET
#endlocal
RET
;--------
; Convert 4 ASCII hex digits to a word, must be uppercase
; HL - points to first ascii digit
; On return HL points to next byte after digit
; Return word in BC
;----
; Carry set on invalid digit, HL points to invalid digit
HEX2WORD:
#local
CALL HEX2BYTE
JR C, INVAL
LD B, A ; Save as high byte
CALL HEX2BYTE
JR C, INVAL
LD C, A ; Save as low byte
; Carry already clear
RET
INVAL:
SCF
RET
#endlocal
;--------
; Convert 2 ASCII hex digits to a byte, must be uppercase
; HL - points to first ascii digit
; On return HL points to next byte after digit
; Return byte in A
;----
; Carry set on invalid digit, HL points to invalid digit
HEX2BYTE:
#local
PUSH BC
LD A, (HL)
CALL HEX2NYB
JR C, INVAL
SLA A
SLA A
SLA A
SLA A ; Move to high nybble
LD B, A ; Save in B
INC HL
LD A, (HL) ; Read low nybble
CALL HEX2NYB ; Convert
JR C, INVAL
OR B ; Combine into one
POP BC
INC HL ; Point to next
; Carry already clear from OR
RET
INVAL:
POP BC
LD A, 0
SCF
RET
#endlocal
;--------
; Convert 1 ASCII hex digit to a nybble, must be uppercase
; A - ASCII Hex digit
; Carry set on invalid digit
HEX2NYB:
#local
CP '0' ; ch < 0
JR C, INVAL
CP '9'+1 ; 0 <= ch <= 9
JR C, DECI
CP 'A'
JR C, INVAL ; 9 < ch < 'A'
CP 'F'+1
JR NC, INVAL ; 'F' < ch
LETR: ; A-F
SUB 'A'-10 ; Make 'A' -> $0A
JR DONE
DECI: ; 0-9
SUB '0' ; Make '0' -> $00
JR DONE
INVAL:
LD A, $80 ; Set highbit if invalid
SCF
RET
DONE:
SCF
CCF
RET
#endlocal
;--------
; Increments HL till non-whitespace in (HL)
SKIPWHITE:
#local
LD A, (HL)
CALL ISWHITE
JR NZ, END
INC HL
JR SKIPWHITE
END:
RET
#endlocal
;----------
; Extract an argument string
; Replaces first whitespace character with NULL
; HL is set to 0 if end of string encountered
EXTRACTARG:
#local
LD A, (HL)
AND A
JR Z, ENDN ; Stop on NUL
CALL ISWHITE
JR Z, END ; Or whitespace
INC HL
JR EXTRACTARG
ENDN:
;LD HL, 0 ; Leave pointing to null instead
RET
END:
XOR A ; 0
LD (HL), A ; Null terminate
RET
#endlocal
;--------
; Is character in A whitespace? Sets Z if so
ISWHITE:
#local
CP ' ' ; Space
JR Z, END
CP $09 ; TAB
JR Z, END
CP $0B ; Vert Tab
JR Z, END
CP $0C ; Form Feed
END:
RET
#endlocal
;--------
; Convert character in A to uppercase if lowercase
TO_UPPER:
#local
CP 'a'
JR C, NOCHG ; ch < 'a'
CP 'z'+1
JR NC, NOCHG ; ch > 'z'
; 'a' <= ch <= 'z'
SUB $20 ; 'a' - 'A' convert to uppercase
NOCHG:
RET
#endlocal