我被困在如何使用MASM检查回文。Palindrome检查数组
#include <iostream>
#include <cstring>
#include<string>
#include <algorithm>
using namespace std;
extern "C"
char test(char*, int);
int main()
{
char arr[] = {NULL};
cout << "Enter a string: " << endl;
cin >> arr;
int name = strlen(arr);
test(arr, name);
if (name == 1)
{
cout << "It is a palindrome! " << endl;
}
else
cout << "Not a palindrome. " << endl;
return 0;
}
我问用户一个字符串,并将其插入到一个数组。我将它发送到程序集文件,如果它为真,它将返回'1'或者如果为假,则返回'0'。
.686
.model flat
.code
_test PROC ;named _test because C automatically prepends an underscode, it is needed to interoperate
push ebp
mov ebp,esp ;stack pointer to ebp
mov eax,[ebp+8]
mov ecx,[ebp+12]
mov ebp,0
mov edi,0
mov edx,0
loopMe:
cmp ebp,ecx
je True
mov al,[eax+edi]
mov bl,[edx+esi]
cmp al,bl ;compare
jne false ;if not equal then jump to false
inc edi
dec esi
jmp loopMe
True:
mov eax,1
jmp allDone
False:
mov eax,0
jmp allDone
allDone:
pop ebp
ret
_test ENDP
END
当我进入它似乎总是返回0。我检查了调试器,它会一直跳到即使值相等的假标签的字符串
。任何帮助表示赞赏。
_'cin >> arr;'_是未定义的行为。 –
为什么不用C++完全编写代码,然后使用类似[this](https://gcc.godbolt.org/)的内容来确定汇编代码的外观?你知道生成的程序集是正确的,因为C++程序是正确的。然后如有必要,调整生成的汇编代码。 – PaulMcKenzie