#include <iostream>
using namespace std;
void Add()
{
int a, b, r;
cout << "please input two numbers" << endl;
cin>>a;
cin>>b;
cout << "input end" << endl;
int * c = & a;
__asm
{
//<将a, b 移入寄存器eax, ebx>
mov eax, c; // 将位址移入eax
mov eax, [eax]; // 将位址中的值移到eax
//mov r, eax; //将寄存器中的内容移到記憶體r中
mov ebx, b;//可以像這樣直接對ebx指派
lea eax, [eax + ebx];
mov r, eax;
//</将a, b 移入寄存器eax, ebx>
}
cout << a << " + "<< b << " = " << r << endl;
}
void AddOther()
{
int a, b, r;
cin>>a;
cin>>b;
__asm
{
//add a, b; //不正确的操作數類型
//add ->先移到寄存器中
mov eax, a;
mov ebx, b;
add eax, ebx;
mov r, eax;
}
cout << r << endl;
}
void Increase()
{
cout << "start increasing" << endl;
int a;
cin >> a;
__asm
{
inc a;
}
cout << a << endl;
cout << "end" << endl;
}
void Decrease()
{
cout << "start decreasing" << endl;
int a;
cin >> a;
__asm
{
dec a;
}
cout << a << endl;
cout << "end" << endl;
}
void Negtive()
{
cout << "negtive" << endl;
int a;
cin >> a;
__asm
{
neg a;
}
cout << a << endl;
}
void Subtract()
{
cout << "subtract" << endl;
int a, b;
cin >> a;
cin >> b;
_asm
{
mov eax, a;
mov ebx, b;
sub eax, ebx;
mov a, eax;
}
cout << a << endl;
}
void Multiply()
{
cout << "Multiply" << endl;
int a, b;
cin >> a;
cin >> b;
//mul a eax=eax*a, 結果儲存在eax中
_asm
{
mov eax, a;
mul b;
mov a, eax;
}
cout << a << endl;
}
void AddTwoNumbers(int* storehere, int number1, int number2)
{
_asm
{
mov eax,dword ptr [number1];
add eax,dword ptr [number2];
mov ecx,dword ptr [storehere];
mov dword ptr [ecx],eax;
}
}
void main( )
{
//Add();
//Increase();
//Decrease();
//Negtive();
//AddOther();
//Subtract();
//Multiply();
int a = 3;
int b = 5;
int r;
AddTwoNumbers(&r, a, b);
cout << r << endl;
system("pause");
}
shl 邏輯左移,右面補零
shl 寄存器(設數值為m) 移動位數n
則寄存器結果為2n m
#include <iostream>
int Power(int num, int power);
int main()
{
std::cout << Power( 3, 8);
system("pause");
return 0;
}
int Power(int num, int power)
{
//ECX為位寄存器,它的低位為CX,CX又可分為CH和CL
__asm
{
MOV EAX, num ;
MOV ECX, power //CL為CX的低bit
SHL EAX, CL
}
// 結果儲存在EAX中
}