所以我正在使用NASM开发Linux的x86汇编程序。该程序基本上询问用户他们的名字和他们喜欢的颜色。执行此操作并将两个字符串存储在.bss部分中声明的变量后,程序将打印“No way 用户名,最喜欢的颜色也是我最喜欢的颜色!
我遇到的问题是输出中有很多空格,因为我不知道用户输入的字符串有多长,只有我声明缓冲区的长度。
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
我正在做的代码就在上面。有没有人有一个很好的方法来查找输入的字符串的长度,或者一次接收一个字符来找出字符串的长度?
提前谢谢。
答案 0 :(得分:1)
int80
中的getname
返回后,EAX
将包含实际读取的字节数或负错误指示。
你应该
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] = '\0'; // NUL terminate the string
}