0

I am reading Jeff Duntenmam Assembly book for Linux and i am trying to understand the code below

Read:
        mov eax,3               ; Specify sys_read call
        mov ebx,0               ; Specify File Descriptor 0: Standard Input
        mov ecx,buff           ; Pass offset of the buffer to read to
        mov edx,bufflen        ; Pass number of bytes to read at one pass
        int 80h                 ; Call sys_read to fill the buffer
        mov ebp,eax             ; Save # of bytes read from file for later
        cmp eax, 0              ;If eax=0, sys_read reached EOF on stdin
        je Done

To use the read from the system call table uses registers eax till edx. From the system call table read is 3 and goes into register eax i am reading the buffer from standard input (0) which goes into register ebx Where i would like the buffer to be stored is Buff which goes in register ecx and how larger this buffer is in edx.

Then the author does

mov ebp,eax

i am not understanding how this lets him save the actual number of bytes entered into the buffer though? fyi i am a noob when it comes to assembly. Please don't make your answers too complicated.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
opobtdfs
  • 5
  • 2
  • 2
    Each system call returns something in `eax`. In case of `read`, this is the number of bytes read or a small negative value on error. – fuz Jan 27 '21 at 17:28
  • 1
    Turns out my answer on [What are the return values of system calls in Assembly?](https://stackoverflow.com/q/38751614) happens to use `read()` as an example, so it exactly answers the specific question as well as system-call return values in general. – Peter Cordes Jan 27 '21 at 18:32
  • @fuz thank you very much – opobtdfs Jan 27 '21 at 19:07
  • @PeterCordes I was reading the exact same thread. I am reading the read man pages and it states that it gives a return value " On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number...." but it doesn't explicitly state where the return value will be place. Or am i reading the wrong thing? – opobtdfs Jan 27 '21 at 21:41
  • The return value is always in RAX, that's part of the ABI. The C man page will of course only talk about "the return value" - the whole point of my linked answer is that the C man pages document in C terms, and to use it in asm you just need to know the calling convention that all system calls use. – Peter Cordes Jan 27 '21 at 21:43

0 Answers0