I’m learning x86 masm and stuck in a project. The code should be fairly simple but I have no idea what I did wrong, it supposed to check for Palindrome from an array of character, but the code always return eax = 0 no matter what. I tried different approach, but it doesn’t make any difference.
This is the test.cpp
#include <iostream>
using namespace std;
extern"C" {
int testPalindrome(char[]);
}
int main()
{
char testString1[] = "birThday";
char testString2[] = "raceCar";
//for checking result, will do a better presentation
cout << testString1 << ' ' << testPalindrome(testString1) << endl;
cout << testString2 << ' ' << testPalindrome(testString2) << endl;
return 0;
}
and this is the .asm
.686
.model flat
.code
_testPalindrome PROC
push ebp
mov ebp,esp ; stack pointer to ebp
mov ebx,[ebp+8] ; address of first array element
xor ecx,ecx
xor dl,dl
mov al,223
arrayEnd:
cmp [ebx+ecx],dl
je continue
inc ecx
jmp arrayEnd
continue:
mov edx,0
mov edi,ecx
dec edi ;to not include the at the end
shr ecx,1 ;to traverse only to middle of string
palindromeCheck:
cmp ecx, 0
je yesPalindrome
;compare, convert to uppercase but don't change the original string
mov cl,[ebx+edx]
and cl,al
mov dl,[ebx+edi]
and dl,al
cmp cl,[ebx+edi]
jne notPalindrome
sub ecx, 1
add edx, 1
sub edi, 1
jmp palindromeCheck
yesPalindrome:
mov eax,1
jmp allDone
notPalindrome:
mov eax,0
allDone:
pop ebp
ret
_testPalindrome ENDP
END
New contributor
user24909579 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.