简体   繁体   English

无法在x86程序集中打印回输入的文本

[英]Unable to print back entered text in x86 assembly

I have an Assembly program here which is supposed to print a string, allow the user the enter some text, print that exact same text again and then wait for a keypress to terminate the program, using only Win32 native functions. 我在这里有一个汇编程序,该程序应该打印一个字符串,允许用户输入一些文本,再次打印完全相同的文本,然后等待仅使用Win32本机函数的按键终止程序。
The problem is that everything seems to work, except printing the user entered string. 问题是,除了打印用户输入的字符串外,其他所有内容似乎都可以正常工作。 It just prints a blank new line. 它只是打印一个空白的新行。 Here's the code: 这是代码:

global _main

extern _GetStdHandle@4
extern _WriteFile@20
extern _ReadFile@20
extern _ExitProcess@4

section .text

_main:
    mov ebp, esp
    sub esp, 12

    push -11
    call _GetStdHandle@4
    mov ebx, eax

    push 0
    push dword [ebp - 12]
    lea ecx, [_msg_end - _msg]
    push ecx
    lea edx, [_msg]
    push edx
    push ebx
    call _WriteFile@20

    push -10
    call _GetStdHandle@4
    mov ebx, eax

    push 0
    lea ecx, [ebp - 8]
    push ecx
    push 20
    lea edx, [ebp - 4]
    push edx
    push ebx
    call _ReadFile@20

    push -11
    call _GetStdHandle@4
    mov ebx, eax

    push 0
    push dword [ebp - 12]
    lea ecx, [ebp - 8]
    push ecx
    lea edx, [ebp - 4]
    push edx
    push ebx
    call _WriteFile@20

    push -10
    call _GetStdHandle@4
    mov ebx, eax

    push 0
    lea ecx, [ebp - 8]
    push ecx
    push 1
    lea edx, [ebp - 4]
    push edx
    push ebx
    call _ReadFile@20

    push 0
    call _ExitProcess@4
_msg:
    db "Hello, world!", 10
_msg_end:

EDIT - Here's the working code: 编辑 -这是工作代码:

global _main

extern _GetStdHandle@4
extern _ReadFile@20
extern _WriteFile@20
extern _ExitProcess@4

section .bss
_input_buf: resb 20

section .text
_main:
    mov ebp, esp
    sub esp, 8

    push -10
    call _GetStdHandle@4
    mov ebx, eax

    push 0
    lea ecx, [ebp - 4]
    push ecx
    push 20
    lea eax, [_input_buf]
    push eax
    push ebx
    call _ReadFile@20

    push -11
    call _GetStdHandle@4
    mov ebx, eax

    push 0
    lea ecx, [ebp - 8]
    push ecx
    mov edx, [ebp - 4]
    push edx
    lea eax, [_input_buf]
    push eax
    push ebx
    call _WriteFile@20

    push 0
    call _ExitProcess@4

Two things: 两件事情:

You're only allocating 4 bytes - making space for two characters - as you are reading input into the last allocated dword on the stack: 在将输入读入堆栈中最后分配的dword时,您仅分配4个字节(为两个字符腾出空间):

ebp-12 [undefined]
ebp-8: [input length]
ebp-4: [input buffer]
ebp:

You're giving the length of the input string as a pointer instead of dereferencing it, making it try to output a huge number of bytes, and failing: 您将输入字符串的长度作为指针而不是取消引用,使它尝试输出大量字节,并且失败:

lea ecx, [ebp - 8]
push ecx <- address, not value

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

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