Memory Segment in Assembly Language - Data Code and Stack.

Prahlad Godara ------ From DOOSEEP

Assembly - Memory Segment

A partitioned memory model divides system memory into groups of independent segments referenced by pointers located in segment registers. Each segment is used to contain a specific type of data. One part is used to hold the instruction code, the second part stores the data elements, and the third part holds the program stack.

According to the above statement we can specify the different memory parts as follows. Data Segment, Code Segment, Stack.

  1. Data Segment – The data Segment is used to declare initialized data or constants. This data does not change at runtime. You can declare various constant values, file names or buffer sizes etc. in this section.
    Syntax for declaring a data segment - segment.data
  2. Code Segment - This is represented by the .text Segment. It defines an area in memory that stores the instruction code. This is also a fixed area.
    code segment syntax - segment.text
  3. stack - This segment contains data values passed to functions and procedures within the program.

We have already discussed the three sections of an assembly program. These sections also represent different memory segments.

Interestingly, if you replace the section keyword with segment, you get the same result. Try the following code -


segment .text	   ;code segment
   global _start   ;must be declared for linker 
	
_start:	           ;tell linker entry point
   mov edx,len	   ;message length
   mov ecx,msg     ;message to write
   mov ebx,1	   ;file descriptor (stdout)
   mov eax,4	   ;system call number (sys_write)
   int 0x80	       ;call kernel

   mov eax,1       ;system call number (sys_exit)
   int 0x80	       ;call kernel

segment .data      ;data segment
msg	db 'Hello, world!',0xa   ;our dear string
len	equ	$ - msg              ;length of our dear string
               

When the above code is compiled and executed, it produces the following result −

 Hello, world! 
Tags- Assembly Segment, assembly language Segment, type of Assembly Program Segment, Hello world program in assembly language