Problem description:<\/strong> For example:<\/strong> Solution:<\/strong> cr equ 0DH ; cr represents carriage return msg DB cr,lf,’ENTER AN ALGEBRIC EXPRESSION : ‘,cr,lf,’$’ .code <\/p>\n main proc<\/p>\n mov ax,@data ;get data segment start: xor cx,cx ;initializing cx input: ;this label for taking input <\/p>\n int 21h <\/p>\n cmp al,0Dh ;checking if the enter is pressed or not ;if left bracket,then push on stack ;if right bracket,then pop stack<\/p>\n cmp al, ")" push_data: parentheses: pop dx curly_braces: line_bracket: end_input: mov ah, 9 lea dx, msg_continue mov ah,1 cmp al, "Y" mismatch: many_left: many_right: exit: MAIN ENDP Difficulties & Discussion: Wrong code [\/code]<\/p>\n For the example (a+b)} This problem is an application of stack implementation and we have successfully solved this using stack in assembly language. <\/p>\n","protected":false},"excerpt":{"rendered":" Problem description: This is a problem in elementary algebra is to decide if a given algebric expression containing several kinds of brackets,such as [,],{,},(,), is correctly bracketed or not. For example: (a+[b-{c*(d-e)}]+f) is correctly bracketed but (a+[b-{c*(d-e)]}+f) is not correctly bracketed. Solution: This is the case if (a) There are the same number of left … <\/p>\n
\nThis is a problem in elementary algebra is to decide if a given algebric expression containing several kinds of brackets,such as [,],{,},(,), is correctly bracketed or not. <\/p>\n
\n(a+[b-{c*(d-e)}]+f) is correctly bracketed but (a+[b-{c*(d-e)]}+f) is not correctly bracketed.<\/p>\n
\nThis is the case if
\n(a)\tThere are the same number of left and right bracketes of each kinds,and
\n(b)\tWhen a right bracket appears,the most recent preceeding unmatched left bracket should be of the same type.
\n This can be decided by using stack. The expression is scanned left to right. When a left bracket is encountered,it is pushed onto the stack. When a right bracket is encountered, the stack is poped (if the stack is empty ,there are too many right brackets) and the brackets are compared. If they are of the same type,the scanning continues.If there is a mismatch,the expression is incorrectly bracketed. At the end of the expression ,if the stack is empty ,the expression is correctly bracketed otherwise there are too many left brackets.
\n[code]
\n.model small
\n.stack 100h
\n.data<\/p>\n
\n lf equ 0AH ; lf represents line feed <\/p>\n
\n msg_correct DB cr,lf,’EXPRESSION IS CORRECT.$’
\n msg_left_bracket DB cr,lf,’TOO MANY LEFT BRACKETS. BEGIN AGAIN!’,cr,lf,’$’
\n msg_right_bracket DB cr,lf,’TOO MANY RIGHT BRACKETS. BEGIN AGAIN!’,cr,lf,’$’
\n msg_mismatch DB cr,lf,’BRACKET MISMATCH. BEGIN AGAIN!’,cr,lf,’$’
\n msg_continue DB cr,lf,’Type Y if you want to Continue : ‘,cr,lf,’$’<\/p>\n
\n mov ds,ax ;initialising<\/p>\n
\n lea dx,msg ;user prompt
\n mov ah,9
\n int 21h<\/p>\n
\n mov ah,1 <\/p>\n
\n JE end_input <\/p>\n
\n cmp al, "["
\n JE push_data
\n cmp al, "{"
\n JE push_data
\n cmp al, "("
\n JE push_data<\/p>\n
\n JE parentheses
\n cmp al, "}"
\n JE curly_braces
\n cmp al, "]"
\n JE line_bracket
\n jmp input<\/p>\n
\n push ax
\n inc cx
\n jmp input<\/p>\n
\n dec cx
\n cmp cx,0
\n JL many_right <\/p>\n
\n cmp dl, "("
\n JNE mismatch
\n JMP input <\/p>\n
\n dec cx
\n cmp cx,0
\n JL many_right
\n pop dx
\n cmp dl, "{"
\n JNE mismatch
\n JMP input <\/p>\n
\n dec cx
\n cmp cx, 0
\n JL many_right
\n pop dx
\n cmp dl, "["
\n JNE mismatch
\n JMP input <\/p>\n
\n cmp cx, 0
\n JNE many_left <\/p>\n
\n lea dx, msg_correct
\n int 21h <\/p>\n
\n int 21h<\/p>\n
\n int 21h <\/p>\n
\n JNE exit
\n JMP start <\/p>\n
\n lea dx, msg_mismatch
\n mov ah,9
\n int 21h
\n JMP start <\/p>\n
\n lea dx, msg_left_bracket
\n mov ah,9
\n int 21h
\n JMP start <\/p>\n
\n lea dx, msg_right_bracket
\n mov ah,9
\n int 21h
\n JMP start <\/p>\n
\n mov ah,4ch
\n int 21h<\/p>\n
\n END MAIN
\n[\/code]<\/p>\n
\nSince I have already solved this problem in C, I hadn\u2019t face too much problem for the algorithm to solve this in assembly language. All dificulties I faced was in the syntax of Assembly language. At first,finishing the code,it wasn\u2019t working for some inputs like (a+b)) or (a+b)} for which output will be \u201cTOO MANY RIGHT BRACKETS\u201d. But my code was being redirected to starting position of main procedure because of my silly mistake.Thsi mistake was in line number 83,94 and 104.<\/p>\n
\n[code] pop dx
\n dec cx
\n cmp cx,0
\n JL many_right
\n cmp dl, "{"
\n JNE mismatch
\n JMP input[\/code]
\nCorrected code:
\n[code]
\ndec cx
\n cmp cx,0
\n JL many_right
\n pop dx
\n cmp dl, "{"
\n JNE mismatch
\n JMP input<\/p>\n
\nWhen the last \u2018}\u2019 is scanned,then stack is empty.but in the code I have poped from stack,which is an invalid instruction. But actually it is required that,I will first check if the stack is empty or not.If empty,then \u201ctoo many right brackets\u201d. If not then pop dx and should be checked with the scanned data.It was done in the right side code.<\/p>\n