内联程序集 - 检查单词是否为回文

inline assembly - check if word is a palindrome

本文关键字:是否 回文 单词 检查 程序集      更新时间:2023-10-16

我正在编写一个简单的程序,它使用内联来检查给定的单词是否是回文。问题是它没有返回正确的答案。在调试过程中,我发现esi寄存器有问题(al中的值是正确的('a'),但在bl中它不是(0)。我不确定我做错了什么。

#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;
} 

您将sizeofstring设置为 8,但字符串"arabara"的长度为 7 个字符。