Palindrome检查与阵列

时间:2016-12-01 03:17:44

标签: c++ arrays assembly masm masm32

我一直坚持如何使用MASM检查回文。

#include <iostream>
#include <cstring>
#include<string>
#include <algorithm>
using namespace std;

extern "C"
char test(char*, int);


int main()
{
char arr[] = {NULL};


cout << "Enter a string: " << endl;
cin >> arr;



int name = strlen(arr);
test(arr, name);
if (name == 1)
{
    cout << "It is a palindrome! " << endl;

}
else
    cout << "Not a palindrome. " << endl;

return 0;
  }

我向用户询问了一个字符串并将其插入到数组中。我将它发送到汇编文件,它会返回一个&#39; 1&#39;如果它是真的或&#39; 0&#39;如果是假的

.686
.model flat

.code


_test PROC ;named _test because C automatically prepends an underscode, it is needed to interoperate
push ebp
mov ebp,esp ;stack pointer to ebp

mov eax,[ebp+8]
mov ecx,[ebp+12]
mov ebp,0
mov edi,0
mov edx,0



loopMe: 
cmp ebp,ecx
je True

mov al,[eax+edi]
mov bl,[edx+esi]
cmp al,bl   ;compare 
jne false   ;if not equal then jump to false
inc edi     
dec esi
jmp loopMe

True:
mov eax,1
jmp allDone

False:
mov eax,0
jmp allDone


allDone:    
pop ebp
ret
_test ENDP

END 

当我输入一个字符串时,它似乎总是返回0.我检查了调试器,即使值相等,也总会跳转到False标签。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

你在C ++中使用字符串。

替换

char arr[] = {NULL}; // by the way NULL here has no sence.

通过

std::string arr;

并使用字符串的方法做你想要的:

答案 1 :(得分:-1)

所以我最终改变了一些代码并让它运行起来。

int main()
{
char arr[32] = {NULL};


cout << "Enter a string: " << endl;
cin >> arr;



int name = strlen(arr);
int palindrome= test(arr, name);

if (palindrome )
{
    cout << "It is a palindrome! " << endl;

}
else
    cout << "Not a palindrome. " << endl;

return 0;
}

然后是asm文件

.686
.model flat

.code


_test 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]
mov ecx,[ebp+12]
mov edx,ebx
add edx,ecx
dec edx




loopMe: 
cmp ebx,edx
jge True

mov ch,[ebx]
mov cl,[edx]
cmp ch,cl   ;compare 
jne false   ;if not equal then jump to false
inc ebx     
dec edx
jmp loopMe

True:
mov eax,1
jmp allDone

False:
mov eax,0
jmp allDone


allDone:    
pop ebp
ret
_test ENDP

 END 

我现在唯一的问题是,如果我输入妈妈而不是妈妈,它会说它不是回文。我只需要弄清楚如何忽略装配中的情况。