inline assembly - check if word is a palindrome

821 views Asked by At

I am writing a simple program that uses inline assebly to check if given word is a palindrome. The problem is it doesn't return correct answers. During debugging I found out that there's something wrong with esi register (the value in al is correct ('a'), but in bl it's not (0). I'm not sure what I'm doing wrong.

#include "stdafx.h"
#include <iostream>
#include <string> 
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    char s[] = "arabara";
    int sizeofstring = 8; // size of s[]
    int x = 0;
    int y = 1; //flag when is_palindrome
    __asm
    {
        lea edi, s 
        mov esi, edi 
        add esi, sizeofstring
        dec esi  //point to the last char 

        mov ecx, sizeofstring
        cmp ecx, 1
        je is_palindrome  //single char is always a palindrome 

        shr ecx, 1   //divide by 2 

nextchar: 
        mov al, [edi]
        mov bl, [esi]
        cmp al, bl
        jne stop
        inc edi
        dec esi
        loop nextchar

    is_palindrome:
    mov eax, y
    mov x, eax //change flag to 1 

stop: 
    }

    cout << x  << endl; //shoud print 1 when palindrome
    system("pause");
    return 0;
} 
1

There are 1 answers

1
Timothy Baldwin On

You set sizeofstring to 8, but your string "arabara" is seven characters long.