2014-03-14 76 views
0

我在程序集中做了strcpy函數,然後我嘗試啓動幾個程序,如Firefox,emacs,... 但我有一個複製的問題,我不知道在哪裏問題是。關於strcpy啓動Firefox的NASM問題

彙編代碼:

global strcpy 

section .text 

strcpy: 
    push rbp 
    push rdx 
    mov rbp, rsp 
    mov rdx, 0 
strcpy_loop: 
    cmp byte [rsi + rdx], 0 
    je strcpy_end 
    mov al, byte [rsi + rdx] 
    mov byte [rdi + rdx], al 
    inc rdx 
    jmp strcpy_loop 
strcpy_end: 
    mov rax, rdi 
    mov rsp, rbp 
    pop rdx 
    pop rbp 
    ret 

結果當我試圖用我的共享庫推出了一個小程序:

s1 = hello 
s2 before copy = 
s2 after copy = hello 

結果當我試圖用我的共享庫啓動Firefox:

/usr/bin/firefox: line 52: $'basenamex\326g\002Lame $0`': command not found 
/usr/bin/firefox: line 57: bad substitution: no closing "`" in `x�gL 
/usr/bin/firefox: line 63: $'[\340\n\002': command not found 
/usr/bin/firefox: line 73: fileL: command not found 
/usr/bin/firefox: line 75: echoL: command not found 
grep: invalid option -- 'g' 
Usage: grep [OPTION]... PATTERN [FILE]... 
Try 'grep --help' for more information. 
/usr/bin/firefox: line 78: $'[\351\n\002L': command not found 
/usr/bin/firefox: line 83: export: `MOZILLA_FIVE_HOME�gL': not a valid identifier 
/usr/bin/firefox: line 91: $'[\257\n\002': command not found 
/usr/bin/firefox: line 95: /dev/null�gL: Permission denied 
/usr/bin/firefox: line 97: $'[\t\v\002': command not found 
/usr/bin/firefox: line 108: $'[\006\v\002': command not found 
/usr/bin/firefox: line 131: $'[\023\v\002': command not found 
/usr/bin/firefox: line 138: exec: =0 
           : not found 

小程序正在運行,但不是firefox。

+2

從'strcpy'文檔:_「的的strcpy()函數將字符串由src指出,**包括終止空字節**( '\ 0'),至dest指向的緩衝區。「_ – Michael

+0

謝謝你的問題。 問題已解決 –

回答

0

你實際上並沒有在字符串的末尾複製字符串終結符\0,這意味着目標字符串沒有一個。換句話說,如果你這樣做:

char buff[] = "Hello there"; 
strcpy (buff, "Bye");   // your strcpy, not a 'real' one. 

,那麼你就結了:

Byelo there 

這是不是你想要的。爲了解決這個問題,你可以像做:

strcpy_end: 
    xor al, al     ; force al to `\0`. 
    mov byte [rdi + rdx], al ; and store it. 

    mov rax, rdi    ; rest of original 
    mov rsp, rbp    ; code here ... 
    pop rdx 
    pop rbp 
    ret 

或者,你可以離開整理代碼不變並最終轉移集成到你的主循環,使其成爲一個do-X-while-condition相當比while-condition-do-X

strcpy_loop: 
    mov al, byte [rsi + rdx] 
    mov byte [rdi + rdx], al 
    inc rdx 
    cmp al, 0 
    jne strcpy_loop 

    mov rax, rdi    ; rest of original 
    mov rsp, rbp    ; code here ... 
    pop rdx 
    pop rbp 
    ret