Logo 
Search:

Assembly Language Articles

Submit Article
Home » Articles » Assembly Language » GeneralRSS Feeds

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

Posted By: Easy Tutor     Category: Assembly Language     Views: 2878

An AL Program that lets the user type in an algebric expression, ending with a carriage return, that contains round
(parenthesis), square, and curly brackets. As the expression is being typed in, the program evaluates each character. If at any point the expression is incorrectly bracketed ( too many right brackets or a mismatch between left and right brackets), the program tells the user to start over. After the carriage return is typed, if the expression is correct, the program displays "expression is correct". If not, the programdisplays "too many left brackets". In both cases, the program ask the user if he or she wants to continue. If the user types 'Y', the program runs again. Your program does not need to store the input string, only check it for correctness.

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

 .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
       JE @END_INPUT              ; jump to label @END_INPUT if AL=CR

       CMP AL, "["                ; compare AL with "["
       JE @PUSH_BRACKET           ; jump to label @PUSH_BRACKET if AL="["

       CMP AL, "{"                ; compare AL with "{"
       JE @PUSH_BRACKET           ; jump to label @PUSH_BRACKET if AL="{"

       CMP AL, "("                ; compare AL with "("
       JE @PUSH_BRACKET           ; jump to label @PUSH_BRACKET if AL="("

       CMP AL, ")"                ; compare AL with ")"
       JE @ROUND_BRACKET          ; jump to label @ROUND_BRACKET if AL=")"
                                   
       CMP AL, "}"                ; compare AL with "}"
       JE @CURLY_BRACKET          ; jump to label @CURLY_BRACKET if AL="}"

       CMP AL, "]"                ; compare AL with "]"
       JE @SQUARE_BRACKET         ; jump to label @SQUARE_BRACKET if AL="]"

       JMP @INPUT                 ; jump to label @INPUT

       @PUSH_BRACKET:             ; jump label

       PUSH AX                    ; push AX onto the STACK
       INC CX                     ; set CX=CX+1
       JMP @INPUT                 ; jump to label @INPUT

       @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
       JL @RIGHT_BRACKETS         ; jump to label @RIGHT_BRACKETS if CX<0

       CMP DL, "("                ; compare DL with "("
       JNE @MISMATCH              ; jump to label @MISMATCH if DL!="("
       JMP @INPUT                 ; jump to label @INPUT
       
       @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
       JL @RIGHT_BRACKETS         ; jump to label @RIGHT_BRACKETS if CX<0

       CMP DL, "{"                ; compare DL with "{"
       JNE @MISMATCH              ; jump to label @MISMATCH if DL!="{"
       JMP @INPUT                 ; jump to label @INPUT

       @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
       JL @RIGHT_BRACKETS         ; jump to label @RIGHT_BRACKETS if CX<0

       CMP DL, "["                ; compare DL with "["
       JNE @MISMATCH              ; jump to label @MISMATCH if DL!="["
     JMP @INPUT                   ; jump to label @INPUT

     @END_INPUT:                  ; jump label

     CMP CX, 0                    ; compare CX with 0
     JNE @LEFT_BRACKETS           ; jump to label @LEFT_BRACKETS if CX!=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"
     JNE @EXIT                    ; jump to label @EXIT if AL!="Y"
     JMP @START                   ; jump to label @START

     @MISMATCH:                   ; jump label

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

     JMP @START                   ; jump to label @START

     @LEFT_BRACKETS:              ; jump label

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

     JMP @START                   ; jump to label @START

     @RIGHT_BRACKETS:             ; jump label

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

     JMP @START                   ; jump to label @START

     @EXIT:                       ; jump label

     MOV AH, 4CH                  ; return control to DOS
     INT 21H
   MAIN ENDP
 END MAIN

 ;**************************************************************************;
 ;**************************************************************************;
 ;------------------------------  THE END  ---------------------------------;
 ;**************************************************************************;
 ;**************************************************************************;
  
Share: 



Easy Tutor
Easy Tutor author of Program that lets the user type in an algebric expression, ending with a carriage return, that contains round (parenthesis), square, and curly........ is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in Assembly Language:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!