ASCII Art Generator in Assembly Language

Creating an ASCII Art Generator in assembly language is a fascinating project that allows you to explore low-level programming and artistic expression. In this article, we’ll guide you through the process of building a basic ASCII Art Generator using x86 assembly language.

Prerequisites:

Before you start, ensure that you have the following prerequisites:

  1. A basic understanding of x86 assembly language.
  2. An x86 assembly development environment, such as NASM (Netwide Assembler).
  3. A text editor for writing assembly code.

Building the ASCII Art Generator:

The ASCII Art Generator we’re building will take user input and display corresponding ASCII characters based on the input.

section .data
    prompt db 'Enter a character (q to quit): ', 0
    output db 'ASCII value of input: ', 0
    newline db 10, 0
    quit db 'q', 0

section .bss
    user_input resb 1

section .text
    global _start

_start:
    ; Display a prompt
    mov eax, 4
    mov ebx, 1
    mov ecx, prompt
    mov edx, 30
    int 80h

    ; Read user input
    mov eax, 3
    mov ebx, 0
    mov ecx, user_input
    mov edx, 1
    int 80h

    ; Check if the user wants to quit
    mov esi, [user_input]
    mov edi, [quit]
    cmp byte [esi], [edi]
    je .done

    ; Display the ASCII value of the input character
    mov eax, 4
    mov ebx, 1
    mov ecx, output
    mov edx, 20
    int 80h

    mov eax, 4
    mov ebx, 1
    mov ecx, user_input
    mov edx, 1
    int 80h

    ; Display a newline character
    mov eax, 4
    mov ebx, 1
    mov ecx, newline
    mov edx, 1
    int 80h

    ; Repeat the process
    jmp _start

.done:
    ; Exit the program
    mov eax, 1
    mov ebx, 0
    int 80h

Explanation:

  1. We define data sections for storing strings, including prompts and user input.
  2. The _start label marks the beginning of our program.
  3. We display a prompt asking the user to enter a character.
  4. We read a single character from the user.
  5. We check if the user wants to quit by comparing the input with ‘q’.
  6. If the user doesn’t want to quit, we display the ASCII value of the input character and repeat the process.
  7. If the user wants to quit, the program exits.

Building and Running:

  1. Save the assembly code in a file, e.g., ascii_art.asm.
  2. Assemble the code using NASM:
nasm -f elf ascii_art.asm -o ascii_art.o

3. Link the object file to create an executable:

ld ascii_art.o -o ascii_art

4. Run the program:

./ascii_art

5. Follow the prompts to enter characters and view their ASCII values. Enter ‘q’ to quit.

This simple ASCII Art Generator in assembly demonstrates the basics of input/output and control flow. You can expand on this project to create more complex ASCII art based on user input.