Linux x86 ASM - 获取用户输入

发布于 2024-12-10 09:36:56 字数 1838 浏览 0 评论 0原文

希望这是一个简单的问题:

首先,我想知道是否有人知道如何在 Linux 上使用 x86 NASM 语法汇编来获取用户输入。现在,我有:

section .data
    greet:       db 'Hello!', 0Ah, 'What is your name?', 0Ah  ;simple greeting
    greetL:      equ $-greet                                  ;greet length
    colorQ:      db 'What is your favorite color?'            ;color question
    colorL:      equ $-colorQ                                 ;colorQ length
    suprise1:    db 'No way '                               
    suprise1L    equ $-suprise1
    suprise3:    db ' is my favorite color, too!', 0Ah

section .bss 
    name:        resb 20                                      ;user's name
    color:       resb 15                                      ;user's color

section .text
    global _start
_start:

    greeting:
         mov eax, 4
         mov ebx, 1
         mov ecx, greet
         mov edx, greetL
         int 80                                               ;print greet

    getname:
         mov eax, 3
         mov ebx, 0
         mov ecx, name
         mov edx, 20
         int 80                                               ;get name

    askcolor:
         ;asks the user's favorite color using colorQ

    getcolor: 
         mov eax, 3
         mov ebx, 0
         mov ecx, name
         mov edx, 20
         int 80

    thesuprise:
         mov eax, 4
         mov ebx, 1
         mov ecx, suprise1
         mov edx, suprise1L
         int 80 

         mov eax, 4
         mov ebx, 1
         mov ecx, name
         mov edx, 20
         int 80 

         ;write the color

         ;write the "suprise" 3

         mov eax, 1
         mov ebx, 0
         int 80

所以它所做的就是询问名称和颜色,然后说,“不可能 --name-- --color-- 也是我最喜欢的颜色。

我需要帮助的是如何找到上面的“名称”和“颜色”变量在用户输入后有多长,否则,我会在它们之间得到一堆又长又讨厌的空格,因为我只知道它们的最大大小是我之前声明的

。寻求您的任何帮助。

This is, hopefully, a simple question:

First, I would like to know if anyone has an idea of how to get user input using x86 NASM Syntax Assembly on Linux. Right now, I have:

section .data
    greet:       db 'Hello!', 0Ah, 'What is your name?', 0Ah  ;simple greeting
    greetL:      equ $-greet                                  ;greet length
    colorQ:      db 'What is your favorite color?'            ;color question
    colorL:      equ $-colorQ                                 ;colorQ length
    suprise1:    db 'No way '                               
    suprise1L    equ $-suprise1
    suprise3:    db ' is my favorite color, too!', 0Ah

section .bss 
    name:        resb 20                                      ;user's name
    color:       resb 15                                      ;user's color

section .text
    global _start
_start:

    greeting:
         mov eax, 4
         mov ebx, 1
         mov ecx, greet
         mov edx, greetL
         int 80                                               ;print greet

    getname:
         mov eax, 3
         mov ebx, 0
         mov ecx, name
         mov edx, 20
         int 80                                               ;get name

    askcolor:
         ;asks the user's favorite color using colorQ

    getcolor: 
         mov eax, 3
         mov ebx, 0
         mov ecx, name
         mov edx, 20
         int 80

    thesuprise:
         mov eax, 4
         mov ebx, 1
         mov ecx, suprise1
         mov edx, suprise1L
         int 80 

         mov eax, 4
         mov ebx, 1
         mov ecx, name
         mov edx, 20
         int 80 

         ;write the color

         ;write the "suprise" 3

         mov eax, 1
         mov ebx, 0
         int 80

So what it does is ask for a name and color, and say, "No way --name-- --color-- is my favorite color, too.

What I need help on is how to find how long the "name" and "color" variables above are after the user enters them. Otherwise, I get a bunch of long, nasty spaces in between because I only know that the max size they can be is what I declared before.

Thank you for any and all help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

開玄 2024-12-17 09:36:56

read 系统调用返回 eax 寄存器中读取的字节数。如果这个数字< 0,存在某种读取错误。

The read system call returns the number of bytes read in the eax register. If this number is < 0, there was a read error of some sort.

﹂绝世的画 2024-12-17 09:36:56

您将循环调用 read 。

最简单的方法(尽管不是最好的)是一次读取一个字节,查找 LF(字节 10)。

You will be calling read in a loop.

The easiest way, although not the best, is to read one byte at a time looking for LF (byte 10).

探春 2024-12-17 09:36:56

我知道这已经很老了,但对于将来关注这个的人来说,还有另一种方法可以完成OP所要求的任务,基本上只需要一行。它可能并不理想,但对于这样的事情,它应该工作得很好。基本上,我们不要试图计算出用户输入的单词的长度,而是假设他们要输入“红色”或“橙色”等内容。没有花哨的颜色。所以让我们假设最长的单词颜色就像8个字符。话虽这么说,我们可以做以下事情。

.bss 
     color:     resb     8

同样,它并不理想,但它确实有效,在这种情况下,一些额外的字节不应该对其产生太大影响。

I know this is quite old but for anyone in the future looking at this, there is another way to accomplish what the OP is asking for, with basically 1 line. It may not be ideal, but for something like this, it should work fine. Basically, instead of trying to figure out the length of the word that the user input, lets just assume that theyre going to input something like 'RED' or 'ORANGE', etc.. no fancy colors.. So lets assume that the longest color would be like 8 characters. That being said, we can do the following.

.bss 
     color:     resb     8

Again, its not ideal but it does work and in this case, a few extra bytes shouldnt effect it too much.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文