Hello World in Assembly Language

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:

  1. NASM Assembler: Download and install the Netwide Assembler (NASM) from the official website.
  2. A Linker: Use a linker like ld (on Linux) or an equivalent tool.
  3. A Text Editor: Use any text editor, such as Visual Studio Code, Sublime Text, or Notepad++.

Writing Your First Assembly Program

  1. Open your text editor.
  2. Create a new file and name it hello_world.asm.
  3. 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

  1. Save the file.
  2. Open a terminal or command prompt.
  3. Assemble the program using NASM:
nasm -f elf32 hello_world.asm -o hello_world.o
  1. Link the object file to create an executable:
ld -m elf_i386 -s -o hello_world hello_world.o
  1. 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!

Previous
Next

More Amazing Post

Get 15GB free hosting with Infinity Free

  • January 23, 2025

hosting is a compulsory thing for publish a website....

author-avatar
Posted By nazir

Programming Languages and Their Frameworks

  • January 20, 2025

Programming languages and frameworks go hand-in-hand to simplify and...

author-avatar
Posted By nazir

Uses of C#

  • January 20, 2025

C# (pronounced as “C-Sharp”) is a versatile and powerful...

author-avatar
Posted By nazir
Scroll to Top