我有以下代碼。C++中的彙編程序
#include<iostream>
using namespace std;
int main() {
int result=0;
_asm {
mov eax,2
mov eax,result;
}
cout<<result<<endl;
return 0;
}
我知道彙編很好,但爲什麼不節目輸出2到屏幕?
我有以下代碼。C++中的彙編程序
#include<iostream>
using namespace std;
int main() {
int result=0;
_asm {
mov eax,2
mov eax,result;
}
cout<<result<<endl;
return 0;
}
我知道彙編很好,但爲什麼不節目輸出2到屏幕?
我不知道,但彙編你正在做的:
mov eax,2;
mov eax,result;
那不是移動2到eax,然後結果到eax, 你不想要
move eax, 2;
mov result, eax;
?
此行是錯誤的:
mov eax,result;
應該
mov result,eax;
#include<iostream>
using namespace std;
int main()
{
int result=0;
//the assembly code below do the same as: result = 2;
_asm
{
mov $2,%eax
mov %eax, result
}
cout << result << endl;
//main() returns 0 to tell the operating system that
//the program executed without problems.
return 0;
}
在用C語言編寫任何彙編代碼之前,用C語言中的一個小函數編寫代碼,並告訴編譯器打印彙編列表。這將告訴你編譯器如何生成彙編代碼並給你一個例子。通常程序集代碼顯示了參數和返回值是如何傳遞的。
根據我的經驗,彙編代碼最好用匯編語言編寫在單獨的模塊中。我很少在C中使用匯編代碼,我會讓編譯器爲我生成它。有時候,我變得執迷不悟,並將功能轉換爲針對特定處理器進行優化。例如,我重寫了memcpy
,優化爲使用ARM處理器的特殊功能(在研究了編譯器提供的低效版本之後)。
建議:
正確的語法是
MOV Destination, Source
你可以寫一個語法正確的問題嗎? – zvrba 2010-07-16 15:08:43
@zvrba:僅僅通過OP的名字來判斷,OP很可能不是以英語爲母語的人。當問題的含義明確時,我們不要對語法過於挑剔。 – 2010-07-16 15:15:48
我已經解決了交換eax和結果地方,一切正常 – 2010-07-16 15:17:08