简体   繁体   English

Windows程序集(x86)从Stdin读取并输出到Stdout

[英]Windows Assembly (x86) Read from Stdin and output to Stdout

I recently figured out how to write to stdout in assembly but am now having trouble reading from stdin, and outputting what I read back to stdout. 我最近想出了如何在汇编中写入stdout,但现在我无法从stdin读取,并将我读回的内容输出到stdout。 This is the code I have so far: 这是我到目前为止的代码:

.386
.model flat, stdcall

WriteFile PROTO STDCALL:DWORD, :PTR, :DWORD, :PTR DWORD, :PTR OVERLAPPED
ReadFile  PROTO STDCALL:DWORD, :PTR, :DWORD, :PTR DWORD, :PTR OVERLAPPED
GetStdHandle PROTO STDCALL:DWORD
.data


.data?
input DW ?
input_size DD ?
read DD ?

.code
main:
    INVOKE GetStdHandle, -10
    INVOKE ReadFile, eax, OFFSET input, input_size, read, 0
    INVOKE GetStdHandle, -11
    INVOKE WriteFile, eax, OFFSET input, OFFSET input_size, read, 0
    RET 
END main

I'm pretty sure I'm reading it wrong. 我很确定我读错了。 I am pretty sure that input_size and read are not behaving as expected (if I replace input_size with a number it displays a space number times) which is contributing to my problem (when I enter my input and hit return it simply displays nothing). 我敢肯定, input_sizeread不按预期行为(如果我更换input_sizenumber它会显示一个空number这有助于我的问题次)(当我进入我的输入并回车,它不显示任何内容)。

I have fumbled around with this for quite some time and would appreciate any help. 我已经在这方面摸索了很长一段时间,并希望得到任何帮助。 (I am doing this just to learn this is not homework). (我这样做只是为了学习这不是功课)。

My question essentialy is what am I doing wrong? 我的问题本质上是我做错了什么?

You are allocating only two bytes for the input buffer: 您只为输入缓冲区分配两个字节:

input DW ?

Your input size is zero, causing ReadFile to read a maximum of 0 bytes: 您的输入大小为零,导致ReadFile读取最多0个字节:

input_size DD ?

nNumberOfBytesToRead should be passed as a value and not a pointer. nNumberOfBytesToRead应该作为值而不是指针传递。 And you want to write as many bytes as were input in the Readfile: 并且您希望写入与Readfile中输入的字节数一样多的字节:

INVOKE WriteFile, eax, OFFSET input, OFFSET input_size, read, 0
INVOKE WriteFile, eax, OFFSET input, read, read, 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM