-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.asm
252 lines (230 loc) · 3.84 KB
/
parser.asm
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
model tiny
.386
.code
org 100h
locals @@
@entry:
jmp @start
@start:
call parseCommandLineArgs
cmp al, 0FFh
je @@failRet
call checkKeysValues
cmp al, 0FFh
je @@failRet
mov ah, 02h
mov dl, [KeyValues + 01h]
add dl, '0'
int 21h
mov dl, ' '
int 21h
mov dl, [KeyValues + 02h]
add dl, '0'
int 21h
mov dl, ' '
int 21h
mov dl, [KeyValues + 03h]
add dl, '0'
int 21h
ret
@@failRet:
mov ah, 02h
mov dl, '!'
int 21h
ret
MaxSnakeStartLength equ 09h
checkKeysValues proc
xor ax, ax
mov al, [KeyValues + 01h]
cmp ax, MaxSnakeStartLength
jg @@endFail
xor al, al
cmp al, [KeyValues + 01h]
jne @@skipLength
mov al, 03h
mov [KeyValues + 01h], al
@@skipLength:
mov al, [KeyValues + 02h]
cmp al, 02h
jg @@endFail
mov al, 02h
cmp [KeyValues + 02h], al
jne @@skipCollision
mov al, 03h
mov [KeyValues + 02h], al
@@skipCollision:
xor al, al
cmp [KeyValues + 03h], al
jne @@skipFood
mov al, 01h
mov [KeyValues + 03h], al
@@skipFood:
ret
@@endFail:
mov al, 0FFh
ret
endp
;----------DFA states enum------------
WrongState db 00h
InitialState db 01h
SlashState db 02h
KeyWithValState db 03h
KeyOnlyState db 04h
DigitState db 05h
;----------DFA states enum------------
StatesCount equ 06d
KeysCount equ 04h
KeyValues db KeysCount dup (0)
LastKey db 05h
Sygma equ 016d
Alphabet db '/?lsf 0123456789'
Terminal db StatesCount dup (0)
traslateSymbol proc
push si cx
lea si, Alphabet
mov cx, Sygma
@@whileAlNotEqualToCsSi:
cmp al, [si]
je @@found
inc si
loop @@whileAlNotEqualToCsSi
mov al, 0FFh
jmp @@end
@@found:
sub si, offset Alphabet
mov ax, si
@@end:
pop cx si
ret
endp
DFA db Sygma * StatesCount dup (0)
initDFA proc
xor ax, ax
cld
lea si, DFA
add si, Sygma
;InitialState
mov al, [SlashState] ;by slash
mov [si], al
mov al, [InitialState] ;by whitespace
mov [si + 05h], al
add si, Sygma
;SlashState
mov al, [KeyOnlyState]
mov [si + 01h], al
mov di, si ;by keys with values
add di, 02h
mov cx, KeysCount - 01h
mov al, [KeyWithValState]
rep stosb
add si, Sygma
;KeyWithValState
mov al, [KeyWithValState] ;by whitespace
mov [si + KeysCount + 01h], al
mov di, si ;by digits
add di, KeysCount + 02h
mov al, [DigitState]
mov cx, 0ah
rep stosb
add si, Sygma
;KeyOnlyState
mov al, [InitialState]
mov [si + KeysCount + 01h], al
add si, Sygma
;DigitState
mov al, [InitialState] ;by whitespace
mov [si + KeysCount + 01h], al
mov di, si ;by digits
add di, KeysCount + 02h
mov al, [DigitState]
mov cx, 0ah
rep stosb
;
lea si, Terminal
mov al, 01h
mov [si + 01h], al ;q1, q4, q5
mov [si + 04h], al
mov [si + 05h], al
ret
endp
parseArgs proc
lea di, DFA
add di, Sygma
xor dx, dx
mov dl, 01h
mov si, 80h
xor cx, cx
mov cl, [si]
test cl, cl
jz @@end
inc si
@@loop:
lodsb
call traslateSymbol
cmp al, 0FFh
je @@endFail
call processSymbol ;
call getDFAstate ;to dx
mov al, dl
mov ah, Sygma
mul ah
lea di, DFA
add di, ax
loop @@loop
@@end:
lea bx, Terminal
add bx, dx
mov al, [bx]
test al, al
jz @@endFail
ret
@@endFail:
mov al, 0FFh
ret
endp
processSymbol proc ;al = symbol dx = state ;al <- Sygma if wrong
push ax bx cx
test al, al ;slash
jz @@end
cmp al, KeysCount + 01h ;whitespace
je @@end
cmp al, KeysCount
jg @@digit
cmp al, 01h
je @@help
dec al
mov [LastKey], al
jmp @@end
@@digit:
mov cx, ax
sub cx, KeysCount + 02h
xor bx, bx
mov bl, [LastKey]
add bx, offset KeyValues
mov al, [bx]
mov ah, 0Ah
mul ah
add ax, cx
mov [bx], ax
jmp @@end
@@help:
mov [KeyValues], al
@@end:
pop cx bx ax
ret
endp
getDFAstate proc
push bx
xor dx, dx
mov bx, ax
add bx, di
mov dl, [bx]
pop bx
ret
endp
parseCommandLineArgs proc
call initDFA
call parseArgs
ret
endp
end @entry