n_asm assembly memory segments (sol)

1
http://www.tutorialspoint.com/assembly_prog ramming /assembly_memory_seg ments.htmCopyright © tutorialspoint.com ASSEMBLY - MEMORY SEGMENTS We have already discussed three sections of an assembly program. These sections represent various memory se g me nts as we ll. Inte re sting ly, if you re place the se ction ke yword with se g me nt, you will g e t the same re sult. T ry 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! Memory Segments A seg mented memory model divides the system memory into g roups of independent seg ments referenced by pointers located in the seg ment reg isters. Each seg ment is used to contain a specific type of data. One seg ment is used to contain instruction codes, another seg ment stores the data elements, and a third seg ment keeps the prog ram stack. In the lig ht of the above discussion, we can specify various memory seg ments as: Data seg ment - it is represented by .data se ction and the .bss . The .data section is used to declare the memory reg ion, where data elements are stored for the prog ram. This section cannot be expanded after the data elements are declared, and it remains static throug hout the prog ram. The .bss section is also a static memory section that contains buffers for data to be declared later in the prog ram. This buffer memory is zero-filled. Code segment - it is represented by .text se ction. T his de fine s an are a in me mory that store s the instruction codes. This is also a fixed area. Stack - this seg ment contains data values passed to functions and procedures within the prog ram.

Upload: selomon-birhane

Post on 20-Jul-2015

26 views

Category:

Software


2 download

TRANSCRIPT

Page 1: N_Asm Assembly memory segments (sol)

http://www.tuto rialspo int.co m/assembly_pro g ramming /assembly_memo ry_seg ments.htmCopyrig ht © tutorialspoint.com

ASSEMBLY - MEMORY SEGMENTS

We have already discussed three sections of an assembly prog ram. These sections represent various memoryseg ments as well.

Interesting ly, if you replace the section keyword with seg ment, you will g et the same result. Try the followingcode:

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 segmentmsg db 'Hello, world!',0xa ;our dear stringlen equ $ - msg ;length of our dear string

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

Hello, world!

Memory Seg ments

A seg mented memory model divides the system memory into g roups of independent seg ments referenced bypointers located in the seg ment reg isters. Each seg ment is used to contain a specific type of data. One seg mentis used to contain instruction codes, another seg ment stores the data elements, and a third seg ment keeps theprog ram stack.

In the lig ht of the above discussion, we can specify various memory seg ments as:

Data seg ment - it is represented by .data section and the .bss. The .data section is used to declare thememory reg ion, where data elements are stored for the prog ram. This section cannot be expanded afterthe data elements are declared, and it remains static throug hout the prog ram.

The .bss section is also a static memory section that contains buffers for data to be declared later in theprog ram. This buffer memory is zero-filled.

Code seg ment - it is represented by .text section. This defines an area in memory that stores theinstruction codes. This is also a fixed area.

Stack - this seg ment contains data values passed to functions and procedures within the prog ram.