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
segment
andsection
can be used interchangeably.
segment .data:message db "Welcome to Edpresso!" , 0xAmessage_length equ $-messagesegment .bss:segment .text:global _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, message ;message is the string that needs to be printed on the consolemov edx, message_length ;length of messageint 0x80 ;interrupt to run the kernelmov 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 gracefully
Free Resources