Welcome to the fascinating world of Assembly Language! Assembly is a low-level programming language that is closely tied to machine code. Writing in Assembly provides insight into how computers operate at a fundamental level.
In this post, we will guide you through writing your first “Hello, World!” program in Assembly Language. This example uses x86 architecture with the NASM assembler.
Setting Up Your Environment
Before writing any code, ensure you have the following:
- NASM Assembler: Download and install the Netwide Assembler (NASM) from the official website.
- A Linker: Use a linker like
ld
(on Linux) or an equivalent tool. - A Text Editor: Use any text editor, such as Visual Studio Code, Sublime Text, or Notepad++.
Writing Your First Assembly Program
- Open your text editor.
- Create a new file and name it
hello_world.asm
. - Add the following code:
section .data
hello db 'Hello, World!', 0 ; Null-terminated string
section .text
global _start ; Entry point for the program
_start:
; Write the string to stdout
mov eax, 4 ; System call number for sys_write
mov ebx, 1 ; File descriptor for stdout
mov ecx, hello ; Address of the string
mov edx, 13 ; Length of the string
int 0x80 ; Interrupt to invoke system call
; Exit the program
mov eax, 1 ; System call number for sys_exit
xor ebx, ebx ; Exit code 0
int 0x80 ; Interrupt to invoke system call
Explanation of the Code
section .data
: This section is used to define data, such as strings or constants.hello db 'Hello, World!', 0
: Defines a null-terminated string “Hello, World!”.section .text
: This section contains the code to be executed.global _start
: Declares the entry point of the program.mov
instructions: Load values into registers.int 0x80
: Triggers a system call.
Assembling and Running Your Program
- Save the file.
- Open a terminal or command prompt.
- Assemble the program using NASM:
nasm -f elf32 hello_world.asm -o hello_world.o
- Link the object file to create an executable:
ld -m elf_i386 -s -o hello_world hello_world.o
- Run the program:
./hello_world
You should see the output:
Hello, World!
Congratulations! You’ve successfully written and executed your first Assembly Language program.
What’s Next?
Now that you’ve written your first program, explore more Assembly concepts like loops, conditionals, and memory management. Understanding Assembly Language is a valuable skill that deepens your knowledge of computer architecture.
Happy coding!