在从文件中重定向时,读取到newline字符后,读取nasm中的syscall不会停止
以下组装代码应该以两条不同的行读取两个单独的字符串。果然,当用户通过终端给出输入时,它确实可以正常工作:
section .bss
str1 resb 100
str2 resb 100
section .text
global _start
_start:
;Reading the first string
mov rax, 3
mov rbx, 0
mov rcx, str1
mov rdx, 100
int 0x80
;Reading the second string
mov rax, 3
mov rbx, 0
mov rcx, str2
mov rdx, 100
int 0x80
Exit:
mov rax, 1
mov rbx, 0
int 0x80
要运行此程序(保存在名为a.asm
的文件中),我在终端中运行以下命令
nasm -f elf64 ./a.asm
ld -o ./a -e _start ./a.o
./a
:读两条线和停止的两个字符串。但是,当我设置一个输入文件(例如input.txt
)时,它不会读取第二个字符串;通过第一个读取系统调用读取整个文件内容。 input.txt
的内容:
Hello1
Hello2
要通过终端设置输入文件,我使用以下命令:
./a < input.txt
据我了解,问题是,当程序从文件中读取时,当呼叫达到\ n
字符时,它不会停止,并且仅在文件末尾到达null
字符时停止:因此,仅读取一个字符串不是问题。
我尝试用\ n
角色用\ r
替换,但行不通。
我该如何解决?
The following assembly code is supposed to read two separate strings in two different lines; and sure enough, it does work properly when the input is given by the user via terminal:
section .bss
str1 resb 100
str2 resb 100
section .text
global _start
_start:
;Reading the first string
mov rax, 3
mov rbx, 0
mov rcx, str1
mov rdx, 100
int 0x80
;Reading the second string
mov rax, 3
mov rbx, 0
mov rcx, str2
mov rdx, 100
int 0x80
Exit:
mov rax, 1
mov rbx, 0
int 0x80
To run this program (saved in a file named a.asm
), I run the following commands in terminal:
nasm -f elf64 ./a.asm
ld -o ./a -e _start ./a.o
./a
It reads two strings in two lines and halts. But when I set an input file such as input.txt
, it does not read the second string; the whole file content is read by the first read system call. The contents of input.txt
:
Hello1
Hello2
To set the input file via terminal, I use the following command:
./a < input.txt
The problem, as I understand it, is that when the program is reading from a file, the read system call does not stop when it reaches the \n
character and only stops when it reaches the NULL
character at the end of the file: So reading only one string is not a problem.
I have tried replacing the \n
character with \r
, but did not work.
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论