0

当我从用户那里获取玩家姓名时,如何限制输入的大小?我是这样使用的:

player1 byte 36 dup(' '),0

但是当玩家输入超过 36 个字符时,计算机会发出警报声,并且剩余部分正在写入 player2 名称的一部分。

4

1 回答 1

0

来自: http: //lcs.syr.edu/faculty/pease/handouts/CSE%20281/Irvine%20Programming%20Examples/Lib32/Irvine32_Library/Irvine32.asm

;--------------------------------------------------------
ReadString PROC
    LOCAL bufSize:DWORD, saveFlags:DWORD, junk:DWORD
;
; Reads a string from the keyboard and places the characters
; in a buffer.
; Receives: EDX offset of the input buffer
;           ECX = maximum characters to input (including terminal null)
; Returns:  EAX = size of the input string.
; Comments: Stops when Enter key (0Dh,0Ah) is pressed. If the user
; types more characters than (ECX-1), the excess characters
; are ignored.
; Written by Kip Irvine and Gerald Cahill
;
; Last update: 11/19/92, 03/20/2003
;--------------------------------------------------------`

所以答案是您应该将 ECX 设置为您能够安全地接受到缓冲区中的最大字符数,包括 null。也许您没有正确设置该值 - 您可以尝试手动将其人为缩短,例如 10 个字符并验证它是否有效。

于 2013-05-23T22:19:39.300 回答