我正在测试输入的字符串是否是回文,方法是取字符串,将其移动到字符数组中,并将char数组的第一个和最后一个元素相互比较以进行测试。我可以让数组的第一个元素轻松找到第二个字符,但要找到最后一个可接受的值并减少它,它就找不到数组中的下一个字符。因此,如果更正/清理的char数组如下所示:
[ 'A'] [ 'B'] [ 'C'] [ 'd'] [ 'A']
ebx将来自'A' - > 'B'但edi不会改变'A' - > 'd'
为什么ebx会改变字符,但edi只从它的寄存器值中减去1?如果让edi更改字符值,我该怎么办?谢谢!
C ++代码:(以防万一)
#include <iostream>
#include <cstring>
#include <sstream>
using namespace std;
extern"C"
{
char stringClean(char *, int);
char isPalindrome(char *, int);
}
int main()
{
int pal = 0;
const int SIZE = 30;
string candidate = "";
char strArray[SIZE] = { '\0' };
cout << "enter a string to be tested: ";
getline(cin, candidate);
int j = 0;
for (int i = 0; i < candidate.length(); i++) //getting rid of garbage before entering into array
{
if (candidate[i] <= 'Z' && candidate[i] >= 'A' || candidate[i] <= 'z' && candidate[i] >= 'a')
{
strArray[j] = candidate[i];
j++;
}
}
if (int pleaseWork = stringClean(strArray, SIZE) == 0)
pal = isPalindrome(strArray, SIZE);
if (pal == 1)
cout << "Your string is a palindrome!" << endl;
else
cout << "Your string is NOT a palindrome!" << endl;
system("pause");
return 0;
}
masm code:
.686
.model flat
.code
_isPalindrome PROC ; named _test because C automatically prepends an underscode, it is needed to interoperate
push ebp
mov ebp,esp ; stack pointer to ebp
mov ebx,[ebp+8] ; address of first array element
mov ecx,[ebp+12] ; number of elements in array
mov ebp,0
mov edx,0
mov eax,0
push edi ;save this
push ebx ;save this
mov edi, ebx ;make a copy of first element in array
add edi, 29 ;move SIZE-1 (30 - 1 = 29) elements down to, HOPEFULLY, the last element in array
mov bl, [ebx]
mov dl, [edi]
cmp dl, 0 ;checks if last element is null
je nextElement ;if null, find next
jne Comparison ;else, start comparing at Comparison:
nextElement:
dec edi ;finds next element
mov dl, [edi] ;move next element into lower edx
cmp dl, 0 ;checks if new element is mull
je nextElement ;if null, find next
jne Comparison ;else, start comparing at Comparison:
Comparison:
cmp bl,dl ;compares first element and last REAL element
je testNext ;jump to textNext: for further testing
mov eax,1 ;returns 1 (false) because the test failed
jne allDone ;jump to allDoneNo because it's not a palindrome
testNext:
dec edi ;finds last good element -1 --------THIS ISN'T DOING the right thing
inc ebx ;finds second element
cmp ebx, edi ;checks if elements are equal because that has tested all elements
je allDone
;mov bl,[ebx] ;move incremented ebx into bl
;mov dl,[edi] ;move decremented edi into dl
jmp Comparison ;compare newly acquired elements
allDone:
xor eax, eax
mov ebp, eax
pop edi
pop edx
pop ebp
ret
_isPalindrome ENDP
END
答案 0 :(得分:0)
我没有测试过你的代码,但看着它我注意到了一些可能的问题。
为什么ebx会改变字符
看起来就是这样,但这不是你试图达到的目的。在初始阶段之后,您注释掉了从内存/数组中读取字符的行(见下文)。所以事实上,你确实改变了 EBX中的角色,但不是你期望的(并且据称是想要的)。使用INC EBX
,您将char值从“A”(65dec)增加到“B”(66dec)。 'B'也是字符串的第二个字符只是巧合。尝试将字符串从ABCDA更改为ARRCD或其他东西,你仍然会在第二轮获得'B'。所以EBX确实发生了变化。
...
;mov bl,[ebx] ;move incremented ebx into bl
;mov dl,[edi] ;move decremented edi into dl
jmp Comparison ;compare newly acquired elements
...
但是edi只从它的寄存器值中减去1? 如果让edi更改字符值,我该怎么做?
是。这就是你的代码所做的,而且是正确的。取消注释上面包含[edi]
的行,EDI指向的char将加载到EDX = DL的低位字节。
您的代码的问题在于您将EBX用作指针和(char)值。将下一个char加载到EBX中会破坏指针,并且您的程序很可能在下一次迭代中使用ACCESS_VIOLATION崩溃或显示难以调试的随机行为。
与您使用EDI / EDX完成的值分开指针(EDI =指向char的指针,EDX(DL)= char值。
另一个问题是:您的代码仅适用于奇数长度的字符串。
testNext:
dec edi ; !!!
inc ebx ; !!!
cmp ebx, edi ; checks if elements are equal because that has tested all elements
je allDone
所以你要增加和减少(应该是)指针然后比较它们。现在考虑一个偶数长度字符串的情况:
ABBA
^ ^ (EBX(first) and EDI(second))
=> dec both =>
ABBA
^^ (EBX(first) and EDI(second))
=> dec both =>
ABBA
^^ (EDI(first) and EBX(second))
=> dec both =>
ABBA
^ ^ (EDI(first) and EBX(second))
=> dec both =>
ABBA
^ ^ (EDI(first) and EBX(second))
...
=&GT;问题!不会终止,条件EBX = EDI永远不会被满足* 可能的解决方案:在跳转
中添加A(在无符号值以上=大于)...
cmp ebx, edi
jae allDone