NASA

A Handbook for Assembly | Chapter 2: Assembly Language Directives


 


Chapter 2: Assembly Language Directives


### 2.1 Introduction


Assembly language directives are instructions to the assembler that provide information about how to process the code. In this chapter, we will explore common assembly language directives that are used to define data, sections, and other essential aspects of a program.


### 2.2 Data Definition Directives


#### 2.2.1 `db` - Define Byte


- **Purpose:**

  - Reserves one or more bytes of storage and initializes them with specified values.


- **Example:**

  ```assembly

  variable db 42       ; Defines a byte with the value 42

  message db 'Hello'   ; Defines a byte array with the ASCII values of 'H', 'e', 'l', 'l', 'o'

  ```


#### 2.2.2 `dw` - Define Word


- **Purpose:**

  - Reserves one or more words (16 bits) of storage and initializes them.


- **Example:**

  ```assembly

  number dw 1000       ; Defines a word with the value 1000

  array dw 1, 2, 3, 4  ; Defines an array of words

  ```


#### 2.2.3 `dd` - Define Doubleword


- **Purpose:**

  - Reserves one or more doublewords (32 bits) of storage and initializes them.


- **Example:**

  ```assembly

  value dd 123456      ; Defines a doubleword with the value 123456

  list dd 10, 20, 30   ; Defines a list of doublewords

  ```


### 2.3 Section Directives


#### 2.3.1 `section` - Section Declaration


- **Purpose:**

  - Defines a section of the program, such as the code section or data section.


- **Example:**

  ```assembly

  section .data        ; Data section

      variable db 42

  section .text        ; Code section

      global _start

  ```


#### 2.3.2 `global` - Global Declaration


- **Purpose:**

  - Declares a label as globally accessible, allowing it to be used in other files.


- **Example:**

  ```assembly

  global _start

  _start:

      ; Code here

  ```


#### 2.3.3 `extern` - External Declaration


- **Purpose:**

  - Informs the assembler that a label is defined in another file.


- **Example:**

  ```assembly

  extern function     ; Declares that 'function' is defined in another file

  _start:

      ; Code here

  ```


### 2.4 Reserving Storage


#### 2.4.1 `resb`, `resw`, `resd` - Reserve Bytes, Words, Doublewords


- **Purpose:**

  - Reserves a specified number of bytes, words, or doublewords without initializing them.


- **Example:**

  ```assembly

  buffer resb 100      ; Reserves 100 bytes of storage for a buffer

  array resd 10        ; Reserves space for an array of 10 doublewords

  ```


### 2.5 Equating Values


#### 2.5.1 `equ` - Equate


- **Purpose:**

  - Assigns a constant value to a symbol.


- **Example:**

  ```assembly

  size equ 100         ; Defines a constant 'size' with the value 100

  ```


### 2.6 Including Files


#### 2.6.1 `include` - Include File


- **Purpose:**

  - Inserts the content of an external file into the source code.


- **Example:**

  ```assembly

  include 'macros.inc' ; Includes the content of 'macros.inc' in the source code

  ```


### 2.7 Conclusion


Assembly language directives play a crucial role in providing structure and information to the assembler during the compilation process. As you advance in assembly programming, a solid understanding of these directives will empower you to effectively organize and manage your code.


Comments

Houston We Have a Podcast

Translate