Я проверяю, является ли введенная строка палиндромом, беря строку, перемещая ее в массив символов и сравнивая первый и последний элементы массива символов друг с другом для проверки. Я могу заставить первый элемент массива легко найти второй символ, но чтобы найти последнее приемлемое значение и уменьшить его, он не находит следующий символ в массиве. Итак, если исправленный/очищенный массив символов выглядит так:
['A']['B']['C']['D']['A']
ebx изменится с «A» -> «B», но edi не изменится с «A» -> «D»
Почему ebx меняет символы, а edi вычитает только 1 из значения регистра? Что я могу сделать, чтобы изменить значение символа edi? Спасибо!
Код С++: (на всякий случай)
#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;
}
масм код:
.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
mov eax, 1
для возвращаемого значения, но затем всегда очищаетсяeax
перед возвратом вallDone
. Кроме того, не пишитеjcc label / label:
. Просто позвольте выполнению провалиться, так как оно будет независимо от того, взята ветвь или нет. Обычно после сравнения вам нужен только одинjcc
, если только у вас нет одногоjg
и одногоjge
или чего-то подобного. Но неjg / jle
. Либо провалитесь без прыжка, либо используйте безусловнуюjmp
для второго, если вам нужно прыгнуть куда-то еще. Как только вы разобрались со своим дизайном, вы можете попробовать. переставить, чтобы сэкономить на ветках. - person Peter Cordes   schedule 03.12.2015