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.
Install NASM and ld on your system if you haven't already. There are prebuilt binaries available for most operating systems.
Open your text editor and create a new file. Save it with a .asm
extension, e.g., notes.asm
.
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
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
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
.
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.
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!