I'm trying to create a program which iterates through a string and checks to see if it is a palindrome.
The word I am currently testing is racecar, but when I run the following code, it automatically decides that the first and last values aren't the same.
Is this just a case of entering the wrong registers into the loop, or is there something more than that?
main proc
mov edi, SIZEOF myWord- TYPE myWord ;length of string - type of data
mov ecx, LENGTHOF myWord/ 2 ;loop (N / 2) times
mov esi,OFFSET myWord ;beginning of string
checkWord:
mov al,[esi]
cmp [esi+edi],al
jne wordIsNotAPalindrome
add esi,TYPE myWord
sub edi,TYPE myWord
loop checkWord
mov edx, offset wordIsAPalindrome
call WriteString
exit
main endp
Declaration for myWord:
myWord BYTE "racecar", 0
Edit:
I just made a few adjustments, and this is what I have now:
main proc
mov edi, SIZEOF myWord- TYPE myWord
mov ecx, LENGTHOF myWord/ 2
mov esi,OFFSET myWord
checkWord:
mov al, SIZEOF myWord
mov al,[esi]
cmp [myWord+edi],al
jne wordIsNotAPalindrome
add esi,TYPE myWord
sub edi,TYPE myWord
loop checkWord
mov esi, offset wordIsAPalindrome
call WriteString
call Crlf
call Crlf
exit
main endp
It loops now correctly, and when I do Step Over, it proceeds to make it until wordIsAPalindrome. The problem is, this is the output:
Θµ#
Press any key to continue . . .
If you are using Irvine's
WriteString
, it takes the offset of the string fromEDX
register, notESI
, so :