2013-04-27 66 views
0

在NASM我有strconst_0 dw 5, 0, __utf16__('hello')(單反引號)作爲字符串從字符串文字中檢索單詞?

我試圖訪問這樣的「h」(其中,[EBP + 8]是0並且[EBP + 4]是的地址字符串)

mov eax, [ebp+8] ; get the index 
mov ebx, [ebp+4] ; get the address 
movzx eax, word [ebx + 2 * eax + 4] 
push eax 
call print_char ; call the print function 

然而,當我運行此代碼,只是一個空字符打印

編輯:全部房源

main: 
pop edx 
push ebp 
mov ebp, esp 
sub esp, 4 
mov [ebp-4], edx 
mov eax, strconst_0 
push eax 
push dword 0 
call getChar 
add esp, 8 
push eax 
call printChar 
add esp, 4 
mov edx, [ebp-4] 
add esp, 4 
pop ebp 
push edx 
ret 

getChar: 
pop edx 
push ebp 
mov ebp, esp 
sub esp, 4 
mov [ebp-4], edx 
mov eax, [ebp+8] 
mov ebx, [ebp+4] 
movzx eax, word [ebx + 2 * eax + 4] 
push eax 
pop eax 
mov edx, [ebp-4] 
add esp, 4 
pop ebp 
push edx 
ret 
+0

OT,但是,爲什麼這麼多的代碼來計算地址?尤其是那個雙倍寬度的mul ..'movzx eax,word [ebx + 2 * eax + 4]' – harold 2013-04-27 13:32:13

+0

不知道你可以用地址來做 – CallumDev 2013-04-27 13:36:27

+0

不錯,對不對?但是這並不能解決這個問題,我不知道什麼是錯的 – harold 2013-04-27 13:41:33

回答

1

我以錯誤的方式得到了參數。如果我在初始化mov指令中交換ebx和eax,它可以正常工作。

0
  1. 您在程序開始時將main的返回地址彈出到edx中,這沒有任何意義。
  2. 你根本沒有評論,所以我不可能理解你想要做什麼,因爲你沒有getChar的原型。

基本上,這裏的分析,我不會對我的工具,所以我做了幾個假設

main: 
    ; why is return address of main popped into edx? 
    pop  edx 

    ; standard stack setup     
    push  ebp 
    mov  ebp,  esp 

    ; make space for 4 bytes 
    sub  esp,  4 

    ; store edx in where return address used to be... why? 
    mov  [ebp-4], edx 

    ; move address of strconst_0 to eax 
    mov  eax,  strconst_0 

    ; push eax on stack 
    push  eax 

    ; push 4 bytes 0x00000000 on stack 
    push dword 0 

    ; call getchar(0, strconst_0) 
    call  getChar 

    ; restore stack 
    add  esp,  8 

    ; push eax which was mutated in getChar on stack 
    push  eax 

    ; printChar(eax) 
    call  printChar 

    ; restore stack 
    add  esp,  4 

    ; move whatever we overwritten old return address to, to edx 
    mov  edx,  [ebp-4] 

    ; restore stack 
    add  esp,  4 
    pop  ebp 

    ; restore return address 
    push  edx 

    ; return 
    ret 

; what do I accept(on stack) 
; what do I return(in eax) 
getChar: 
    ; again, seems silly 
    pop edx 

    ; obvious 
    push ebp 
    mov  ebp,  esp 

    ; make space for 4 bytes on stack 
    sub  esp,  4 

    ; overwrite return address with edx 
    mov  [ebp-4], edx 

    ; I am guessing you are trying to get the two arguments into eax, ebx 

    ; eax = 0 
    mov  eax,  [ebp+8] 
    ; ebx = strconst_0 
    mov  ebx,  [ebp+4] 
    ; magic is here 
    movzx  eax,  word [ebx + 2 * eax + 4] 
    ; push magic number 
    push  eax 
    ; pop ... something into eax 
    pop  eax 
    ; restore edx from whatever you put there... 
    mov  edx,  [ebp-4] 
    ; restore stack? 
    add  esp,  4 

    ; obvious 
    pop  ebp 
    push  edx 
    ret 

老實說,我不知道你在做什麼,如果你說的東西,這將有助於沿着 「我試圖做一個函數接受?和字符串緩衝區的地址,哪個?」

特別是, 1.我不知道getChar輸入是什麼,是字符的位置?這是別的嗎? 2.我不知道getChar的輸出是什麼,它存儲在eax中的東西?它發射導彈嗎? 3.我不知道爲什麼你使用你的函數的返回地址臨時存儲,這是愚蠢的。

main: 
    push   ebp 
    mov   ebp,    esp 

    ; getChar(0, str) 
    push   str 
    mov dword  eax,    0 
    push   eax 
    call   getChar 
    add   esp,    8 

    ; printChar(str) 
    push   str 
    call   printChar 
    add   esp,    4 

    mov   esp,    ebp 
    pop   ebp 
    ret 

; char getChar(long ix, char *str) 
; returns: char at index indicated by ix in eax register 
getChar: 
    push   ebp 
    mov   ebp,    esp 
    push   ebx ; save ebx to avoid unwanted side-effects 

    mov   eax,    [ebp + 8] ; ix 
    mov   ebx,    [ebp + 12] ; str 
    add   eax,    ebx ; eax = &(str[ix]) 
    mov   eax,    (eax) ; eax = *eax = str[ix] 

    pop   ebx ; restore ebx 
    mov   esp,    ebp 
    pop   ebp 
    rts 

str db 'this is a string.', 0 

這是絕對行不通的,併爲示範的目的純粹是給定的,因爲我沒有在一段時間寫的x86,這個想法應該是明確的,但。希望能幫助到你。

+0

getChar接受一個指向字符串的32位指針和一個字符索引。這些字符每個都是2個字節(UTF-16),並且必須以包含大小的雙字前綴。 getChar然後在返回之前將該字符放在通過eax傳遞的索引處。 – CallumDev 2013-04-27 15:10:20

+0

除了你需要乘以(或移位)索引值以使它乘以2(字符串地址+2 *位置)之外,其他想法都是相同的,這就是你的字符位置。然後你將16個字節(所以一個字,而不是一個字節)從這個指針移動到你的eax作爲返回值。 – Dmitry 2013-04-27 15:15:34