重置字符串变量以在循环中打印多个用户输入(NASM 程序集) [英] Reset a string variable to print multitple user inputs in a loop (NASM Assembly)

查看:49
本文介绍了重置字符串变量以在循环中打印多个用户输入(NASM 程序集)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 64 位 Linux 系统,我正在尝试使用 NASM 来构建一个程序,该程序要求用户输入,然后将其打印出来.之后,用户可以选择再次执行相同操作或退出.

I'm using a 64 bits Linux system, and I'm trying to use NASM to build a program that asks the user for input, and then prints it. Afterwards, the user can choose to do the same again, or exit.

我的问题是用于存储用户输入的变量text"在每次执行结束时不会重置,因此会发生以下情况:

My issue is that the variable 'text', which is used to store the user's input, is not reset at the end of each execution, so something like this happens:

  • 用户第一次输入文本:Helloooooooooooooooooooooooooooo

  • User enters text the 1st time: Helloooooooooooooooooooooooooooooo

输出:Hellooooooooooooooooooooooooooooo

Output: Helloooooooooooooooooooooooooooooo

用户第二次输入文本:Boom!

User enters text the 2nd time: Boom!

输出:砰!呜呜呜呜呜呜呜

Output: Boom! oooooooooooooooooooooooooooo

Helloooooo"的一部分从第一次执行开始,我不知道如何防止这种情况发生.

Part of the "Helloooooo" from the first execution is showing up, and I don't know how to prevent that from happening.

感谢您的帮助,因为我刚刚开始了解这一点.

I'd appreciate any help, as I'm just starting to learn about this.

谢谢

这就是我所做的:

section .data
prompt db "Type your text here.", 0h

retry db "Wanna try again? If yes, enter y. If not, enter anything else to close the program", 0h

section .bss
text resb 255
choice resb 2

section .text
    global _start

_start:

    mov rax, 1      ;Just asking the user to enter input
    mov rdi, 1
    mov rsi, prompt
    mov rdx, 21
    syscall


    mov rax, 0      ;Getting input and saving it on var text
    mov rdi, 0
    mov rsi, text
    mov rdx, 255
    syscall

    mov rax, 1      ;Printing the user input
    mov rdi, 1
    mov rsi, text
    mov rdx, 255
    syscall

    mov rax, 1      ;Asking if user wants to try again
    mov rdi, 1
    mov rsi, retry
    mov rdx, 83
    syscall

    mov rax, 0      ;Getting input and saving it on var choice
    mov rdi, 0
    mov rsi, choice
    mov rdx, 2
    syscall

    mov r8b, [choice] ;If choice is different from y, go to end and close the program. Otherwhise, go back to start.
    cmp byte r8b, 'y'
    jne end
    jmp _start

    end:
    mov rax, 60      
    mov rdi, 0       
    syscall

推荐答案

    mov rax, 1      ;Printing the user input
    mov rdi, 1
    mov rsi, text
    mov rdx, 255
    syscall

无论输入多少个字符,您总是要写 255 个字符.如果用户输入少于 255,您将写入 text 缓冲区中的任何其他内容,可能是前一次迭代留下的数据.

You always write 255 characters, no matter how many were input. If the user input fewer than 255, you'll write whatever else happens to be in your text buffer, possibly data left over from a previous iteration.

当您发出 read 系统调用(即 syscallrax=0)时,它会在 rax 中返回code> 指示读取字符数的值(或文件末尾为 0,或错误时为负数).只有那么多字符已写入缓冲区;其余部分不变.

When you issue the read system call (i.e. syscall with rax=0), it will return in rax a value indicating the number of characters read (or 0 on end-of-file, or a negative number on error). Only that many characters have been written into the buffer; the rest of it is unchanged.

没有必要重置"缓冲区的剩余内容,即使你在它上面写零或其他东西,它只会导致你输出一堆不需要的零字节 - 事实上你已经这样做了.您可能在终端上看不到它们,但如果您将输出重定向到文件,它们就会在那里.

It's not necessary to "reset" the remaining contents of the buffer, and even if you write zeros over it or something, it will only result in you outputting a bunch of unwanted zero bytes - which in fact you already do. You may not see them on a terminal, but if you redirect your output to a file, they'll be there.

所以你应该将从 read 返回的值,如果它是正的,在 rdxwrite (系统调用 rax=1) 而不是总是使用 255.那么你只会写读到的内容,缓冲区的剩余内容是无关紧要的,因为它们不会被使用.

So you should pass the value returned from read , if it's positive, in rdx to write (syscall with rax=1) instead of always using 255. Then you will only write what was read, and the remaining contents of the buffer are irrelevant as they will not be used.

这篇关于重置字符串变量以在循环中打印多个用户输入(NASM 程序集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆