Memory Management in Assembly language

Prahlad Godara ------ From DOOSEEP


Assembly - Memory Management

Memory management is the process of controlling and coordinating a computer's main memory. It ensures that blocks of memory space are properly managed and allocated so the operating system (OS), applications and other running processes have the memory they need to carry out their operations.
As part of this activity, memory management takes into account the capacity limitations of the memory device itself, deallocating memory space when it is no longer needed or extending that space through virtual memory. Memory management strives to optimize memory usage so the CPU can efficiently access the instructions and data it needs to execute the various processes.

In assembly language you can use both static and dynamic memory allocation. By properly managing the process of memory allocation, reallocation and deallocation, large programs can run efficiently even with limited memory.


The assembly language me Sys_brk() system call is provided by the kernel, in order to allocate memory without the need to move it later. This call allocates memory right behind the application image in memory. This system function allows you to set the highest available address in the data section.

This system call takes one parameter, which is the highest memory address needed to be set. This value is stored in the EBX register.
In case of any error, sys_brk() returns -1 or returns the negative error code itself. The following example demonstrates dynamic memory allocation.


Exampal

The following program allocates 16kb of memory using the sys_brk() system call −

section	.text
   global _start         ;must be declared for using gcc
	
_start:	                 ;tell linker entry point

   mov	eax, 45		 ;sys_brk
   xor	ebx, ebx
   int	80h

   add	eax, 16384	 ;number of bytes to be reserved
   mov	ebx, eax
   mov	eax, 45		 ;sys_brk
   int	80h
	
   cmp	eax, 0
   jl	exit	;exit, if error 
   mov	edi, eax	 ;EDI = highest available address
   sub	edi, 4		 ;pointing to the last DWORD  
   mov	ecx, 4096	 ;number of DWORDs allocated
   xor	eax, eax	 ;clear eax
   std			 ;backward
   rep	stosd            ;repete for entire allocated area
   cld			 ;put DF flag to normal state
	
   mov	eax, 4
   mov	ebx, 1
   mov	ecx, msg
   mov	edx, len
   int	80h		 ;print a message

exit:
   mov	eax, 1
   xor	ebx, ebx
   int	80h
	
section	.data
msg    	db	"Allocated 16 kb of memory!", 10
len     equ	$ - msg 

Result −

 Allocated 16 kb of memory! 

Tags- Nasm Assembly language Memory Management. what is Memory Management in assembly language, This blogcreates content similar to stackoverflow geeks for geeks tutorialspoint w3schools and dooseep,Memory Management in assembly in hindi
No comments:
Post a Comment