To read characters entered by the user, call GetStdHandle to get the console’s standard input handle and call ReadConsole, using the same input handle. The following ReadConsole program demonstrates the technique. Notice that Win32 API calls are compatible with the Irvine32 library, so we are able to call DumpRegs at the same time we call Win32 functions:
TITLE Read From the Console (ReadConsole.asm) INCLUDE Irvine32.inc BufSize = 80 .data buffer BYTE BufSize DUP(?),0,0 stdInHandle HANDLE ? bytesRead DWORD ? .code main PROC ; Get handle to standard input INVOKE GetStdHandle, STD_INPUT_HANDLE mov stdInHandle,eax ; Wait for user input INVOKE ReadConsole, stdInHandle, ADDR buffer, BufSize, ADDR bytesRead, 0 ; Display the buffer mov esi,OFFSET buffer mov ecx,bytesRead mov ebx,TYPE buffer call DumpMem exit main ENDP END main
If the user enters “abcdefg”, the program generates the following output. Nine bytes are inserted in the buffer: “abcdefg” plus 0Dh and 0Ah, the end-of-line characters(First a Carriage-Return(ASCII CODE:0D), then a new line(ASCII CODE:0A)) inserted when the user pressed the Enter key. bytesRead equals 9:
Dump of offset 00404000
-------------------------------
61 62 63 64 65 66 67 0D 0A