Linux NASM x86_64删除新行字符并在字符串末尾添加0
使用系统读取从控制台获取字符串时,如何删除添加在字符串末尾的新行字符? 我想在其末尾添加一个0,以使用该字符串打开文件 我得到的信息如下:Linux NASM x86_64删除新行字符并在字符串末尾添加0,linux,nasm,x86-64,Linux,Nasm,X86 64,使用系统读取从控制台获取字符串时,如何删除添加在字符串末尾的新行字符? 我想在其末尾添加一个0,以使用该字符串打开文件 我得到的信息如下: mov rdx,name_len ; size_t count mov rsi,name ; char *buf mov rdi,0 ; int fd, 0 for stdin mov rax,0 ; system read syscall read返回字符数,因此
mov rdx,name_len ; size_t count
mov rsi,name ; char *buf
mov rdi,0 ; int fd, 0 for stdin
mov rax,0 ; system read
syscall
read
返回字符数,因此您可以索引到缓冲区中,并检查上次读取的字符是否为换行符。或者只需无条件地用0
覆盖它:
...
syscall ; rax = sys_read(0, buf, max_len)
mov byte [rsi + rax - 1], 0
这假定没有错误,并且输入字符串是用换行符而不是EOF提交的
(Linuxsyscall
s保留除RAX(返回值)和RCX/R11之外的所有regs,因此RSI仍然保留name
)