Assembly language directives
are used to allocate space in the memory for variables. Different directives
are used to allocate space of different sizes.
We list down the directives
provided by the assembler for the Intel X86 processors, Netwide Assembler (NASM) below:
DB
– Define Byte is used to allocate only 1 byte.DW
– Define Word allocates 2 bytes.DD
– Define Doubleword is used to allocate 4 bytes.DW
– Define Quadword allocates 8 bytes.DT
– Define Tenword is used to allocate 10 bytes.The directives
listed above are typically used to allocate space for initialized data and are used in the data
section. To reserve memory for uninitialized data, the following directives
are used:
RESB
– Reserves 1 byte.RESW
– Reserves 2 bytes or a Word.RESD
– Reserves 4 bytes or a Doubleword.RESQ
– Reserves 8 bytes or a Quadword.REST
– Reserves 10 bytes or a Tenword.These directives are typically used in the bss
section.
The code below shows how directives
may be used in an assembly language program to allocate and reserve space for initialized and uninitialized variables, respectively:
section .textglobal _start_start:mov eax, 0x4 ;4 is the unique system call number of the write system callmov ebx, 1 ;1 is the file descriptor for the stdout streammov ecx, fir_message ;message is the string that needs to be printed on the consolemov edx, fir_length ;length of messageint 0x80 ;interrupt to run the kernelmov eax, 0x4 ;same steps as above to print sec_messagemov ebx, 1mov ecx, sec_messagemov edx, sec_lengthint 0x80mov edx, "A" ;sets value of edx to "A"mov [uninit], edx ;copies value in edx and moves it into the uninitialised variablemov eax, 0x4 ;prints current value of uninitmov ecx, uninitmov edx, 1int 0x80mov eax, 0x1 ;1 is the unique system call number of the exit system callmov ebx, 0 ;argument to exit system callint 0x80 ;interrupt to run the kernel and exit gracefullysection .datafir_message db "Welcome to Edpresso!" , 0xAfir_length equ $-fir_messagesec_message db "The variable in the bss section is now initialised to: "sec_length equ $-sec_messagesection .bssuninit resb 1 ;declares uninitialised variable
We declare and initialize two strings in the data
section of our program. In the bss
section, an uninitialized variable of size 1 byte is declared and later initialized to “A” in the code
section. The write
system call is used to write data to the stdout
stream and print it onto the console.
The Netwide Assembler (NASM) can be used to assemble this program. Upon successful execution, it gives the following output:
Welcome to Edpresso!
The variable in the bss section is now initialised to: A
Free Resources