Hello, I wanted to write a simple Win64 Hello World program that should greet the user personally. Unfortunately, the program does not work properly. It does not print the read name and exits if the name entered was too long. What is my mistake?
bits 64
extern ExitProcess
extern GetStdHandle
extern WriteConsoleA
extern ReadConsoleA
STD_INPUT_HANDLE equ -10
STD_OUTPUT_HANDLE equ -11
section .bss
CharsWritten resq 1
CharsRead resq 1
InputBuffer resq 20
StdOut resq 1
StdIn resq 1
namelen resq 1
section .text
global main
main:
sub rsp, 0x28
mov rcx, STD_OUTPUT_HANDLE
call GetStdHandle
mov [StdOut], rax
mov rcx, STD_INPUT_HANDLE
call GetStdHandle
mov [StdIn], rax
mov rcx, [StdOut]
mov rdx, msg1
mov r8, msg1.len
mov r9, CharsWritten
mov rax, qword 0
mov qword [rsp+0x28], rax
call WriteConsoleA
mov rcx, [StdIn]
mov rdx, InputBuffer
mov r8, CTR1
mov r9, CharsRead
mov rax, qword 0
mov qword [rsp+0x28], rax
call ReadConsoleA
mov rcx, [StdOut]
mov rdx, msg2
mov r8, msg2.len
mov r9, CharsWritten
mov rax, qword 0
mov qword [rsp+0x28], rax
call WriteConsoleA
mov rax, qword [CharsRead]
sub rax, 2
mov qword [namelen], rax
mov rcx, [StdOut]
mov rdx, InputBuffer
mov r8, namelen
mov r9, CharsWritten
mov rax, qword 0
mov qword [rsp+0x28], rax
call WriteConsoleA
mov rcx, [StdOut]
mov rdx, msg3
mov r8, msg3.len
mov r9, CharsWritten
mov rax, qword 0
mov qword [rsp+0x28], rax
call WriteConsoleA
mov rcx, [StdIn]
mov rdx, InputBuffer
mov r8, CTR2
mov r9, CharsRead
mov rax, qword 0
mov qword [rsp+0x28], rax
call ReadConsoleA
add rsp, 0x28
xor rcx, rcx
call ExitProcess
section .data
msg1 db "Hello, World!", 10, "Please enter your name (maximum 20 characters): "
msg1.len equ $ - msg1
msg2 db "Hello, "
msg2.len equ $ - msg2
msg3 db "!", 10, "Please press enter to quit."
msg3.len equ $ - msg3
CTR1 equ 20
CTR2 equ 0