1

文字列の長さを数えるのに問題があります。私はいつも2432のような数字を受け取ります、あなたは「abc」のような文字列を渡します。

問題はこの行にあると思います

mov bl, byte [esi]

でも理由はわかりません。多分それはビット単位の文字長を持つものですか?

問題は64ビットオペレーティングシステムまたはデュアルコアプロセッサでしょうか?(最初の行の「ビット32」で問題が解決すると思うので、どういうわけか疑わしいです)。

PS:これは練習です。そのため、このように弦の長さを決定する必要があります。

コード:

bits 32
extern _printf
extern _scanf
global _main

section .data
number_frmt db 10,"%d",10,0
enter_str db "Enter some string: ", 10,0
string_frmt db "%s", 0

section .bss
entered_string resb 100

section .text

_main:
    pushad

    push dword enter_str
    call _printf
    add esp, 4

    push dword entered_string
    push dword string_frmt
    call _scanf
    add esp, 4  ;leave the entered string in the stack

    call count  ; count it and put the result to eax

    push dword eax
    push dword number_frmt
    call _printf
    add esp, 12

    popad
    ret

count:
    push esi    ;save it
    push ebx    ;save it
    mov eax, 0  ;init eax=0
    mov esi, [esp+12] ;put the entered string to esi

.loop:
    mov bl, byte [esi]  ;get the first char
    inc eax             ;eax++
    add esi,1           ;point to next char
    cmp bl,10           ;is it new line?
    jne .loop           ;if not loop
    dec eax             ;eax-- (because of newline at the end)
    pop ebx             ;retrieve ebx
    pop esi             ;retrieve esi
    ret
4

2 に答える 2

5
cmp bl,10           ;is it new line?

する必要があります

cmp bl,0

c / c ++文字列は毎回0で終了/終了するため、実際には、次の10が存在するメモリ内のランダムな位置を検索しました。

于 2010-07-17T16:25:35.697 に答える
1

scanfを使用することは、問題を混乱させるため、おそらく良いことではありません。getsで切り替えると、新しい行も有効な文字であり、カウントの一部としてカウントする必要があります。文字列はNULで終了します(自動的に)

count:
    push esi    ;save it
    push ebx    ;save it
    xor eax, eax; initialize it to zero
    mov esi, [esp+12] ;put the entered string to esi

.loop:
    mov bl, byte [esi]  ;get the first char

    cmp bl, bl          ;set the flags
    jz  .out            ;nul character

    inc eax
    jmp .loop

    pop ebx             ;retrieve ebx
    pop esi             ;retrieve esi

    ret
于 2010-07-17T19:04:06.650 に答える