Search code examples
linuxassemblyx86system-calls

what is the purpose of "movl $0, %ebx" or getting 0 into ebx before an int $0x80 Linux SYS_EXIT?


I know that its getting 0 into ebx but why? I'm so sorry if it looks like a no-brainer question to you, its my first week of learning assembly and a few months of programming.

I haven't included everything below because it is a quite long, lmk if its necessary

The assembly is from the book "Programming From Ground Up Chapter 6"

summary of assembly:

Opens an input and output file, reads records from the input, increments the age, writes the new record to the output file

SYS_EXIT is 1
LINUX_SYSCALL is 0x80

loop_begin:
pushl ST_INPUT_DESCRIPTOR(%ebp)
pushl $record_buffer
call read_record
addl $8, %esp 

# Returns the number of bytes read. If it isn’t the same number we requested, then it’s either an end-of-file, or an error, so we’re quitting
cmpl $RECORD_SIZE, %eax
jne loop_end

#Increment the age
incl record_buffer + RECORD_AGE

#Write the record out
pushl ST_OUTPUT_DESCRIPTOR(%ebp)
pushl $record_buffer
call write_record
addl $8, %esp
jmp loop_begin

loop_end:
movl $SYS_EXIT, %eax
movl $0, %ebx                             <------------------------ THE INSTRUCTION'S PURPOSE THAT IM ASKING FOR
int $LINUX_SYSCALL

Solution

  • This is the equivalent of _exit(0); in C; except that the Linux kernel uses different calling conventions (parameters passed in registers and not on the stack).

    The movl $0, %ebx is loading the 2nd parameter (0) into the right register for the kernel's calling convention. The first parameter is the function number (SYS_EXIT).