This looks like the same issue:
Linux write sys call using string on stack
Disclaimer: I am running 32-bit hardware and have no experience with 64-bit code. It "might" be a mistake to use a 32-bit reference (Jon Bartlett's excellent PGU!) for 64-bit code. As indicated in the link above (and on the right), you're using 32-bit system call numbers, putting the parameters in registers suitable for 32-bit code, and using the 32-bit int 0x80
. I'm told that this works (still), and you confirm that it works with buffer in .bss
. I think that %rsp
is "too high", as indicated in the link.
In any case, sys_read
does not return a zero-terminated string, and sys_write
wouldn't pay any attention to it if it did. sys_write
writes the number of bytes in %edx
(%rdx
) regardless of whether it's "garbage" or not. sys_read
returns the number of bytes actually entered in %eax
(%rax
), and this is what you want to put in %edx
(%rdx
) (same register for 32- or 64-bit code in this case) for the sys_write
. This is not your problem, but it's still "wrong".
I've seen this issue of "excess" appearing at the command prompt if the pesky user types more than is allowed in %edx
in 32-bit code. It is potentially "dangerous"! It is probably a good idea to "flush the buffer" if necessary. If %eax
(%rax
) is less than %edx
(%rdx
), you're okay. If they're equal, check the last byte in the buffer for the linefeed (ascii code 0xa). If it's there, you're okay. If not, keep reading until you find the linefeed. This is probably "overkill" for a toy program where you're the only user, but it's still a good idea to be aware of the issue.