Linux x86的ASM - 获取用户输入 [英] Linux x86 ASM - Getting User Input

查看:161
本文介绍了Linux x86的ASM - 获取用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is, hopefully, a simple question:

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

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

那么,它的作用是要求一个名称和颜色,并说,没办法--name-- --color--是我最喜欢的颜色了。

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.

推荐答案

读取系统调用返回在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.

这篇关于Linux x86的ASM - 获取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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