N
N
Nikita Kargin2021-06-12 10:29:51
assembler
Nikita Kargin, 2021-06-12 10:29:51

Why is ReadConsole not working properly?

.386
.model flat,stdcall
option casemap:none
include ..\INCLUDE\kernel32.inc 
include ..\INCLUDE\user32.inc 
includelib ..\LIB\kernel32.lib 
includelib ..\LIB\user32.lib

.data
state     db 4 dup(7)
continue  db 1
input	  dd ?
input_buf dd 3 dup(?)
read	  db ?
output    dd ?
written   db ?
.code
start:
invoke AllocConsole
invoke GetStdHandle, -10
mov input,eax
invoke GetStdHandle, -11
mov output,eax
main:
invoke  Sleep, 2000
invoke ReadConsole, ADDR input, ADDR input_buf, 1, ADDR read, 0

invoke WriteConsoleA, output, ADDR input_buf, 3, ADDR written, 0
invoke ExitProcess,0  
end start

There is such a code. As planned, it should read user input and display it on the screen. But while the program is running, I cannot enter anything into the console, and the characters I entered are shown only after the end of the program, as if it were a command line command

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pfemidi, 2021-06-12
@Pakonigoosy

First, don't

invoke ReadConsole, ADDR input, ADDR input_buf, 1, ADDR read, 0

a
invoke ReadConsole, input, ADDR input_buf, 1, ADDR read, 0

In this case, we need not the address of the variable, but its contents. Therefore, by the way, nothing is read.
Secondly, the variables readand writtenmust be of size not db, but dd. ReadConsole returns DWORD, not BYTE, and by itself the returned value readclogs the previously received outputone, zeros are obtained there and therefore nothing is displayed.
Thirdly, judging by
invoke ReadConsole, input, ADDR input_buf, 1, ADDR read, 0
ONE character is read , and output
invoke WriteConsoleA, output, ADDR input_buf, 3, ADDR written, 0

somehow three.
Well, purely cosmetic:
a) why is input_buf declared as a DWORD array? it must be declared as a BYTE array
input_buf db КОЛИЧЕСТВО_БАЙТ_ПЛАНИРУЕМОЕ_ПРОЧИТАТЬ dup(?)

b) what for call Sleep? he is not needed there
c) why ReadConsole, but at the same time WriteConsoleA? Either all *A, or all functions (which IMHO is better) all functions without *A at the end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question