Data Definition Instruction in NASM

In NASM (Netwide Assembler), data definition instructions are directives used to define and allocate memory for data in assembly language programs. These instructions specify how data should be stored in memory and allow programmers to organize and manage data efficiently.

  • Data Definition Instruction doesn't perform operation on data but rather defines how data should be structured in memory.

Here are the main data definition instructions commonly used in NASM:

1. Reserve Space Instructions

These instructions allocate memory without initializing it to specific values:

resb:

It reserves a specified number of bytes (8 bits) of memory.

buffer resb 64   ; Reserves 64 bytes of memory

resw:

Reserves a specified number of words (16 bits) of memory.

array resw 10    ; Reserves space for 10 words (20 bytes)

resd:

Reserves a specified number of doublewords (32 bits) of memory.

numbers resd 5   ; Reserves space for 5 doublewords (20 bytes)

resq:

Reserves a specified number of quadwords (64 bits) of memory.

values resq 2     ; Reserves space for 2 quadwords (16 bytes)

2 Define Data Instructions

These instructions define memory and optionally initialize it with specific values:

db:

Define bytes and optionally initialize them.

message db 'Hello, world!', 0  ; Defines a null-terminated string

dw:

Define words (16 bits) and optionally initialize them.

data dw 1234, 5678  ; Defines two words with initial values

dd:

Define doublewords (32 bits) and optionally initialize them.

values dd 0x12345678, 0x90ABCDEF  ; Defines two doublewords with hexadecimal values

dq:

Define quadwords (64 bits) and optionally initialize them.

large_number dq 12345678901234567890  ; Defines a quadword with a large integer value

3 Times

times:

Repeat a data definition a specified number of times.

zeroes resb 10        ; Reserves 10 bytes initialized to 0
lots_of_numbers times 1000 db 0   ; Defines 1000 bytes initialized to 0

4 Structure Definition (struc Directive)

struc and endstruc:

  • Purpose: Defines a structure template that specifies the layout of data.
  • Usage: Used to organize and group related data fields into a cohesive structure.
struc Employee
    .id resd 1       ; 4 bytes for employee ID
    .name resb 32    ; 32 bytes for employee name
    .age resb 1      ; 1 byte for employee age
endstruc
  •  

Usage and Purpose

  • Allocation: These instructions allocate memory for variables, arrays, strings, and other data structures used in the program.
  • Initialization: They optionally initialize allocated memory with specific values.
  • Flexibility: Provide flexibility in defining data structures according to the needs of the program.
  • Efficiency: Help in optimizing memory usage by specifying exact requirements for data storage.