Understanding Assembly Code

As the title suggests, I want to better understand the following assembly code:

section .text
    global main          ; must be declared for linker (gcc)
main:                        ; tell linker entry point
    mov    edx, len     ; message length
    mov    ecx, msg    ; message to write
    mov    ebx, 1         ; file descriptor (stdout)
    mov    eax, 4         ; syscall for write (sys_write)
    int      0x80           ; call kernel

    mov    edx, 9        ; message length
    mov    ecx, s2       ; message to write
    mov    ebx, 1        ; file descriptor (stdout)
    mov    eax, 4        ; syscall number for write (sys_write)
    int      0x80          ; call kernel
    mov    eax, 1        ; system call (sys_exit)
    int      0x80          ; call kernel

section .data
msg db 'Displaying 9 stars', 0xa ; a message
len equ $ - msg            ; length of message
s2 times 9 db '*'

As you can see I already have descriptions in the comments from the tutorial I found here. Here are some of the things I don't understand:

    • What is s2? Is this just a variable or a register I know nothing about?
  1. When '1' is moved into ebx, is this a parameter to the sys_exit later called in eax? I found a listing of Linux syscalls here and it does seem sys_exit does take one parameter in ebx that's an integer. If this is the case why not exit cleanly with zero?

I just want to make sure I understand everything correctly in this. Thanks in advance!

My last assembler is quite some days ago, but I'd say
1) s2 is (the address of) a constant ("*********") defined in the last line of your listing
2) eax holds the call No. (function selector) for the linux kernel interrupt 0x80 see here. As the return value in ebx is not explicitly set, it seems to rely on the value returned by sys_write to be used as the exit code.

1 Like

That actually makes a lot of sense. I think I had just been looking at it too long and over analyzing things. The link you provided seems to be the same as the pdf I'm reading, but with some extra information. Thanks for the clarity!