-
Notifications
You must be signed in to change notification settings - Fork 0
/
brackets.asm
203 lines (155 loc) · 2.72 KB
/
brackets.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
.model small
.stack 100h
.386
.data
next_line db 10, 13, '$'
arr1 db 10, 13, 'Enter an Algebraic Expression: $'
arr2 db 10, 13, 'Too many right brackets begin again! $'
arr3 db 10, 13, '"Expression is correct."$'
arr4 db 10, 13, 'Type Y if you want to continue: $'
arr5 db 50 dup ('$') ; contains input string
arr6 db 10, 13, 'Too many left brackets begin again! $'
.code
main proc
mov ax, @data
mov ds, ax
start:
lea dx, arr1
mov ah,09h
int 21h
call take_input
mov bl, cl ;Bl contains the count of characrters in the input
call validate
printing:
cmp bh,'B'
je balance_print
cmp bh,'L'
je left_print
cmp bh, 'R'
je right_print
balance_print:
lea dx, arr3
mov ah,09h
int 21h
lea dx, arr4
mov ah,09h
int 21h
mov ah,01h
int 21h
cmp al,'Y'
je next_try
jmp terminate
left_print:
lea dx, arr6
mov ah,09h
int 21h
jmp next_try
right_print:
lea dx, arr2
mov ah,09h
int 21h
jmp next_try
next_try:
xor bx,bx
xor cx,cx
xor ax,ax
jmp start
terminate:
mov ah,4ch
int 21h
main endp
take_input proc
mov cl, 0
lea si, arr5
mov ah,0
again:
mov ah,01h
int 21h
cmp al,13
je return
mov [si],al
inc si
inc cl
jmp again
return:
ret
take_input endp
validate proc
lea si, arr5
mov cl, bl
again:
mov dl,[si]
cmp dl ,'}'
je check
cmp dl ,']'
je check
cmp dl ,')'
je check
cmp dl ,'('
je pushing
cmp dl ,'{'
je pushing
cmp dl ,'['
je pushing
jmp next
check:
; check if stack is empty so return many right brackets
cmp ch,0
je right
pop ax ; ax contain the top of the stack
dec ch
cmp dl ,'}'
je curly
cmp dl ,']'
je square
cmp dl ,')'
je round
next:
inc si
dec cl
jz balance
jmp again
pushing:
mov al, [si]
mov ah,0
push ax
inc ch
jmp next
round:
cmp al, '('
jne left
jmp next
curly:
cmp al,'{'
jne left
jmp next
square:
cmp al,'['
jne left
jmp next
balance:
cmp ch,0
je is_balance
mov bh,'L'
pop_extra:
pop ax
dec ch
jnz pop_extra
jmp return
is_balance:
mov bh,'B'
jmp return
right:
mov bh,'R'
je return
left:
mov bh,'L'
je return
return:
ret
validate endp
end main
; AX, USE FOR PUSH PULL
; BL CONTAINS INPUT LENGTH
; CL CONTAINS THE COUNT OF CHARACTERS VALIDATED (ITERATED)
; CH CONTAINS NUMBER OF BRACKETS IN THE stack