【发布时间】:2014-05-05 08:35:43
【问题描述】:
我正在学习 GNU/Linux 上的 x86 汇编,我正在尝试编写一个程序,该程序从标准输入读取用户输入并将其打印到标准输出。
以下代码确实有效,但如果用户输入的字符串的大小小于 100 字节,它会打印额外的字符。
section .data
str: db 100 ; Allocate buffer of 100 bytes
section .bss
section .text
global _start
_start:
mov eax, 3 ; Read user input into str
mov ebx, 0 ; |
mov ecx, str ; | <- destination
mov edx, 100 ; | <- length
int 80h ; \
mov eax, 4 ; Print 100 bytes starting from str
mov ebx, 1 ; |
mov ecx, str ; | <- source
mov edx, 100 ; | <- length
int 80h ; \
mov eax, 1 ; Return
mov ebx, 0 ; | <- return code
int 80h ; \
如何可靠地计算用户输入字符串的长度?
如何避免打印多余的字符?
【问题讨论】: