# Program that lets the user type in an algebric expression, ending with a carriage return, that contains round (parenthesis), square, and curly........

.MODEL SMALL
.STACK 100H

.DATA
PROMPT          DB  0DH,0AH,\'Enter an Algebraic Expression : \',0DH,0AH,\'\$\'
CORRECT         DB  0DH,0AH,\'Expression is Correct.\$\'
LEFT_BRACKETS   DB  0DH,0AH,\'Too many Left Brackets.\$\'
RIGHT_BRACKETS  DB  0DH,0AH,\'Too many Right Brackets.\$\'
MISMATCH        DB  0DH,0AH,\'Bracket Mismatch. Begin Again.\$\'
CONTINUE        DB  0DH,0AH,\'Type Y if you want to Continue : \$\'

.CODE
MAIN PROC
MOV AX, @DATA                ; initialize DS
MOV DS, AX

@START:                      ; jump label

LEA DX, PROMPT               ; load and print the string PROMPT
MOV AH, 9
INT 21H

XOR CX, CX                   ; clear CX

MOV AH, 1                    ; set input function

@INPUT:                      ; jump label
INT 21H                    ; read a character

CMP AL, 0DH                ; compare AL with CR

CMP AL, \"[\"                ; compare AL with \"[\"

CMP AL, \"{\"                ; compare AL with \"{\"

CMP AL, \"(\"                ; compare AL with \"(\"

CMP AL, \")\"                ; compare AL with \")\"

CMP AL, \"}\"                ; compare AL with \"}\"

CMP AL, \"]\"                ; compare AL with \"]\"

@PUSH_BRACKET:             ; jump label

PUSH AX                    ; push AX onto the STACK
INC CX                     ; set CX=CX+1

@ROUND_BRACKET:            ; jump label

POP DX                     ; pop a value from STACK into DX
DEC CX                     ; set CX=CX-1

CMP CX, 0                  ; compare CX with 0

CMP DL, \"(\"                ; compare DL with \"(\"

@CURLY_BRACKET:            ; jump label

POP DX                     ; pop a value from STACK into DX
DEC CX                     ; set CX=CX-1

CMP CX, 0                  ; compare CX with 0

CMP DL, \"{\"                ; compare DL with \"{\"

@SQUARE_BRACKET:           ; jump label

POP DX                     ; pop a value from STACK into DX
DEC CX                     ; set CX=CX-1

CMP CX, 0                  ; compare CX with 0

CMP DL, \"[\"                ; compare DL with \"[\"

@END_INPUT:                  ; jump label

CMP CX, 0                    ; compare CX with 0

MOV AH, 9                    ; set string output function

LEA DX, CORRECT              ; load and print the string CORRECT
INT 21H

LEA DX, CONTINUE             ; load and print the string CONTINUE
INT 21H

MOV AH, 1                    ; set input function
INT 21H                      ; read a character

CMP AL, \"Y\"                  ; compare AL with \"Y\"

@MISMATCH:                   ; jump label

LEA DX, MISMATCH             ; load and display the string MISMATCH
MOV AH, 9
INT 21H

@LEFT_BRACKETS:              ; jump label

LEA DX, LEFT_BRACKETS        ; load and display the string LEFT_BRACKETS
MOV AH, 9
INT 21H

@RIGHT_BRACKETS:             ; jump label

LEA DX, RIGHT_BRACKETS       ; load and display the string RIGHT_BRACKETS
MOV AH, 9
INT 21H