2012-03-03 165 views
0

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

global _main 

extern [email protected] 
extern [email protected] 
extern [email protected] 
extern [email protected] 

section .text 

_main: 
    mov ebp, esp 
    sub esp, 12 

    push -11 
    call [email protected] 
    mov ebx, eax 

    push 0 
    push dword [ebp - 12] 
    lea ecx, [_msg_end - _msg] 
    push ecx 
    lea edx, [_msg] 
    push edx 
    push ebx 
    call [email protected] 

    push -10 
    call [email protected] 
    mov ebx, eax 

    push 0 
    lea ecx, [ebp - 8] 
    push ecx 
    push 20 
    lea edx, [ebp - 4] 
    push edx 
    push ebx 
    call [email protected] 

    push -11 
    call [email protected] 
    mov ebx, eax 

    push 0 
    push dword [ebp - 12] 
    lea ecx, [ebp - 8] 
    push ecx 
    lea edx, [ebp - 4] 
    push edx 
    push ebx 
    call [email protected] 

    push -10 
    call [email protected] 
    mov ebx, eax 

    push 0 
    lea ecx, [ebp - 8] 
    push ecx 
    push 1 
    lea edx, [ebp - 4] 
    push edx 
    push ebx 
    call [email protected] 

    push 0 
    call [email protected] 
_msg: 
    db "Hello, world!", 10 
_msg_end: 

编辑 - 这里的工作代码:

global _main 

extern [email protected] 
extern [email protected] 
extern [email protected] 
extern [email protected] 

section .bss 
_input_buf: resb 20 

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

    push -10 
    call [email protected] 
    mov ebx, eax 

    push 0 
    lea ecx, [ebp - 4] 
    push ecx 
    push 20 
    lea eax, [_input_buf] 
    push eax 
    push ebx 
    call [email protected] 

    push -11 
    call [email protected] 
    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 [email protected] 

    push 0 
    call [email protected] 
+0

怎么能工作的?您不保留缓冲区的任何空间。 – 2012-03-03 11:06:36

+0

是的,我是...比方说,在阅读最多20个字符的字符串后,我将8推入堆栈而不是ecx,然后运行该程序并键入“Benjamin”。然后它会输出“Benjamin”。 – Benjamin 2012-03-03 11:13:21

回答

1

两件事情:

你只能分配4个字节 - 使空间两个字符 - 如您正在将输入读入堆栈中最后分配的双字:

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

你给输入字符串的长度为一个指针,而不是解引用它,使它尽量输出字节的数量庞大,且未能:

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