String 元音计数出现故障
我有一个x86汇编函数,它应该以参数的形式计算字符串中元音的数量 此函数的工作方式是:String 元音计数出现故障,string,assembly,x86,String,Assembly,X86,我有一个x86汇编函数,它应该以参数的形式计算字符串中元音的数量 此函数的工作方式是: 它通过将每个元素与0进行比较来迭代字符串。如果当前元素为0,则停止。在每次迭代中,is\u将调用元音,并且根据此函数返回的值,ebx将递增 我已经测试了is_元音和lower好几次,它们似乎都正常工作。但是当我在这个输入上调用count\u元音时,它返回1。这种价值观显然是不对的,是一种垃圾 我做错了什么 #include "stdafx.h" int lower(int) { __asm
它通过将每个元素与0进行比较来迭代字符串。如果当前元素为0,则停止。在每次迭代中,
is\u将调用元音
,并且根据此函数返回的值,ebx
将递增
我已经测试了is_元音
和lower
好几次,它们似乎都正常工作。但是当我在这个输入上调用count\u元音时,它返回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字节寄存器,所以这一行mov eax[ecx]
应该替换为movzx eax,byte ptr[ecx]
byte ptr
因为字符使用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;
}
mov-eax,[ecx]
旁白:在mov-ebx中,65;cmp eax,ebx
您不需要先将立即数加载到另一个寄存器中。只使用cmpeax,65
更简单,尽管如前所述,它应该导致cmpal,65