Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/default-filters.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/default-filters.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-theme.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-theme.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-styles.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/class-wp-styles.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-request.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/block-supports/duotone.php on line 1

Warning: Uninitialized string offset 0 in /home/ujjal/public_html/blog/wp-includes/block-supports/duotone.php on line 1

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768

Warning: Cannot modify header information - headers already sent by (output started at /home/ujjal/public_html/blog/wp-includes/default-filters.php:1) in /home/ujjal/public_html/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1768
{"id":121,"date":"2011-10-17T11:59:21","date_gmt":"2011-10-17T05:59:21","guid":{"rendered":"http:\/\/ujjalruet.wordpress.com\/?p=121"},"modified":"2011-10-17T11:59:21","modified_gmt":"2011-10-17T05:59:21","slug":"stack-implementation-in-assembly-language-algebric-expression-is-correct-or-not","status":"publish","type":"post","link":"https:\/\/blog.ujjal.net\/?p=121","title":{"rendered":"Stack Implementation in Assembly Language (Algebric expression is correct or not.)"},"content":{"rendered":"

Problem description:<\/strong>
\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

For example:<\/strong>
\n(a+[b-{c*(d-e)}]+f) is correctly bracketed but (a+[b-{c*(d-e)]}+f) is not correctly bracketed.<\/p>\n

Solution:<\/strong>
\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

cr equ 0DH ; cr represents carriage return
\n lf equ 0AH ; lf represents line feed <\/p>\n

msg DB cr,lf,’ENTER AN ALGEBRIC EXPRESSION : ‘,cr,lf,’$’
\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

.code <\/p>\n

main proc<\/p>\n

mov ax,@data ;get data segment
\n mov ds,ax ;initialising<\/p>\n

start:
\n lea dx,msg ;user prompt
\n mov ah,9
\n int 21h<\/p>\n

xor cx,cx ;initializing cx
\n mov ah,1 <\/p>\n

input: ;this label for taking input <\/p>\n

int 21h <\/p>\n

cmp al,0Dh ;checking if the enter is pressed or not
\n JE end_input <\/p>\n

;if left bracket,then push on stack
\n cmp al, "["
\n JE push_data
\n cmp al, "{"
\n JE push_data
\n cmp al, "("
\n JE push_data<\/p>\n

;if right bracket,then pop stack<\/p>\n

cmp al, ")"
\n JE parentheses
\n cmp al, "}"
\n JE curly_braces
\n cmp al, "]"
\n JE line_bracket
\n jmp input<\/p>\n

push_data:
\n push ax
\n inc cx
\n jmp input<\/p>\n

parentheses:
\n dec cx
\n cmp cx,0
\n JL many_right <\/p>\n

pop dx
\n cmp dl, "("
\n JNE mismatch
\n JMP input <\/p>\n

curly_braces:
\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

line_bracket:
\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

end_input:
\n cmp cx, 0
\n JNE many_left <\/p>\n

mov ah, 9
\n lea dx, msg_correct
\n int 21h <\/p>\n

lea dx, msg_continue
\n int 21h<\/p>\n

mov ah,1
\n int 21h <\/p>\n

cmp al, "Y"
\n JNE exit
\n JMP start <\/p>\n

mismatch:
\n lea dx, msg_mismatch
\n mov ah,9
\n int 21h
\n JMP start <\/p>\n

many_left:
\n lea dx, msg_left_bracket
\n mov ah,9
\n int 21h
\n JMP start <\/p>\n

many_right:
\n lea dx, msg_right_bracket
\n mov ah,9
\n int 21h
\n JMP start <\/p>\n

exit:
\n mov ah,4ch
\n int 21h<\/p>\n

MAIN ENDP
\n END MAIN
\n[\/code]<\/p>\n

Difficulties & Discussion:
\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

Wrong code
\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

[\/code]<\/p>\n

For the example (a+b)}
\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

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