-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalisis.asm
332 lines (285 loc) · 6.01 KB
/
analisis.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
;--------------------------------------------------------------
;------macros para leer analizar el archivo y ejectuarlo-------
;--------------------------------------------------------------
analisis macro
;Lectura del archivo
openFile file2, handler
readFile handler, fileData, SIZEOF fileData
closeFile handler
;Analisis
lexicalAnalysis
syntaxAnalysis
;Si todo esta bien lexicamente y sintacticamnete
;Imprimir lo que se leyo
print msgOp
print fileData
toPostfix
executeExpression
endm
lexicalAnalysis macro
LOCAL while, continue, finish, error
xor si, si
xor cx, cx
while:
cmp si, fileSize
je finish
mov cl, fileData[si]
;math symbols
cmp cl, '+'
je continue
cmp cl, '-'
je continue
cmp cl, '*'
je continue
cmp cl, '/'
je continue
;spaces
cmp cl, 32
je continue
;EFO (;)
cmp cl, ';'
je continue
;numbers
cmp cl, '0'
jl error
cmp cl, '9'
jg error
continue:
inc si
jmp while
error:
jmp lexicalError
finish:
endm
syntaxAnalysis macro
LOCAL while, finish, continue, symbol, continue, error, space
xor si, si
xor ax, ax ;flag for numbers
xor bx, bx ;flag for symbols
while:
cmp si, fileSize
je finish
mov cl, fileData[si]
;Symbols
cmp cl, '+'
je symbol
cmp cl, '-'
je symbol
cmp cl, '*'
je symbol
cmp cl, '/'
je symbol
;space
cmp cl, 32
je space
;EOF
cmp cl, ';'
je space
;check if numbers >= 2
cmp ax, 2
jge error
inc ax
mov bx, 0
jmp continue
symbol:
cmp ax, 1
je error
cmp bx, 1
je error
mov ax, 0
mov bx, 1
space:
cmp ax, 1
je error
continue:
inc si
jmp while
error:
jmp syntaxError
finish:
cmp cl, ';'
jne syntaxEOF
endm
toPostfix macro
LOCAL while, continue, finish, plus, minus
mov stackPost, 0
xor si, si
xor cx, cx ;sizeOf postfix string
xor bx, bx
while:
cmp si, fileSize
jge finish
mov bl, fileData[si]
;space
cmp bl, 32
je continue
cmp bl, ';'
je continue
;symbols
cmp bl, '+'
je plus
cmp bl, '-'
je minus
cmp bl, '*'
je times
cmp bl, '/'
je divs
;mov num to postfix
push si
mov si, cx
mov postfix[si],bl
pop si
inc cx
jmp continue
plus:
precedencia 1, '+'
jmp continue
minus:
precedencia 1, '-'
jmp continue
times:
precedencia 2, '*'
jmp continue
divs:
precedencia 2, '/'
jmp continue
continue:
inc si
jmp while
finish:
vaciarPila
endm
precedencia macro op, sym
LOCAL isEmpty, equal, greater, lower, finish
cmp stackPost, 0
je isEmpty
pop ax
cmp al, op
je equal
cmp al, op
jl greater
jmp lower
equal:;When is equal it's replaced
push si
mov si, cx
mov postfix[si], ah
pop si
mov al, op
mov ah, sym
push ax
inc cx
jmp finish
greater:;When is greater it's just added to the 'stack'
inc stackPost
push ax
mov al, op
mov ah, sym
push ax
jmp finish
lower:;When is lower it empty the stack
push ax
vaciarPila
isEmpty:
inc stackPost
mov al, op
mov ah, sym
push ax
finish:
endm
vaciarPila macro
LOCAL while, finish
while:
cmp stackPost, 0
je finish
pop bx
push si
mov si, cx
mov postfix[si], bh
pop si
inc cx
dec stackPost
jmp while
finish:
endm
executeExpression macro
LOCAL while, addition, finish, continue, substraction, is_negative, normal, fin
xor ax, ax ;
xor bx, bx ;actual char
xor cx, cx ;number
xor si, si
while:
int 3
xor ax, ax
mov ax, 10
cmp postfix[si], '$'
je finish
mov bl, postfix[si]
cmp bl, '+'
je addition
cmp bl, '-'
je substraction
cmp bl, '*'
je multiplication
cmp bl, '/'
je division
;then is a number
mov ch, postfix[si]
inc si
mov cl, postfix[si]
;cast number
sub ch, 48
mul ch ;res ax
xor ch, ch
sub cx, 48
add ax, cx
push ax
jmp continue
addition:
pop ax
mov cx, ax
pop ax
add ax, cx
push ax
jmp continue
substraction:
pop ax
mov cx, ax
pop ax
sub ax, cx
push ax
jmp continue
multiplication:
pop ax
mov cx, ax
pop ax
mul cx
push ax
jmp continue
division:
pop ax
mov cx, ax
pop ax
div cx
push ax
continue:
inc si
jmp while
finish:
print msgRes
pop ax
test ax, ax
js is_negative
jmp normal
is_negative:
neg ax
convertAscii ax, res
print minus
print res
print newLine
jmp fin
normal:
convertAscii ax, res
print res
print newLine
fin:
endm