-1

I'm working on making a standard library from scratch in assembly language using NASM. I'm not doing this to use it in huge project, but just to train myself with assembly language making a project that I can put on GitHub.

Anyway, I'm facing something strange with the SYS_READ syscall. When I type a string in the standard input and I try to create a file with it as filename, it creates the file with some strange characters after it.

weird filename behavior

As you can see, I'm trying to create a Hello.txt file. The program was able to do so, but it added a dollar sign and a line return. I don't know why. I reserved 10 bytes in the .bss section as my filename buffer, and I'm only using 8 bytes of it. I tried to use the full length and more to see what happened.

BUT, when I use GDB to see what's happening, there is not trace of a $ symbol. I don't know where it comes from : gdb

When I'm only using the full length, the file is made without weird symbols. When I use more, it acts the same, but with an overflow on the console (obviously).

I'm working on a 64bits Windows Subsystem Linux. The repo is at this address : https://github.com/Ximaz/nasm-stdlib . The issue I'm facing is stored into the main.asm file, at the root of the project, and all functions used are into the stdlib folder. I'm trying to not use libc functions such as scanf or else, because I want to do it from scratch + this issue can help me to learn more that I think.

Thanks in advance.

Ximaz
  • 198
  • 2
  • 9
  • You are not removing the terminating end of line from the input. Also you are ignoring the actual length of the input which is bad practice. – Jester Apr 20 '22 at 13:21
  • I don't know how to remove the terminating end of line from the input. What do you mean by ignoring the actual length of the input ? – Ximaz Apr 20 '22 at 13:23
  • 1
    The system call returns the number of bytes actually read. You should use that to check the last character. If it's a newline, remove it. If it's not a newline that means the user provided longer input so handle that, e.g. by printing an error message. The `$` is not in the filename, the `$'\n'` is shell syntax for a newline, it's only shown like that by your `ls`. – Jester Apr 20 '22 at 13:25
  • 1
    Next time, do not post pictures of text! Instead copy/paste the text itself directly into your question. Use a `
    ` block if appropriate.
    – fuz Apr 20 '22 at 16:03
  • I'm not sure that posting gdb result as code would be a greater idea. here, it's more visuable, same thing with my prompt's output. I would agree if I posted a screen of assembly code, but those screens aren't code. – Ximaz Apr 20 '22 at 18:05

1 Answers1

2

Thanks to Jester :

I didn't notice that the stdin call returned the read bytes into rax, which is what I need to put a termination byte. Since the string contains the \n, I had to decrement it before puting the null byte at the \n position.

Ximaz
  • 198
  • 2
  • 9
  • 1
    Note that a `read` system call doesn't guarantee that the input will end with a `\n`, even if it comes from the terminal. A user can submit input with the EOF key (control-d by default) instead of EOL (end-of-line = newline). Try it by running `cat` on a terminal. (And maybe attach `strace -p $(pidof cat)` to it in another terminal window). But for toy programs, sure, safe enough to just blindly chop off one character from the input without checking that it's a newline. – Peter Cordes Apr 21 '22 at 00:09
  • Thanks for the note. I'm going to search on how to correctly crop the string if it's not a ``\n`` string, because it's a toy project, sure, but I want to make it right as much as I can. I appreciate it. – Ximaz Apr 21 '22 at 11:00
  • 1
    Check if last byte of the read (`[rsi+rax-1]` ) is a `\n`, and if so overwrite it. If not, overwrite the byte after that. (Make sure your buffer is big enough that there's room for a terminator after read returns the full number of bytes you passed it.) – Peter Cordes Apr 21 '22 at 11:15
  • Wow, I wan't expecting an answer, but any thanks for the help. I'm going to try to implement it right now. :-) – Ximaz Apr 21 '22 at 12:24