NASA

A Handbook for Assembly | Chapter 2: Assemblers and Linkers



 

Chapter 2: Assemblers and Linkers


### 2.1 Introduction


In the realm of assembly language programming, assemblers and linkers play pivotal roles in transforming human-readable code into machine-executable binaries. This chapter provides an in-depth exploration of these essential tools and their functions in the development process.


### 2.2 Assemblers


An assembler is a program that converts assembly language code into machine code or an intermediate form known as object code. This process involves translating mnemonic instructions and symbolic addresses into the binary language understood by the computer's CPU.


#### 2.2.1 NASM (Netwide Assembler)


- **Overview:**

  - NASM is a widely used assembler for x86 and x86-64 architectures.

  - It supports various output formats, including ELF (Executable and Linkable Format) and others.


- **Installation:**

  - Use the package manager on your system:

    ```bash

    # For Linux

    sudo apt-get install nasm


    # For macOS (using Homebrew)

    brew install nasm

    ```


- **Usage:**

  - Assemble an assembly file (`program.asm`) to produce object code (`program.o`):

    ```bash

    nasm -f elf -o program.o program.asm

    ```


### 2.3 Linkers


Once assembly code is assembled into object code, the linker combines it with other necessary components (libraries, object files) to produce the final executable file. Linking resolves addresses and dependencies, creating a coherent program ready for execution.


#### 2.3.1 GNU Linker (ld)


- **Overview:**

  - The GNU linker (`ld`) is a common linker used in conjunction with GNU Assembler (GAS).

  - It performs various tasks, including symbol resolution and address binding.


- **Installation:**

  - Included with the GNU Binutils package. Install it using your system's package manager.


- **Usage:**

  - Link object files (`program.o`) to produce an executable (`program`):

    ```bash

    ld -m elf_i386 -s -o program program.o

    ```


### 2.4 Object Files


Object files are the intermediate output of the assembly process. They contain machine code in a format that is not directly executable. Object files can be:


- **Relocatable Object Files:**

  - Contain machine code with unresolved addresses.

  - Used as input to the linker.


- **Executable Object Files:**

  - Result from linking, with resolved addresses.

  - Directly executable by the computer.


### 2.5 Hello, Assembly World! (Revisited)


Let's revisit the "Hello, Assembly World!" program, taking into account the assembly, assembly, and linking processes.


#### 2.5.1 Assembly (NASM):


```assembly

section .data

    hello db 'Hello, Assembly World!',0


section .text

    global _start


_start:

    ; write the string to stdout

    mov eax, 4

    mov ebx, 1

    mov ecx, hello

    mov edx, 22

    int 0x80


    ; exit the program

    mov eax, 1

    xor ebx, ebx

    int 0x80

```


#### 2.5.2 Assemble (NASM):


```bash

nasm -f elf -o program.o program.asm

```


#### 2.5.3 Link (GNU Linker):


```bash

ld -m elf_i386 -s -o program program.o

```


#### 2.5.4 Run:


```bash

./program

```


### 2.6 Conclusion


Understanding the roles of assemblers and linkers is foundational for assembly language programmers. As you progress, these tools will continue to be integral components of your development workflow. In the next chapter, we'll delve into advanced assembly language programming concepts and techniques.


Comments

Houston We Have a Podcast

Translate