WriteConsoleA输出包含太多字符

问题描述 投票:1回答:1

我需要在第一行没有名称而第二行末尾带有星号的地方修复输出。

CONSOLE     equ     -11
KEYBOARD    equ     -10

extern  GetStdHandle:   PROTO
extern  WriteConsoleA:  PROTO
extern  ReadConsoleA:   PROTO
extern  ExitProcess:    PROTO

.data
prompt      byte    'What is your name? '
asterisk    byte    ' ****************************'
oneAsterisk byte    '* '
myname      byte    40 dup(0)

.data?
stdin       qword ? 
stdout      qword ?
numWrite    qword ?
numRead     qword ?

.code
mainCRTStartup  PROC    ;Initialize the program

                sub rsp, 20     ;reserve shadow space

                ;Get handle id to console window & keyboard
                mov rcx, CONSOLE    ;/subsystem:console
                call GetStdHandle   ;handle in rax
                mov stdout, rax     ;save out handle
                mov rcx, KEYBOARD   ; keyboard code
                call GetStdHandle   ;handle in rax
                mov stdin, rax      ;save in handle

                ;Display message on console window
                mov rcx, stdout     ;parm1 = console handle
                lea rdx, prompt     ;parm2 = ascii message
                mov r8, lengthof prompt ;# bytes to display
                lea r9, numWrite    ;display message
                call WriteConsoleA  ;display message

                ;Read ASCII from the keyboard
                mov rcx, stdin      ;parm1 = keyboard handle
                lea rdx, myname     ;parm2 = ascii buffer
                mov r8, lengthof myname ;# bytes to read
                lea r9, numRead     ;& to store #bytes actually read
                call ReadConsoleA   ;get keystrokes

                ;Display first message on console window
                mov rcx, stdout             ;parm1 = console handle
                lea rdx, asterisk           ;parm2 = ascii message 
                ;# bytes to dispaly
                mov r8, lengthof asterisk   ;length
                add r8, 10
                sub r9, 2
                mov qword ptr [rsp + 32], 0 ;parm5 = 0 (unused)
                call WriteConsoleA          ;display message

                ;Display second message on console window
                mov rcx, stdout             ;parm1 = console handle
                lea rdx, oneAsterisk        ;parm2 = ascii message 
                ;# bytes to dispaly
                mov r8, lengthof oneAsterisk;length
                add r8, numRead             ;add length name 
                lea r9, numWrite            ;& to store #bytes written
                mov qword ptr [rsp + 32], 0         ;parm5 = 0 (unused)
                call WriteConsoleA      ;display message

                ;Display third message on console window
                mov rcx, stdout         ;parm1 = console handle
                lea rdx, asterisk       ;parm2 = ascii message 
                ;# bytes to dispaly
                mov r8, lengthof asterisk;length
                sub r9, 2
                mov qword ptr [rsp + 32], 0;parm5 = 0 (unused)
                call WriteConsoleA      ;display message



                ;Finalize program and exit
                add rsp, 20         ;restore shadow space
                mov rcx, 0          ;return code %errorlevel%
                call ExitProcess    ;exit
mainCRTStartup  ENDP
END

输出:

What is your name? Jeff
 ***************************** Jeff
 * Jeff
 ****************************
windows assembly x86-64 masm
1个回答
0
投票

似乎很明显;您将太长的长度传递给第一个WriteConsoleA。 mov r8, lengthof asterisk后跟add r8, 10,因此您打印了10个额外的字节,该字节足够长,可以包含简短的用户输入。 (以及oneCaster中的"* "

如果只想打印一个数组,请使用其长度而不是太长的数组。 (不过,您可能想要添加换行符,以便下一个输出可以在新行上开始。您的使用输入通常会包含换行符。)

© www.soinside.com 2019 - 2024. All rights reserved.