【问题标题】:Unable to print back entered text in x86 assembly无法在 x86 程序集中打印回输入的文本
【发布时间】:2012-03-03 10:31:32
【问题描述】:

我这里有一个汇编程序,它应该打印一个字符串,允许用户输入一些文本,再次打印完全相同的文本,然后等待按键终止程序,只使用 Win32 本机函数。
问题是除了打印用户输入的字符串外,一切似乎都正常。它只是打印一个空白的新行。 代码如下:

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:

编辑 - 这是工作代码:

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

【问题讨论】:

  • 它是如何工作的?您没有为缓冲区保留任何空间。
  • 是的,我是......假设我在读取最多 20 个字符长的字符串后将 8 压入堆栈而不是 ecx,然后我运行程序并输入“Benjamin”。然后它会输出“Benjamin”。

标签: windows assembly x86 nasm


【解决方案1】:

两件事:

您只分配 4 个字节 - 为两个字符腾出空间 - 因为您正在将输入读取到堆栈上最后分配的 dword:

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

您将输入字符串的长度作为指针而不是取消引用它,使其尝试输出大量字节并失败:

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

【讨论】:

    猜你喜欢
    • 2021-12-07
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2011-02-12
    • 2014-07-28
    • 1970-01-01
    相关资源
    最近更新 更多