+1 (315) 557-6473 

Check If A String Is Balanced (Different Types Of Brackets) Using Flat Assembler Assignment Solution


Instructions

Objective
Write an assembly language assignment, to read a string of various parentheses and check if those parentheses, brackets, and braces are balanced in an equation.

Requirements and Specifications 

The algorithm below can be used to check if parenthesis, brackets, and braces are balanced in an equation.
Balanced input strings:
  1. (([()]))
  2. (()[]{[]})
  3. {([]([{({{}})([()])[][]}])[](()){()})}
Unbalanced input strings:
  1. ) (
  2. [ (])
  3. (([] {})
Screenshots of output
Check if string is balanced using in Flat Assembler

Check if string is balanced using in Flat Assembler 1

Check if string is balanced using in Flat Assembler 2

Check if string is balanced using in Flat Assembler 3

Check if string is balanced using in Flat Assembler 4

Check if string is balanced using in Flat Assembler 5

Source Code

format PE console

entry _start

include 'win32a.inc'

header: DB "This program checks if the parentheses, brackets, and braces are balanced.", 10, 13, 0

prompt:      DB "Please enter a string: ", 10, 13, 0

balanced:      DB "This string is balanced.", 0

notbalanced:     DB "This string is not balanced.", 0

newline:      DB 10, 13, 0

string:      DB 100 DUP(?)

section '.text' code readable executable 

_start:

    push ebp

    mov ebp, esp

    ; print header message

    push header      ;load address of message

    call [printf]      ; print the string

    add esp, 4      ;restore stack

    ; prompt user to enter a string

    push prompt      ;load address of prompt

    call [printf]     ;print the string

    add esp, 4     ; restore stack

    ;read the string

    push [_iob]      ; pass stdin

    mov eax, 100     ; max string size

    push eax      ; pass max length

    push string     ; pass string address

    call [fgets]     ; read the string

    add esp, 12      ; restore stack

    ;check parentheses, brackets and braces

    mov esi, string     ; point to start of string

chkloop:

    mov al,[esi]      ; load character from string

    cmp al,13      ; if end of string

    je endloop

    cmp al, '('     ; if opening

    je pushOpen     ; push to stack

    cmp al, '['     ; if opening

    je pushOpen     ; push to stack

    cmp al, '{'      ; if opening

    je pushOpen     ; push to stack

    cmp al, ')'      ; if closing

    je checkParen     ; check parenthesis

    cmp al, ']'     ; if closing

    je checkBracket      ; check bracket

    cmp al, '}'     ; if closing

    je checkBrace     ; check brace

    jmp next     ; else advance to next

pushOpen:

    push eax     ; save opening character

    jmp next     ; continue to next char

checkParen:

    cmp esp, ebp      ; check if stack is empty

    je error     ; if empty, is an error

    pop eax      ; else, pop character

    cmp al, '('     ; check if is opening parenthesis

    jne error      ; if not, is not balanced

    jmp next      ; else, continue to next character

checkBracket:

    cmp esp, ebp     ; check if stack is empty

    je error     ; if empty, is an error

    pop eax      ; else, pop character

    cmp al, '['     ; check if is opening bracket

    jne error     ; if not, is not balanced

    jmp next     ; else, continue to next character

checkBrace:

    cmp esp, ebp      ; check if stack is empty

    je error     ; if empty, is an error

    pop eax     ; else, pop character

    cmp al, '{'     ; check if is opening brace

    jne error      ; if not, is not balanced

next:

    inc esi      ; advance to next character in string

    jmp chkloop      ; continue

    endloop:

    cmp esp, ebp      ; check if stack is empty

    jne error      ; if not empty, is an error

    ; Print the balanced result message:

    push balanced     ; load address of the balanced message

    call [printf]      ; print the string

    add esp, 4      ; restore stack

    jmp endprog      ; end program

    error: 

    ; print the not balanced message

    push notbalanced      ; load address of unbalanced message

    call [printf]      ; print message

    add esp, 4      ; restore stack

    endprog: 

    push newline     ; load address of newline

    call [printf]     ; print newline

    add esp, 4     ; restore stack

    call [getchar]     ; wait until user presses a key 

     mov esp, ebp

    pop ebp

    call [ExitProcess]     ; exit the program

   ; Import section  

section '.idata' import data readable

  library kernel32, 'kernel32.dll', \

        msvcrt,'msvcrt.dll'

import kernel32, \

       ExitProcess,'ExitProcess'

  import msvcrt, \

        printf, 'printf', fgets, 'fgets', \

        getchar, 'getchar', _iob, '_iob'