正确的用户输入-x86 Linux程序集 [英] Correct User Input - x86 Linux Assembly

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

问题描述

所以我正在使用NASM为Linux开发一个x86汇编程序。这个程序基本上是要求用户输入他们的名字和他们最喜欢的颜色。在这样做并将两个字符串存储在.bss部分中声明的变量中之后,程序将打印"不可能用户名最喜欢的颜色也是我最喜欢的颜色!"

我遇到的问题是输出中有很大的空格,因为我不知道用户输入的字符串有多长,只知道我声明的缓冲区的长度。

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

我正在做的事情的代码在上面。有没有人有好的方法来计算输入的字符串的长度,或者一次接受一个字符来计算字符串的长度?

提前谢谢您。

推荐答案

getname返回int80之后,EAX将包含实际读取的字节数,或错误指示为负。

您应该

  1. 检查错误返回
  2. 存储返回值,因为它提供了输入的长度

C:中的等效代码

char name[20];
int rc;

rc = syscall(SYS_read, 0, name, 20-1);  // leave space for terminating NUL
if (rc < 0) {
  // handle error
} else {
  name[rc] = '';                      // NUL terminate the string
}

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

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