0

i need to read some unknown number of chars from file. I want to detected the end of string (in this case it is EOF). Firstly i read from file using syscall 3, and then checked if rax is equal to 0. Thats works well if I read one char at the same time,but I want to read like this:

mov rax, 3
mov rbx, stdin
mov rcx, input
mov rdx, 8
int 80h

So i read 8 bits, and then i got loop over this bits i want to detect if there is eof. EOF detection looks like this::

cmp r8b,0
jle loop_end

But that doenst work at all. Can i somehow read more bits fe. 8. And detected EOF on fe position 5? Or maybe syscall 3 can return number of bits readed?

ziyiyituxe
  • 177
  • 1
  • 9
  • On what system are you working? Normally in 64-bit mode you want to use the `syscall` instruction instead of `int 80h`. – Nate Eldredge Mar 25 '20 at 17:24
  • 2
    The return value from the system call will be in `rax`. A value of 0 indicates EOF, just like for `read(2)` in C. Note that you can also get a positive value less than 8, or a negative value in case of error; your code should properly handle all these cases. – Nate Eldredge Mar 25 '20 at 17:25
  • i see that for 7 letters i get rax equal to 8 (because of EOF). How can i decide if the last bit is EOF or normal number, I tried cmp last bit to 0 but it did not work. What is EOF code? – ziyiyituxe Mar 25 '20 at 19:27
  • I don't understand your comment. There is no "EOF code". `rax` contains the number of characters successfully read, pure and simple. You have to process that number of characters. If `rax` is zero then you have reached the end of the file and should stop reading any more. – Nate Eldredge Mar 25 '20 at 20:05
  • 1
    By the way, I think everywhere in your question where it says "bits" you actually mean "bytes". This is rather confusing. – Nate Eldredge Mar 25 '20 at 20:05
  • Related: [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](https://stackoverflow.com/q/46087730) - the 32-bit ABI only works if your pointers happen to fit in 32 bits. And also it zeros r8..r11. – Peter Cordes Mar 26 '20 at 03:38

0 Answers0