?? mubiaodaimajifenpei.cpp
字號:
#include<iostream>
#include<string>
using namespace std;
class Register
{
public:
int num;
int active;
int Print_Register;
string str;
};
void set(Register &a, int num)
{
a.active=1;
a.num=num;
}
void exchange_str_num(string str,int i,int &a,int &b,int i3)
{
int t,k,key,a1,b1,multiples1,multiples2;
a1=0;
b1=0;
multiples1=1;
multiples2=10;
key=1;
t=i;
k=i;
for(t=i;(t>0)&((str[t-1]!='+')&(str[t-1]!='-')&(str[t-1]!='*')&(str[t-1]!='/'));t--)
{
a1=(str[t-1]-48)*multiples1+a1;
multiples1=multiples1*10;
}
for(k=i;(k<(str.length()-1))&((str[k+1]!='+')&(str[k+1]!='-')&(str[k+1]!='*')&(str[k+1]!='/'));k++)
{
b1=b1*multiples2+(str[k+1]-48);
}
a=a1;
b=b1;
if(i3==1)
{
a=a+b;
b=a-b;
a=a-b;
}
}
void exchange_num_str(string &str,int &i,int c)
{
string str1,str2;
int k1,k2,t,k,d,e;
for(t=i;(t>0)&((str[t-1]!='+')&(str[t-1]!='-')&(str[t-1]!='*')&(str[t-1]!='/'));t--)
{
}
k1=t;
for(k=i;(k<(str.length()-1))&((str[k+1]!='+')&(str[k+1]!='-')&(str[k+1]!='*')&(str[k+1]!='/'));k++)
{
}
k2=k;
for(int i1=0;i1<k1;i1++)
{
str1+=str[i1];
}
for(;c>10;)
{
d=c%10;
c=c/10;
str2+=char(d+48);
}
str2+=char(c+48);
e=str2.length();
for(e=e-1;e>=0;e--)
{
str1+=str2[e];
}
for(int j=k2+1;j<(str.length());j++)
{
str1+=str[j];
}
str=str1;
}
void move_num(Register &ax,Register &bx,Register &cx,int &a,int &b)
{
if((ax.Print_Register==1)||(bx.Print_Register==1))
{
if(ax.active==0)
set(ax,b);
else
if(bx.active==0)
set(bx,b);
else
if(cx.active==0)
set(cx,b);
}
else
{
if(ax.active==0)
{
set(ax,a);
}
else
if(bx.active==0)
{
set(bx,a);
}
else
if(cx.active==0)
{
set(cx,a);
}
if(ax.active==0)
set(ax,b);
else
if(bx.active==0)
set(bx,b);
else
if(cx.active==0)
set(cx,b);
}
}
void Print_result(Register &ax,Register &bx,Register &cx,Register &R1,Register &R2,string &str1,string &str2)
{
if(cx.active==1)
{
str1=bx.str;
R1=bx;
str2=cx.str;
R2=cx;
}
else
{
str1=ax.str;
R1=ax;
str2=bx.str;
R2=bx;
}
if((ax.Print_Register==1)||(bx.Print_Register==1))
{
cout<<"MOV "<<str2<<" "<<R2.num<<endl;
}
else
{
cout<<"MOV "<<str1<<" "<<R1.num<<endl;
cout<<"MOV "<<str2<<" "<<R2.num<<endl;
if(R1.str==ax.str)
ax.Print_Register=1;
if(R1.str==bx.str)
bx.Print_Register=1;
if(R1.str==cx.str)
cx.Print_Register=1;
}
if(cx.active==1)
{
cx.active=0;
}
else
{
bx.active=0;
}
}
int MULDIV(Register &ax,Register &bx,Register &cx,Register &dx,Register &R1,Register &R2,string &str1,string &str2,char str)
{
if(str=='*')
{
cout<<"MUL "<<str2<<endl;
if(str1==ax.str)
{
ax.num=R1.num*R2.num;
return ax.num;
}
if(str1==bx.str)
{
bx.num=R1.num*R2.num;
return bx.num;
}
if(str1==cx.str)
{
cx.num=R1.num*R2.num;
return cx.num;
}
if(str1==dx.str)
{
dx.num=R1.num*R2.num;
return dx.num;
}
}
if(str=='/')
{
cout<<"DIV "<<str2<<endl;
if(str1==ax.str)
{
ax.num=R1.num/R2.num;
return ax.num;
}
if(str1==bx.str)
{
bx.num=R1.num/R2.num;
return bx.num;
}
if(str1==cx.str)
{
cx.num=R1.num/R2.num;
return cx.num;
}
if(str1==dx.str)
{
dx.num=R1.num/R2.num;
return dx.num;
}
}
}
int ADDSUB(Register &ax,Register &bx,Register &cx,Register &dx,Register &R1,Register &R2,string &str1,string &str2,char str)
{
if(str=='+')
{
cout<<"ADD "<<str1<<" "<<str2<<endl;
if(str1==ax.str)
{
ax.num=R1.num+R2.num;
return ax.num;
}
if(str1==bx.str)
{
bx.num=R1.num+R2.num;
return bx.num;
}
if(str1==cx.str)
{
cx.num=R1.num+R2.num;
return cx.num;
}
if(str1==dx.str)
{
dx.num=R1.num+R2.num;
return dx.num;
}
}
if(str=='-')
{
cout<<"SUB "<<str1<<" "<<str2<<endl;
if(str1==ax.str)
{
ax.num=R1.num-R2.num;
return ax.num;
}
if(str1==bx.str)
{
bx.num=R1.num-R2.num;
return bx.num;
}
if(str1==cx.str)
{
cx.num=R1.num-R2.num;
return cx.num;
}
if(str1==dx.str)
{
dx.num=R1.num-R2.num;
return dx.num;
}
}
}
void Result(Register ax,Register bx,Register cx,Register dx)
{
cout<<"AX="<<ax.num<<" ";
cout<<"BX="<<bx.num<<" ";
cout<<"CX="<<cx.num<<" ";
cout<<"DX="<<dx.num<<endl<<endl;
}
void main()
{
string str,str1,str2,str3;
Register ax,bx,cx,dx,R1,R2;
int a,b,c;
ax.str="AX";
bx.str="BX";
cx.str="CX";
dx.str="DX";
ax.active=0;
bx.active=0;
cx.active=0;
dx.active=0;
ax.num=0;
bx.num=0;
cx.num=0;
dx.num=0;
ax.Print_Register=0;
bx.Print_Register=0;
cx.Print_Register=0;
dx.Print_Register=0;
cout<<"***********************************************************************"<<endl;
cout<<"***************** 生成目標(biāo)代碼,分配寄存器 *************"<<endl;
cout<<"***********************************************************************"<<endl;
cout<<"請輸入您想轉(zhuǎn)換的表達(dá)式:"<<endl;
getline(cin,str);
int i3=0;
int time=0;
for(int i=0;i<str.length();i++)
{
int g1=0;
if ((str[i]=='*')||(str[i]=='/'))
{
for(int g=i;g>0;g--)
{
if(str[g]=='+')
g1=1;
}
if((g1==1)&(time>0))
{
cout<<"MOV CX AX"<<endl;
cx.num=ax.num;
ax.active=0;
ax.Print_Register=0;
bx.Print_Register=0;
}
exchange_str_num(str,i,a,b,i3);
move_num(ax,bx,cx,a,b);
Print_result(ax,bx,cx,R1,R2,str1,str2);
c=MULDIV(ax,bx,cx,dx,R1,R2,str1,str2,str[i]);
Result(ax,bx,cx,dx);
for(int i2=i;i2>0;i2--)
{
if((str[i2]=='+')||(str[i2]=='-'))
{
i3=1;
i2=0;
}
}
exchange_num_str(str,i,c);
i=0;
}
time++;
}
for(int j=0;j<str.length();j++)
{
if ((str[j]=='+')||(str[j]=='-'))
{
exchange_str_num(str,j,a,b,i3);
move_num(ax,bx,cx,a,b);
Print_result(ax,bx,cx,R1,R2,str1,str2);
c=ADDSUB(ax,bx,cx,dx,R1,R2,str1,str2,str[j]);
Result(ax,bx,cx,dx);
exchange_num_str(str,j,c);
j=0;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -