How to Make a Simple Notes App in Assembly

In this super clear and detailed tutorial, we'll walk through the process of creating a basic notes application using assembly language. This guide is aimed at beginners, so we'll take it step by step and include helpful tips along the way.

Prerequisites

An assembler translates assembly code into machine code that your computer can execute directly. NASM is a popular choice for x86 assembly.

Steps

  1. Set up your development environment

    Install NASM and ld on your system if you haven't already. There are prebuilt binaries available for most operating systems.

  2. Create a new file for your assembly code

    Open your text editor and create a new file. Save it with a .asm extension, e.g., notes.asm.

  3. Define the data section

    In the data section, we'll declare the variables for our notes app. For simplicity, we'll store the notes as a fixed-size array of characters.

    section .data
        notes times 1000 db 0  ; Reserve 1000 bytes for notes
        input times 100 db 0   ; Reserve 100 bytes for user input 
    
  4. Implement the main logic

    In the text section, we'll write the main logic for our notes app. We'll use system calls to interact with the operating system for input/output.

    section .text
    global _start
    
    _start:
        ; Display a welcome message
        mov eax, 4            ; sys_write system call
        mov ebx, 1            ; stdout file descriptor
        mov ecx, welcome_msg  ; address of string to write
        mov edx, welcome_len  ; length of string
        int 0x80              ; invoke system call
    
        ; Main loop
        .loop:
            ; Display a prompt
            mov eax, 4
            mov ebx, 1
            mov ecx, prompt_msg
            mov edx, prompt_len
            int 0x80
    
            ; Read user input
            mov eax, 3        ; sys_read system call  
            mov ebx, 0        ; stdin file descriptor
            mov ecx, input    ; address to store input
            mov edx, 100      ; max length of input
            int 0x80
    
            ; Check for exit command
            mov eax, [input]
            cmp eax, 0x0A71   ; 'q' followed by newline 
            je .exit
    
            ; Append input to notes
            mov esi, input
            mov edi, notes
            call strcpy
    
            jmp .loop
    
        .exit:
            ; Exit the program
            mov eax, 1        ; sys_exit system call
            xor ebx, ebx      ; exit status 0
            int 0x80  
    
    ; String copy function    
    strcpy:
        .loop:
            mov al, [esi]   ; load a byte from source
            mov [edi], al   ; store the byte to destination
            inc esi         ; advance source pointer
            inc edi         ; advance destination pointer
            cmp al, 0       ; check for null terminator
            jne .loop       ; continue if not end of string
        ret
    
    section .data
        welcome_msg db "Welcome to the Notes App!", 0x0A, 0
        welcome_len equ $ - welcome_msg
    
        prompt_msg db "Enter a note (or 'q' to quit): ", 0 
        prompt_len equ $ - prompt_msg
    
    System calls are how a program requests a service from the operating system kernel. Different system calls have different interrupt numbers.
  5. Assemble and link your code

    Open a terminal and navigate to the directory containing your notes.asm file. Run the following commands:

    nasm -f elf notes.asm
    ld -m elf_i386 -o notes notes.o

    This will assemble your code and link it into an executable named notes.

  6. Run your notes app

    In the terminal, run your newly created executable:

    ./notes

    You should see the welcome message. Try entering some notes! Press 'q' and then Enter to quit the program.

Next Steps

Congratulations, you've created a basic notes app in assembly! Some ideas to extend it:

I hope this tutorial was helpful in introducing assembly language in a practical context. Keep coding and never stop learning!