簡體   English   中英

在程序集x86 IRVINE32中查找字符串長度

[英]Finding the String Length in Assembly x86 IRVINE32

我是匯編編程的新手,我試圖找出該程序的最后一個難題。 當我顯示一個字符串(“ Hello”)時,它顯示為0而不是5。為什么會發生這種情況?我需要怎么做才能更改它?

TITLE 


INCLUDE Irvine32.inc

.data

prompt       BYTE   "Enter String: ", 0
response     BYTE   50 DUP(0)
message      BYTE   " Message entered. ",0

.code   

STRQRY       PROC
             push   ebp
             mov    ebp, esp
             push   edx
             push   ecx

             mov    edx, [ebp+8]
             call   writestring

             mov    ecx, SIZEOF response
             mov    edx, OFFSET response
             call   readstring



             pop    ecx
             pop    edx
             pop    ebp  
             ret    4

STRQRY       ENDP

STRLEN       PROC 
             push   ebp 
             mov    ebp, esp
             push   ebx
             push   ecx

             mov    edx,[ebp+8]
             call   writedec

             mov    eax, 0


counter:
             mov    cl,[edx+eax]

             cmp    cl, 0      

             JE     done

             inc    eax 

             jmp    counter

done:
             pop    ecx
             pop    ebx
             pop    ebp
             ret    4

STRLEN       ENDP

main         PROC

             push   OFFSET prompt
             call   STRQRY



             push   eax 
             call   STRLEN

             mov    edx,OFFSET message
             call   WriteString

             mov    edx, OFFSET response
             call   WriteString

             exit
main         ENDP
END          main

您說要查找輸入字符串的長度,但這恰恰是IRVINE32 readstring函數在EAX寄存器中返回的內容!


 mov ecx, SIZEOF response mov edx, OFFSET response call readstring 

在此塊中,ECX應該比您提供的輸入緩沖區的大小小1。 這允許添加NULL終止符。 您將響應定義為50個字節,因此mov ecx, 49就可以了。


 call STRQRY ***HERE EAX HAS THE LENGTH OF THE INPUT!*** push eax call STRLEN 

STRLEN過程運行時,它期望將地址作為參數,但是要為其提供一個長度 那就是崩潰的材料。

 mov    edx, [bp+8]   <<< This needs to be the ADDRESS to start at!
 mov    eax, 0
counter:
 mov    cl,[edx+eax]
 ...

  mov edx,[ebp+8] call writedec mov eax, 0 counter: mov cl,[edx+eax] cmp cl, 0 JE done inc eax jmp counter done: 

在此代碼中,您在錯誤的地方使用writedec EAX目前沒有計算得出的長度。 call writedec移至完成標簽下方。


STRLEN過程中, push EBX寄存器push / pop ,以保存/恢復EDX寄存器。


總而言之,這就是STRLEN過程的樣子:

STRLEN       PROC 
    push   ebp 
    mov    ebp, esp
    push   edx
    push   ecx
    mov    edx, [bp+8]
    mov    eax, 0
counter:
    mov    cl, [edx+eax]
    cmp    cl, 0
    JE     done
    inc    eax
    jmp    counter
done:
    call   writedec         <<< Length calculated by you
    pop    ecx
    pop    edx
    pop    ebp
    ret    4
STRLEN       ENDP

以及如何稱呼它:

    push   OFFSET prompt
    call   STRQRY
    call   writedec         <<< Length given by ReadString
    push   OFFSET response
    call   STRLEN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM