Here one little more addition to this and a fix of some usless buffer: Now checks if you didn't enter any char (only CR key), only accept 2 chars as minimum for the name and a maximum of 20. The username buffer was useless (i think i forget it there... :lol:) so i deleted.
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
.data?
hconsoleinput dd ?
hconsoleoutput dd ?
charswritten dd ?
charsread dd ?
charsattset dd ?
.data
programname db "THE CONSOLE WAY!",0
welcome db "Hi, welcome to this program!",0ah,0dh
text db 256 dup(" "),0
promptname db "Enter your name: "
saveit db 0ah,0ah,"Exit program?(Y/N): "
usernbuff db 256 dup(" "),0
inerror db "Error! names allowed of 20 chars as maximun only...",0
inerror2 db "Error! you must put a name here of 2 chars as minimum...",0
cursorp proto :word,:word
paintb proto :word,:word
.code
main proc
invoke GetStdHandle,STD_INPUT_HANDLE
mov hconsoleinput,eax
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov hconsoleoutput,eax
invoke SetConsoleTitle,offset programname
again:
invoke cursorp,0,0 ; Locates the cursor.
invoke paintb,0,0 ; Clears the screen.
invoke SetConsoleTextAttribute,hconsoleoutput,\ ; Set the text and background
FOREGROUND_BLUE or FOREGROUND_GREEN or FOREGROUND_INTENSITY or\ ; colors for the console.
BACKGROUND_GREEN
invoke WriteConsole,hconsoleoutput,offset welcome,sizeof welcome,\ ; Welcome message in screen.
offset charswritten,NULL
invoke WriteConsole,hconsoleoutput,offset promptname,sizeof promptname,\ ; Prompt for the user's name.
offset charswritten,NULL
invoke ReadConsole,hconsoleinput,offset usernbuff,sizeof usernbuff,\
offset charsread,NULL
cmp charsread,23 ; More chars than allowed?:
jb all_ok ; -no, so continue in all_ok.
invoke cursorp,0,0 ; -yes, so:
invoke paintb,0,0 ; *clears the screen.
invoke WriteConsole,hconsoleoutput,offset inerror,sizeof inerror,\ ; *show error message.
offset charswritten,NULL
invoke SetConsoleMode,hconsoleinput,ENABLE_LINE_INPUT
invoke ReadConsole,hconsoleinput,offset text,sizeof text,\ ; *Waits for the CR key.
offset charsread,NULL
invoke SetConsoleMode,hconsoleinput,ENABLE_ECHO_INPUT or\ ; Allows inputs again (256 chars).
ENABLE_LINE_INPUT
jmp again
all_ok:
cmp charsread,2
ja all_ok2 ; No input at all (just CR)?
invoke cursorp,0,0 ; if so, set cursor location (0,0)
invoke paintb,0,0 ; clears the screen
invoke WriteConsole,hconsoleoutput,offset inerror2,sizeof inerror2,\ ; show error message.
offset charswritten,NULL
invoke SetConsoleMode,hconsoleinput,ENABLE_LINE_INPUT ; Just CR accepted.
invoke ReadConsole,hconsoleinput,offset text,sizeof text,\
offset charsread,NULL
invoke SetConsoleMode,hconsoleinput,ENABLE_ECHO_INPUT or\ ; All keys accepted from now
ENABLE_LINE_INPUT ; again.
jmp again
all_ok2: ; Correct name format until here.
invoke WriteConsole,hconsoleoutput,offset saveit,sizeof saveit,\
offset charswritten,NULL
invoke ReadConsole,hconsoleinput,offset text,sizeof text,\
offset charsread,NULL
invoke ExitProcess,0
main endp
paintb proc colx:word,rowy:word ; Fill into green all the screen,
local cc:COORD ; (background and foreground).
mov ax,colx
mov cc.x,ax
mov ax,rowy
mov cc.y,ax
mov eax,cc
invoke FillConsoleOutputAttribute,hconsoleoutput,BACKGROUND_GREEN or\
FOREGROUND_GREEN,30000,eax,offset charsread
ret
paintb endp
cursorp proc col:word,row:word ; Sets the cursor location.
local cc:COORD
mov ax,col
mov cc.x,ax
mov ax,row
mov cc.y,ax
mov eax,cc
invoke SetConsoleCursorPosition,hconsoleoutput,eax
ret
cursorp endp
end main