Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Assembly Language - Environment Setup

Assembly Language - Environment Setup

This chapter guides you through setting up a NASM assembly development environment on a Linux system and completing your first assembly program.


Preparing the Linux environment

If you are using Windows, it is recommended to install WSL (Windows Subsystem for Linux) or use a virtual machine to get a Linux environment.

macOS users can install Linux in a virtual machine or use a Docker container.

All examples in this tutorial are based on Ubuntu/Debian systems; for other distributions, adjust the package manager commands yourself.


Installing the NASM assembler

On Ubuntu/Debian systems, install NASM using the apt package manager:



$ sudo apt update

$ sudo apt install nasm

After installation, verify that NASM was installed successfully:



$ nasm -version

NASM version 2.16.01

If you see a version number output, NASM has been installed successfully.


Installing the linker

The assembler produces an object file (.o file), which cannot be run directly.

You need a linker to convert the object file into an executable. We use the linker bundled with GCC:



$ sudo apt install gcc

Alternatively, you can use the GNU linker ld directly:



$ ld --version

In this tutorial we mainly use gcc to link, because it automatically handles some low-level details and is more suitable for beginners.


Installing the debugging tool GDB

GDB (GNU Debugger) is the most commonly used debugger on Linux; it can step through programs, view register state, and inspect memory contents.



$ sudo apt install gdb

Verify the GDB installation:



$ gdb --version

GNU gdb (Ubuntu 12.1-0ubuntu1) 12.1


The first assembly program: Hello, runoob

Create a new file hello.asm, enter the following content:

Example

; File path: hello.asm

; The first assembly program: print Hello, runoob!

; NASM syntax, Linux 32-bit

section

.data

                     

; Data segment: stores initialized data

    msg

db

'Hello, runoob!'

,

0xA

   

; Define the string msg, 0xA is the newline character

    len

equ

$

msg                

; Compute the string length: current address - msg start address

section

.text

                     

; Code segment: stores executable instructions

   

global

_start                  

; Declare _start as the program entry point

_start

:

                           

; Program entry

   

; System call sys_write (4): output a string to stdout

   

mov

eax

,

4

                     

; System call number 4 = sys_write

   

mov

ebx

,

1

                     

; File descriptor 1 = stdout (standard output)

   

mov

ecx

,

msg                    

; Address of the string to output

   

mov

edx

,

len                    

; String length

   

int

0x80

                       

; Trigger an interrupt to execute the system call

   

; System call sys_exit (1): exit the program normally

   

mov

eax

,

1

                     

; System call number 1 = sys_exit

   

mov

ebx

,

0

                     

; Return value 0 = normal exit

   

int

0x80

                       

; Trigger an interrupt to execute the system call


Compiling and running

The entire compile-and-run flow is shown in the figure:

Use the following commands to compile and run this program:



$ nasm -f elf32 hello.asm -o hello.o    # 汇编:将 .asm 转为 .o 目标文件

$ ld -m elf_i386 hello.o -o hello       # 链接:将 .o 转为可执行文件

$ ./hello                               # 运行程序

Hello, runoob!

The role of each of the three commands above:

Step Command Description
1. Assemble nasm -f elf32 hello.asm -o hello.o -f elf32 specifies the output format as 32-bit ELF, -o specifies the output file name
2. Link ld -m elf_i386 hello.o -o hello -m elf_i386 specifies 32-bit link mode, producing the executable hello
3. Run ./hello Execute the program in the current directory

Linking with GCC (recommended method)

If the ld configuration in your environment is complicated, you can use gcc to link, which automatically handles details such as the C runtime:



$ nasm -f elf32 hello.asm -o hello.o

$ gcc -m32 hello.o -o hello -nostartfiles

$ ./hello

Hello, runoob!

-nostartfiles The option tells GCC not to add the default startup code, because we defined our own _start entry point.


Code structure analysis

Let us understand the above program section by section:

Section Code Description
Data segment declaration section .data Declare the data segment, storing variables and constants
String definition msg db 'Hello, runoob!', 0xA db defines a byte sequence, 0xA is the ASCII code for newline
Length calculation len equ $ - msg $ represents the current address; subtract the msg address to get the string length
Code segment declaration section .text Declare the code segment, storing instructions
Entry declaration global _start Export _start as a global symbol, which the linker needs
Entry label _start: The location where the program starts executing

int 0x80 is the Linux 32-bit system call instruction. It triggers a software interrupt that lets the kernel execute the system call we specify through eax. This is the bridge between a user program and the operating system kernel.


Debugging with GDB

Add debugging information when compiling, then step through with GDB:



$ nasm -f elf32 -g hello.asm -o hello.o       # -g 添加调试信息

$ ld -m elf_i386 hello.o -o hello

$ gdb ./hello

(gdb) break _start                             # 在 _start 处设置断点

(gdb) run                                      # 运行程序

(gdb) stepi                                    # 单步执行一条指令

(gdb) info registers                           # 查看所有寄存器

(gdb) x/s msg                                  # 查看 msg 字符串内容

(gdb) quit                                     # 退出 GDB

GDB commands Function
break _start Set a breakpoint at the _start label
run Run the program, stopping at the breakpoint
stepi Single-step one machine instruction
info registers Display the current values of all registers
x/s 地址 View the contents at a specified address as a string

Common questions

If you compile 32-bit assembly on a 64-bit system, you need to install the 32-bit compatibility libraries:sudo apt install gcc-multilib

If ld reports the error "cannot find entry symbol _start" when linking, check whether your code has the correct global _start and whether the label name matches exactly (note the underscore).