Facebook
From sa, 7 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 392
  1. .model small               ; declaring this code will be consists of one data segment and one code segment
  2. .stack 100h                ; stack is initializeed to offset address at 100h
  3.  
  4. .data                      ; Data segment
  5.  
  6. n_line db 0ah,0dh,"$"      ; for new line
  7. msg1 db 10,13,"Balanced$"
  8. msg2 db 10,13,"Not Balanced$"
  9.  
  10. a1 dw 5bh ; '['
  11. a2 dw 5dh ; ']'
  12. b1 dw 7bh ; '{'
  13. b2 dw 7dh ; '}'
  14. c1 dw 28h ; '('
  15. c2 dw 29h ; ')'
  16.  
  17. flag db 0
  18. i db ?  
  19.  
  20. .code                      ; Code segment
  21.  
  22. main proc
  23.     mov ax,@data           ; copying starting address of data segment into ax register
  24.     mov ds,ax              ; by copying ax into ds we are initializing data segment
  25.        
  26.     mov cx,8               ; length is 8 fixed here    
  27.     mov i,0d               ; https://stackoverflow.com/questions/45904075/displaying-numbers-with-dos
  28.     xor ax,ax              ; clearing ax    
  29.     xor bx,bx              ; clearing bx
  30.     xor dx,dx              ; clearing dx
  31. @input:
  32.     cmp i,8d
  33.     jge @input_end
  34.                                                  
  35.         mov ah,1           ; taking input
  36.         int 21h
  37.        
  38.         mov ah,0           ; clearing previously stored 1 in ah
  39.         mov bp,sp          ; checking stack is empty or not using bp. it can be done sp in this code
  40.        
  41.         @if1:              ; in this @if1 level: checking if current input is opening bracket or not
  42.            cmp ax,a1
  43.            je @push_it
  44.            cmp ax,b1
  45.            je @push_it
  46.            cmp ax,c1
  47.            je @push_it
  48.            
  49.            jmp @if2
  50.         @push_it:          ; if  current input is opening bracket then push it
  51.             push ax
  52.             mov flag,1     ; and alter the default flag value
  53.        
  54.         @if2:
  55.              cmp bp,100h   ; checking if stack is empty or not
  56.              je @if2_end   ; if not empty then  
  57.              
  58.              @inner_if1:   ; in this @inner_if1 level, checking if top of the stack is co-responding closing bracket of '{' or not
  59.                 cmp ax,b2  ; top==}
  60.                 je @inner_if1_perform
  61.                 jne @inner_if2
  62.                 @inner_if1_perform:  
  63.                     pop bx       ; top is popped and storing it to bx
  64.                     cmp bx,b1
  65.                     jne @inner_push1
  66.                     je @inner_if2
  67.                    
  68.                     @inner_push1:
  69.                         push bx   ; if not matched, then that popped value should be pushed
  70.                  
  71.               @inner_if2:   ; in this @inner_if2 level, checking if top of the stack is co-responding closing bracket of '[' or not
  72.                  cmp ax,a2  ; top==]
  73.                  je @inner_if2_perform
  74.                  jne @inner_if3
  75.                  @inner_if2_perform:
  76.                     pop bx       ; top is popped and storing it to bx
  77.                     cmp bx,a1
  78.                     jne @inner_push2
  79.                     je @inner_if3
  80.                    
  81.                     @inner_push2:
  82.                         push bx   ; if not matched, then that popped value should be pushed
  83.                  
  84.               @inner_if3:   ; in this @inner_if3 level, checking if top of the stack is co-responding closing bracket of '(' or not
  85.                  cmp ax,c2  ; top== )
  86.                  je @inner_if3_perform
  87.                  jne @inner_if3_end
  88.                  @inner_if3_perform:
  89.                     pop bx        ; top is popped and storing it to bx
  90.                     cmp bx,c1
  91.                     jne @inner_push3
  92.                     je @inner_if3_end
  93.                    
  94.                     @inner_push3:
  95.                         push bx    ; if not matched, then that popped value should be pushed
  96.                        
  97.                @inner_if3_end:                        
  98.         @if2_end:
  99.     inc i
  100.     jmp @input  
  101.    
  102. @input_end:                ; if ( (flag == 1) && (stack.empty()) )
  103.     cmp flag,1
  104.     je @next_level:
  105.     jne @print_msg2
  106.    
  107.     @next_level:          
  108.     cmp sp,100h            ; cheking the stack pointer is returned to initial address or not means empty or not
  109.     je @print_msg1
  110.     jne @print_msg2                            
  111.  
  112.     @print_msg1:
  113.         lea dx,msg1  
  114.         mov ah,9
  115.         int 21h
  116. @stop:    
  117.     mov ah,4ch             ; terminate the code
  118.     int 21h    
  119. main endp                  ; ending of main procedure
  120.  
  121. @print_msg2:
  122.     lea dx,msg2  
  123.     mov ah,9
  124.     int 21h  
  125.    
  126.     jmp @stop  
  127. end main                   ; ending of code segment
  128.  
  129. ;    Input:    
  130. ;    [[]({})] ------ Balanced
  131. ;    {({}[])} ------ Balanced
  132. ;    []({)}[] ------ Not Balanced
  133. ;    }()[(]){ ------ Not Balanced