28.11.2014 Views

assembly_tutorial

assembly_tutorial

assembly_tutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Apart from the DS, CS and SS registers, there are other extra segment registers - ES (extra segment), FS and<br />

GS, which provides additional segments for storing data.<br />

In <strong>assembly</strong> programming, a program needs to access the memory locations. All memory locations within a<br />

segment are relative to the starting address of the segment. A segment begins in an address evenly disable by 16<br />

or hexadecimal 10. So all the rightmost hex digit in all such memory addresses is 0, which is not generally stored<br />

in the segment registers.<br />

The segment registers stores the starting addresses of a segment. To get the exact location of data or instruction<br />

within a segment, an offset value (or displacement) is required. To reference any memory location in a segment,<br />

the processor combines the segment address in the segment register with the offset value of the location.<br />

Example:<br />

Look at the following simple program to understand the use of registers in <strong>assembly</strong> programming. This program<br />

displays 9 stars on the screen along with a simple message:<br />

section .text<br />

global main ;must be declared for linker (gcc)<br />

main: ;tell linker entry point<br />

mov edx,len ;message length<br />

mov ecx,msg ;message to write<br />

mov ebx,1 ;file descriptor (stdout)<br />

mov eax,4 ;system call number (sys_write)<br />

int 0x80 ;call kernel<br />

mov edx,9 ;message length<br />

mov ecx,s2 ;message to write<br />

mov ebx,1 ;file descriptor (stdout)<br />

mov eax,4 ;system call number (sys_write)<br />

int 0x80 ;call kernel<br />

mov eax,1 ;system call number (sys_exit)<br />

int 0x80 ;call kernel<br />

section .data<br />

msg db 'Displaying 9 stars',0xa ;a message<br />

len equ $ - msg<br />

;length of message<br />

s2 times 9 db '*'<br />

When the above code is compiled and executed, it produces following result:<br />

Displaying 9 stars<br />

*********<br />

TUTORIALS POINT<br />

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!