Any assembly language program is divided into the following three memory segments:
Code– The segment where actual code is stored and is represented by the .text section. It mainly contains assembly language instructions and can not be expanded once program execution begins.
Data– Represented by the .data and .bss sections, it is used to declare global and local variables. It remains static throughout the program.
Stack– In this memory segment, data may be initialized at run-time. It acts as temporary storage and follows the Last In First Out protocol, which means that elements are only added or removed from the top.
A unique pointer is defined in the segment registers for each of the three memory segments.
The following snippet of code demonstrates how a simple assembly language program, which prints a string to the console, is divided into different memory segments or sections:
In assembly language code, the keywords
sectioncan be used interchangeably.
segment .data: message db "Welcome to Edpresso!" , 0xA message_length equ $-message segment .bss: segment .text: global _start _start: mov eax, 0x4 ;4 is the unique system call number of the write system call mov ebx, 1 ;1 is the file descriptor for the stdout stream mov ecx, message ;message is the string that needs to be printed on the console mov edx, message_length ;length of message int 0x80 ;interrupt to run the kernel mov eax, 0x1 ;1 is the unique system call number of the exit system call mov ebx, 0 ;argument to exit system call int 0x80 ;interrupt to run the kernel and exit gracefully
View all Courses