William
William

Reputation: 1895

How are system calls interpreted in x86 assembly linux

I am confused towards why/how a value gets printed in x86 assembly in a Linux environment.

For example if I wish to print a value I would do this:

mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx msgLength
int 80h

Now I understand the numerical value 4 will make the system call to sys_write after the interrupt. But my question is, what is the significance of the 4? Is it loading the address of the decimal value 4 into eax? Or is it loading the value 4 into the eax register?

I am confused after reading I can transfer the value at an address to a register using the following instruction:

mov eax, [msg]

eax will now contain the bytes at the address of msg, but I would guess this format is not acceptable:

mov eax, [4]

So what is really happening when I move 4 into eax to print something?

Upvotes: 3

Views: 705

Answers (2)

Subrata Paul
Subrata Paul

Reputation: 96

Linux kernel maintains all the system call routines as an array of function pointers (can be called as sys_call table) and the value in the eax gives the index to that array (which system call to choose) by the kernel. Other registers like ebx, ecx, edx contains the appropriate parameters for that system call routine. And the int 80h is for software interrupt to the cpu from user mode to kernel mode because actual system call routine is kernel space function.

Upvotes: 2

Jester
Jester

Reputation: 58802

Simply the value (number) 4 is loaded into eax, no magic there. The operating system will look at the value in eax to figure out what function you want. System call number is a code that identifies the various available kernel functions you can use.

Upvotes: 3

Related Questions