读书人

帮忙看看以下汇编嵌套C++时为什么会有

发布时间: 2012-03-08 13:30:13 作者: rapoo

帮忙看看以下汇编嵌套C++时为什么会有运行时错误。

我是想试验一下汇编嵌套C++对字符串加密的过程,但是有不足,希望帮忙看看~
#include <iostream>
#include <string>

using namespace std;

void Translate(string &temp, unsigned count)
{
__asm{
lea esi, temp // 这里似乎有问题
mov eax, 12
mov ecx, count
L1:
xor [esi], eax
inc esi
loop L1
}
cout < < "temp = " < <temp < <endl; //程序到这里结束了。
}

int main()
{

unsigned int count;
string str;
cout < < "Input a sentences to be done: " < <endl;
getline(cin,str, '\n ');
count = str.length();

Translate(str,count);
cout < < "str = " < <str < <endl;
Translate(str,count);
cout < < "str = " < <str < <endl;

system( "pause ");
return 0;
}

[解决办法]
#include <iostream>
#include <string>

using namespace std;

void Translate(string &temp, unsigned count)
{
const char *p = temp.c_str();
__asm{
//lea esi, temp // 这里似乎有问题
mov esi, p
mov eax, 12
mov ecx, count
L1:
xor [esi], eax
inc esi
loop L1
}
cout < < "temp = " < <temp < <endl; //程序到这里结束了。
}

int main()
{

unsigned int count;
string str;
cout < < "Input a sentences to be done: " < <endl;
getline(cin,str, '\n ');
count = str.length();

Translate(str,count);
cout < < "str = " < <str < <endl;

Translate(str,count);
cout < < "str = " < <str < <endl;

system( "pause ");
return 0;
}

读书人网 >C++

热点推荐