How do I Run an Assembly Language Program in Linux?

Running an Assembly language program in Linux involves a few key steps: writing the code, assembling it, linking it, and finally executing it. Here’s a step-by-step complete guide with examples using NASM (Netwide Assembler), a popular assembler for x86 architecture.

What You’ll Need

Before we begin, make sure you have:

  • Linux System: Any modern distribution like Ubuntu, Fedora, or Arch.
  • Terminal Access: Ability to open and use the terminal.
  • NASM Installed: The Netwide Assembler (NASM) is a popular assembler for x86 architecture.

What is NASM?

NASM stands for Netwide Assembler. It’s a popular assembler used to write and compile assembly language programs on Linux. NASM uses Intel-style syntax, which is relatively readable.

To install NASM:

sudo apt update
sudo apt install nasm

Step 1: Write Your Assembly Code

Start by creating a simple assembly file. Open your terminal and type:

program.asm

Then, paste the following code:

advertisement
section .data
    msg db "Hello, Sanfoundry!", 0xA
    len equ $ - msg
 
section .text
    global _start
 
_start:
    ; write syscall
    mov eax, 4        ; syscall number for sys_write
    mov ebx, 1        ; file descriptor 1 = stdout
    mov ecx, msg      ; pointer to message
    mov edx, len      ; message length
    int 0x80          ; interrupt to invoke syscall
 
    ; exit syscall
    mov eax, 1        ; syscall number for sys_exit
    xor ebx, ebx      ; exit code 0
    int 0x80

Save and exit: Press Ctrl+O, then Enter to save. Press Ctrl+X to exit.

Step 2: Assembling and Linking the Program

Assemble the Code:

Free 30-Day C Certification Bootcamp is Live. Join Now!

Use NASM to assemble the code into an object file:

nasm -f elf32 program.asm -o program.o

-f elf32 tells NASM to produce a 32-bit ELF object file.

Link the Object File

Now, use the ld linker to create the final executable:

ld -m elf_i386 program.o -o program

-m elf_i386 ensures compatibility with 32-bit code, even on 64-bit systems.

Step 3: Running the Program

Execute your program with:

./program

You should see:

advertisement
Hello, Sanfoundry!

Understanding the Code

  • section .data: Defines the data segment, where variables are declared.
  • msg db “Hello, Sanfoundry!”, 0xA: Declares a string with a newline character.
  • len equ $ – msg: Calculates the length of the message.
  • section .text: Defines the code segment.
  • global _start: Indicates the entry point of the program.
  • syscall: Invokes a system call.

Common Errors & Fixes

1. Error: nasm: command not found

Fix: Install NASM using your package manager:

sudo apt install nasm

2. Error: ld: cannot find -m elf_i386

Fix: Install 32-bit libraries:

sudo apt install gcc-multilib

Testing with Different Assemblers

While NASM is widely used, Linux also supports other assemblers like GAS (GNU Assembler). GAS uses AT&T syntax, which differs from NASM’s Intel syntax. If you’re interested in exploring GAS, you can write your code with .s extension and use as and ld for assembling and linking.

Tools to Enhance Your Assembly Programming

  • SASM: A simple IDE for assembly language with support for NASM, MASM, GAS, and FASM. It provides syntax highlighting and debugging features.
  • GDB: The GNU Debugger allows you to debug your assembly programs.
  • objdump: Displays information about object files, useful for inspecting the binary.

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.