Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Assembly Language – Variables

Assembly Language - Variables

A variable is the basic unit for storing data in a program. Defining and using variables in assembly is closer to the hardware than in high-level languages; you must directly control a variable's size, type, and memory layout.


The concept of variables in assembly

In assembly language, a variable is essentially a named storage location in memory.

Unlike high-level languages, assembly variables have no automatic type checking—a label is just an alias for a memory address, and you can read or write it with any size.

NASM provides three sections to hold different types of variables:.data(initialized),.bss(uninitialized), and .rodata(read-only).


Define initialized variables in the .data section

Use the DB, DW, DD, DQ, DT directives to define variables of different sizes:

Directive Data size Meaning Example
DB 1 byte Define Byte flag db 1
DW 2 bytes Define Word count dw 1000
DD 4 bytes Define Doubleword price dd 9999
DQ 8 bytes Define Quadword big_val dq 0x1234567890ABCDEF
DT 10 bytes Define Ten Bytes ext_val dt 3.14

Example

; File path: variables_data.asm

; Demonstrate the definition of various variables in the .data section

section

.data

   

; Byte variable

    status

db

1

               

; 1 byte, value 1

    grade

db

'A'

               

; 1 byte, character 'A' = 0x41

   

; Word variable (2 bytes)

    year

dw

2026

               

; 2 bytes, value 2026

   

; Doubleword variable (4 bytes)

    salary

dd

50000

           

; 4 bytes, value 50000

   

; Multi-byte sequence

    msg

db

'Hello, runoob!'

,

0

 

; String terminated with 0 (C style)

    hex_bytes

db

0x55

,

0xAA

,

0x00

,

0xFF

 

; Hexadecimal byte sequence

   

; Repeated value

    stars

db

10

dup

(

'*'

)

       

; 10 asterisk characters

   

; Multiple variables with names

    x

dd

10

    y

dd

20

    z

dd

30

section

.text

   

global

_start

_start

:

   

; Read variable values into registers

   

mov

al

,

[

status

]

           

; al = 1 (read 1 byte)

   

mov

ax

,

[

year

]

             

; ax = 2026 (read 2 bytes)

   

mov

eax

,

[

salary

]

         

; eax = 50000 (read 4 bytes)

   

; Modify a variable's value

   

mov

byte

[

status

]

,

0

       

; status = 0

   

mov

dword

[

x

]

,

100

         

; x = 100

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80

Use [变量名] When accessing a variable, always make sure the number of bytes read/written matches the size defined for the variable. Use mov al, [status] to read 1 byte, use mov eax, [salary] to read 4 bytes. NASM checks whether the operand sizes are consistent.


Reserve uninitialized space in the .bss section

Use directives such as RESB, RESW, RESD to reserve space (without initialization):

Directive Reserved size Example
RESB bytes buffer resb 256
RESW Word (2 bytes) wbuf resw 100
RESD Doubleword (4 bytes) dbuf resd 50
RESQ Quadword (8 bytes) qbuf resq 25

Example

; File path: variables_bss.asm

; Demonstrate reserving space in the .bss section

section

.bss

    input_buf

resb

128

         

; Reserve a 128-byte input buffer

    numbers

resd

100

           

; Reserve 100 double words (400 bytes)

    temp

resb

1

               

; Reserve a 1-byte temporary variable

section

.data

    prompt

db

'Enter a number: '

    prompt_len

equ

$

prompt

section

.text

   

global

_start

_start

:

   

; Output a prompt

   

mov

eax

,

4

   

mov

ebx

,

1

   

mov

ecx

,

prompt

   

mov

edx

,

prompt_len

   

int

0x80

   

; Read input into the buffer in the .bss section

   

mov

eax

,

3

   

mov

ebx

,

0

   

mov

ecx

,

input_buf          

; Use the space reserved in the .bss section

   

mov

edx

,

128

   

int

0x80

   

; Fill the numbers array with data

   

mov

dword

[

numbers

]

,

42

     

; numbers[0] = 42

   

mov

dword

[

numbers

4

]

,

100

; numbers[1] = 100

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80


Memory layout of byte, word, and doubleword

x86 uses Little Endian—the low byte is stored at the low address.

For example, when defining value dd 0x12345678 the memory layout is:



地址    :[value]  [value+1]  [value+2]  [value+3]

内容    :0x78     0x56       0x34       0x12

Example

; File path: endianness.asm

; Demonstrate little-endian storage

section

.data

    value

dd

0x12345678

       

; Doubleword, 4 bytes

section

.text

   

global

_start

_start

:

   

; Read the same variable at different sizes

   

mov

eax

,

[

value

]

           

; Read 4 bytes: eax = 0x12345678

   

mov

ax

,

[

value

]

           

; Read 2 bytes: ax = 0x5678 (low 2 bytes)

   

mov

al

,

[

value

]

           

; Read 1 byte: al = 0x78 (lowest 1 byte)

   

; Verify little-endian: low address holds the low byte

   

mov

al

,

[

value

]

           

; al = 0x78

   

mov

bl

,

[

value

1

]

       

; bl = 0x56

   

mov

cl

,

[

value

2

]

       

; cl = 0x34

   

mov

dl

,

[

value

3

]

       

; dl = 0x12

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80


Variable initialization and access

Operations on variables in assembly follow a three-step "load-compute-store" pattern:

Example

; File path: var_operations.asm

; Demonstrate the three-step pattern of variable operations

section

.data

    a

dd

100

    b

dd

200

    result

dd

0

section

.text

   

global

_start

_start

:

   

; result = a + b

   

; Step 1: load

   

mov

eax

,

[

a

]

               

; Load a into eax

   

; Step 2: compute

   

add

eax

,

[

b

]

               

; eax = eax + b

   

; Step 3: store

   

mov

[

result

]

,

eax

         

; Store the result into result

   

; result = a * 2 - b

   

mov

eax

,

[

a

]

   

imul

eax

,

2

               

; eax = a * 2

   

sub

eax

,

[

b

]

               

; eax = eax - b

   

mov

[

result

]

,

eax

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80

Assembly does not support mov [result], [a] + [b] this kind of high-level syntax. The x86 mov instruction cannot use two memory operands at once; it must go through a register first.


Variable size operators

When the assembler cannot infer the operand size, you need to specify it explicitly:

Example

; Use size operators to make the data size explicit

section

.data

    var

dd

0

section

.text

   

global

_start

_start

:

   

; Size operators: byte, word, dword, qword

   

mov

byte

[

var

]

,

1

         

; Write 1 byte

   

mov

word

[

var

]

,

1000

       

; Write 2 bytes

   

mov

dword

[

var

]

,

999999

   

; Write 4 bytes

   

; Can be omitted when the target size is clear (determined by the register)

   

mov

al

,

[

var

]

             

; al is 1 byte, automatically read as byte

   

mov

ax

,

[

var

]

             

; ax is 2 bytes, automatically read as word

   

mov

eax

,

[

var

]

             

; eax is 4 bytes, automatically read as dword

   

; But the following line will cause an error (size ambiguous):

   

; mov [var], 1 ; Error! 1 could be a byte/word/doubleword

   

; It must be written as:

   

mov

dword

[

var

]

,

1

         

; Explicitly specify 4 bytes

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80