Windows 程序集 (x86) 从标准输入读取并输出到标准输出 [英] 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. 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
我很确定我读错了.我很确定 input_size
和 read
没有按预期运行(如果我用 number
替换 input_size
显示一个空格 number
次),这会导致我的问题(当我输入我的输入并点击返回时,它只是不显示任何内容).
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).
我已经在这个问题上摸索了很长一段时间,希望得到任何帮助.(我这样做只是为了了解这不是家庭作业).
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 ?
您的输入大小为零,导致 ReadFile 读取最多 0 个字节:
Your input size is zero, causing ReadFile to read a maximum of 0 bytes:
input_size DD ?
nNumberOfBytesToRead 应该作为值而不是指针传递.并且您想写入与 Readfile 中输入的字节一样多的字节:
nNumberOfBytesToRead should be passed as a value and not a pointer. And you want to write as many bytes as were input in the Readfile:
INVOKE WriteFile, eax, OFFSET input, OFFSET input_size, read, 0
INVOKE WriteFile, eax, OFFSET input, read, read, 0
这篇关于Windows 程序集 (x86) 从标准输入读取并输出到标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!