在从文件中重定向时,读取到newline字符后,读取nasm中的syscall不会停止

发布于 2025-01-25 10:53:43 字数 1055 浏览 2 评论 0原文

以下组装代码应该以两条不同的行读取两个单独的字符串。果然,当用户通过终端给出输入时,它确实可以正常工作:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文