0
section     .text
global      _start                              ;must be declared for linker (ld)

_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

section     .data

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

Kernel source references:

How does the system know that it has to exit when it read EAX,1 and not EBX,1 ? Since 1 means Sys_Exit.

Jester
  • 52,795
  • 4
  • 67
  • 108
  • 3
    The interrupt handler for `int 0x80` in the operating system expects the function code in `eax` so that's where you should put it. Not sure if you are confused about `mov eax,1` actually exiting, it doesn't, it just loads `1` into `eax`. It's the kernel that will check value of `eax` after you invoke it with `int 0x80`. – Jester Jan 20 '16 at 17:45
  • The `sys_write` function needs additional information, which is in `ebx`, `ecx` and `edx`. Loading `mov ebx,1` has nothing to do with the later `mov eax,1` which specifies the `sys_exit` function. – Weather Vane Jan 20 '16 at 18:57
  • Thank you :) I kinda understand it ! Im new to Assembly. As I go on I think I will understand it better. But your answer has given me what I wanted – bhuvanesh arasu Jan 20 '16 at 19:07
  • To the microprocessor, each register (`eax`, `ebx`, `ecx`, etc) is a distinct, independent location where a number can be stored. Many external funcitons (such as operating system interrupt calls) have a convention for kinds of information stored in specific registers. Some registers have a special functional meaning or modes of use for the microprocessor itself as well. – lurker Jan 20 '16 at 19:30

1 Answers1

1

This comportement is defined in what we call ABI (application binary interface). This should help : What is Application Binary Interface (ABI)?

Community
  • 1
  • 1
Pierre
  • 1,062
  • 11
  • 26