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

June 29, 2024
Rehana Magnus
Assembly Language
Rehana Magnus, PhD in Computer Science from the esteemed Acadia Institute of Technology, Canada. With 6 years of experience, specializes in assembly language programming. Proficient in low-level coding, optimizing performance, and enhancing system functionality.
Key Topics
• Instructions
• Requirements and Specifications
Tip of the day
News

## 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

Source Code

`format PE consoleentry _startinclude 'win32a.inc'header: DB "This program checks if the parentheses, brackets, and braces are balanced.", 10, 13, 0prompt:      DB "Please enter a string: ", 10, 13, 0balanced:      DB "This string is balanced.", 0notbalanced:     DB "This string is not balanced.", 0newline:      DB 10, 13, 0string:      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 stringchkloop:    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 nextpushOpen:    push eax     ; save opening character    jmp next     ; continue to next charcheckParen:    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 charactercheckBracket:    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 charactercheckBrace:    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 balancednext:    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'`

## Related Samples

Explore our Assembly Language Assignments sample section, where we specialize in solving programming challenges with tailored solutions for students. Dive into detailed examples for MIPS, x86, or ARM assembly to deepen your understanding and excel in your assignments.