计算元音故障

时间:2016-01-25 18:51:27

标签: string assembly x86

我有一个x86汇编函数,它应该计算一个参数形状的字符串中元音的数量。

此功能的工作方式是:
它通过将每个元素与0进行比较来遍历字符串。如果当前元素为0,则我们停止。在每次迭代时调用is_vowel并且取决于此函数返回的值ebx递增。

我已多次测试is_vowellower,但它们似乎正常运行。但是当我在这个输入上调用count_vowels时它会返回1.这些值显然不对,并且是某种垃圾。

我做错了什么?

    #include "stdafx.h"

int lower(int)
{
    __asm
    {
        mov eax, [ebp + 8]
        mov ebx, 65
        cmp eax, ebx
        jnge _endif1

        _if1:
            mov eax, [ebp + 8]
            mov ebx, 90
            cmp eax, ebx
            jnle _endif11

            _if11:
                mov ebx, [ebp + 8]
                sub ebx, 65
                mov eax, 97
                add eax, ebx
                jmp _endfunc
            _endif11:
        _endif1:
            mov eax, [ebp + 8]
        _endfunc:

    }
}

int is_vowel(int)
{
    __asm
    {
        push [ebp + 8]
        call lower
        add esp, 4

        mov ecx, eax
        mov ebx, 97
        cmp ecx, ebx

        jnz _endifa
        _ifa:
            mov eax, 1
            jmp _end_is_vowel
        _endifa:

        mov ebx, 101
        cmp ecx, ebx
        jnz _endife

        _ife:
            mov eax, 1
            jmp _end_is_vowel
        _endife:

        mov ebx, 105
        cmp ecx, ebx
        jnz _endifi

        _ifi:
            mov eax, 1
            jmp _end_is_vowel
        _endifi:

        mov ebx, 111
        cmp ecx, ebx
        jnz _endifo

        _ifo:
            mov eax, 1
            jmp _end_is_vowel
        _endifo:

        mov ebx, 117
        cmp ecx, ebx
        jnz _endifu

        _ifu:
            mov eax, 1
            jmp _end_is_vowel
        _endifu:

            mov eax, 0
        _end_is_vowel:
    }
}

void debug(int a)
{
    printf("%d\n");
}

int count_vowels(char* )
{
    __asm
    {
        mov edi, 0
        mov ebx, 0
        _for:
            mov ecx, [ebp + 8]
            add ecx, edi
            mov eax, [ecx]

            cmp eax, 0
            jz _endfor

            push eax
            call is_vowel
            add esp, 4

            cmp eax, 0
            jz _endif

            _if:
                push ebx
                call debug
                add esp, 4
                add ebx, 1
            _endif:

            inc edi
            jmp _for
        _endfor:
        mov eax, ebx
    }
}

int _tmain(int argc, _TCHAR* argv[])
{

    char arr[] = {'a', 'e', 'a', 'a', '\0'};

    printf("%d\n", count_vowels(arr));

    return 0;
}

1 个答案:

答案 0 :(得分:0)

由于我在我的函数中没有使用1字节寄存器,因此该行mov eax, [ecx]应该替换为movzx eax, byte ptr [ecx]byte ptr,因为char使用1个字节的存储空间和movzx,以便用零填充eax的其余部分。

#include "stdafx.h"

int lower(int)
{
    __asm
    {
        mov eax, [ebp + 8]
        mov ebx, 65
        cmp eax, ebx
        jnge _endif1

        _if1:
            mov eax, [ebp + 8]
            mov ebx, 90
            cmp eax, ebx
            jnle _endif11

            _if11:
                mov ebx, [ebp + 8]
                sub ebx, 65
                mov eax, 97
                add eax, ebx
                jmp _endfunc
            _endif11:
        _endif1:
            mov eax, [ebp + 8]
        _endfunc:

    }
}

char is_vowel(char)
{
    __asm
    {
        push [ebp + 8]
        call lower
        add esp, 4

        cmp eax, 97
        jnz _endifa

        _ifa:
            mov eax, 1
            jmp _end_is_vowel
        _endifa:

        cmp eax, 101
        jnz _endife

        _ife:
            mov eax, 1
            jmp _end_is_vowel
        _endife:

        cmp eax, 105
        jnz _endifi

        _ifi:
            mov eax, 1
            jmp _end_is_vowel
        _endifi:

        cmp eax, 111
        jnz _endifo

        _ifo:
            mov eax, 1
            jmp _end_is_vowel
        _endifo:

        cmp eax, 117
        jnz _endifu

        _ifu:
            mov eax, 1
            jmp _end_is_vowel
        _endifu:

        mov eax, 0
        _end_is_vowel:

    }
}

int count_vowels(char* )
{
    __asm
    {
        mov edx, 0
        mov ebx, 0
        _for:
            mov ecx, [ebp + 8]
            add ecx, edx
            movzx eax, byte ptr [ecx]

            cmp eax, 0
            jz _endfor

            push eax
            call is_vowel
            add esp, 4

            cmp eax, 0
            jz _endif

            _if:
                add ebx, 1
            _endif:

            inc edx
            jmp _for
        _endfor:
        mov eax, ebx
    }
}

int _tmain(int argc, _TCHAR* argv[])
{

    char arr[] = {'a', 'a', 'a', 'a', 'f', '\0'};

    printf("%d\n", count_vowels(arr));

    return 0;
}