NASA

A Handbook for Assembly | Chapter 2: Assembly Language Syntax


 


Chapter 2: Assembly Language Syntax


### 2.1 Introduction


Assembly language syntax is the set of rules and conventions that dictate how assembly code should be written. In this chapter, we'll explore the fundamental elements of assembly language syntax, which vary based on the architecture you're working with.


### 2.2 General Syntax Concepts


#### 2.2.1 Statements and Directives


- **Statements:**

  - Instructions that the CPU executes (e.g., `mov`, `add`, `jmp`).

  

- **Directives:**

  - Instructions for the assembler, providing information about how to process the code (e.g., `section`, `global`, `extern`).


#### 2.2.2 Labels


- **Labels:**

  - Used to mark a location in the code.

  - Typically followed by a colon (e.g., `_start:`).


#### 2.2.3 Comments


- **Comments:**

  - Used for explanatory notes in the code.

  - In NASM syntax: `; This is a comment`.

  - In GAS syntax: `# This is a comment`.


### 2.3 Data and Text Sections


#### 2.3.1 Data Section


- **Data Section:**

  - Declares data that will be used in the program.

  - In NASM syntax:

    ```assembly

    section .data

        variable db 42

    ```


#### 2.3.2 Text Section


- **Text Section:**

  - Contains the executable code.

  - In NASM syntax:

    ```assembly

    section .text

        global _start

    _start:

        mov eax, 1

        int 0x80

    ```


### 2.4 Instructions


#### 2.4.1 Opcode and Operands


- **Opcode:**

  - Mnemonic representing the operation to be performed (e.g., `mov`, `add`).


- **Operands:**

  - Source and destination for the operation.

  - In NASM syntax: `mov eax, ebx`.


#### 2.4.2 Registers


- **Registers:**

  - Storage locations within the CPU.

  - In NASM syntax: `eax`, `ebx`, `ecx`.


#### 2.4.3 Immediate Values


- **Immediate Values:**

  - Constants used as operands.

  - In NASM syntax: `mov eax, 42`.


#### 2.4.4 Memory Addresses


- **Memory Addresses:**

  - Accessing data in memory.

  - In NASM syntax: `mov eax, [variable]`.


### 2.5 Control Flow


#### 2.5.1 Labels and Jumps


- **Labels and Jumps:**

  - Controlling the flow of execution.

  - In NASM syntax:

    ```assembly

    jmp start

    loop:

        ; Code here

    start:

    ```


### 2.6 System Calls


- **System Calls:**

  - Interfacing with the operating system.

  - In NASM syntax:

    ```assembly

    mov eax, 4

    mov ebx, 1

    mov ecx, message

    mov edx, 13

    int 0x80

    ```


### 2.7 Conclusion


Understanding assembly language syntax is foundational for writing effective and functional code. As you delve into assembly programming, keep in mind that the syntax may vary based on the architecture you're working with.


Comments

Houston We Have a Podcast

Translate