NASM Getting Started Tutorial

Summary

This article describes the history and entry-level use of the NASM assembler.

Introduction to NASM

NASM (The Netwide Assembler) is a widely used assembler for x86 and x86-64 platforms. Its version history is as follows:

0.98 - Released in 1997, this version includes support for the x86 series processors, as well as some commonly used assembly instructions and macro definitions, becoming the first assembler capable of compiling the Linux kernel.
0.99 - Released in 1998, adding many new features, including support for the MMX instruction set.
0.98.39 - Released in 2000, became the last DOS-only version.
1.0rc1 - Released in 2002, added support for the Windows platform.
2.0 - Released in 2008, adding support for the x86-64 platform and improving the syntax.
2.11 - Released in 2014, adding many new features, including support for the AVX2 instruction set and compiler plugins.
2.14 - Released in 2017, added support for the AVX-512 instruction set and other improvements.

2.15 - Released in January 2021, it adds support for some of the latest instructions, adds support for LLVM IR and LLVM bitcode output formats, adds some command line options, and improves the flexibility and customizability of NASM. , the error information is more detailed and precise, which facilitates users to debug and eliminate errors, improves the speed and efficiency of assembly, and makes NASM faster when compiling large projects.

Currently, the latest version of NASM is 2.15, which has become one of the most widely used assemblers on x86 and x86-64 platforms. It can generate efficient machine code and relocatable object files.

Install NASM

First you need to install NASM in your development environment. You can use the package manager to install it on Linux, or you can download the installer from the official website on Windows.

Install NASM on Ubuntu:

sudo apt install nasm

Write assembly code

Write the assembly code using any text editor and save the file to .asmthe format. Here is a simple example:

section .data
    hello db 'Hello, world!',0xD,0xA,0

section .text
    global _start

_start:
    ; write the message
    mov eax, 4
    mov ebx, 1
    mov ecx, hello
    mov edx, 15
    int 0x80

    ; exit
    mov eax, 1
    xor ebx, ebx
    int 0x80

This code Hello, world!writes a string to standard output and exits the program.

assembly code

Open a terminal or command line interface, change to .asmthe directory where the files are stored, and run the following command:

nasm -f elf64 hello.asm

The above command will generate a elf64target file in the format hello.o.

Link object file

To convert an assembler into an executable file, the object files need to be linked into the executable file. ldThis can be done using the GNU linker . Run the following command in a terminal or command line interface:

ld -s -o hello hello.o

The above command will generate an executable file hello, which is the final version of the assembler.

Run program

In a terminal or command line interface, run the program with the following command:

./hello

will output Hello world!that the program has run successfully.

References

NASM Manual

Summarize

Learning NASM assembly language can help you deeply understand the underlying architecture and instruction set of the computer, so as to better understand the working principle of the computer system, and help you understand the process of how high-level language is converted into machine code, so as to better understand the work of high-level language. principle.

Guess you like

Origin blog.csdn.net/bigwave2000/article/details/132526594