Linux x86 ASM - 获取用户输入
希望这是一个简单的问题:
首先,我想知道是否有人知道如何在 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 技术交流群。

发布评论
评论(3)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
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.