2012-03-03 161 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