【发布时间】:2016-03-07 10:59:51
【问题描述】:
我正在测试输入的字符串是否是回文,方法是获取字符串,将其移动到字符数组中,然后将 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代码:
.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
【问题讨论】:
-
你用调试器单步执行了吗? Microsoft 在 Visual Studio 中有一个不错的调试器,但 OllyDbg 也是一个非常好的调试器。
-
你有一些错误,比如
mov eax, 1的返回值,但在返回allDone之前总是清除eax。另外,不要写jcc label / label:。只是让执行失败,因为无论是否采用分支都会如此。通常在比较之后你只需要一个jcc,除非你有一个jg和一个jge什么的。但不是jg / jle。如果您需要跳到其他地方,要么不跳就跌倒,或者使用无条件的jmp作为第二个。一旦你弄清楚了你的设计,你就可以探测了。重新安排以保存在树枝上。 -
我发现我正在将一个寄存器的值移动到同一个寄存器的下部。 =p
标签: arrays assembly masm 32-bit palindrome