Home > OS >  Why does NASM system call number perform 2 different operations despite specifying the same call num
Why does NASM system call number perform 2 different operations despite specifying the same call num

Time:07-16

I have the following 'hello world' code written in NASM x86_64 assembly:

section .data
    msg     db "Hello World", 0xa
    msg_L   equ $-msg

section .text
    global _start

_start:
    mov     eax, 4      ; sys_write call
    mov     ebx, 1      ; stdout
    mov     ecx, msg
    mov     edx, msg_L
    int     0x80        ; call kernel

    mov     eax, 1      ; sys_exit call
    int     0x80        ; call kernel

In the first 'function' under the _start: section, mov ebx, 1 is used to specify the standard output for printing. Later, after the first kernel call, mov eax, 1 is used to specify the sys_exit system call. I don't understand how specifying the same system call number yields 2 different results when the kernel is called. This NASM tutorial specifies 1 as the system call number for sys_exit, yet the program does not exit after the first use of that number, and uses it for stdout instead. Can someone explain to me why this is?

CodePudding user response:

You are not specifying the same system call number.

eax, not ebx, is used to specify system call numbers.

mov ebx, 1 sets the value of ebx and doesn't set the value of eax.

The system call number is set to 4 via mov eax, 4 when using the standard output set by mov ebx, 1.

  • Related