其他分享
首页 > 其他分享> > BUPT大作业-冯诺依曼机

BUPT大作业-冯诺依曼机

作者:互联网

这个玩意当时折磨的我够呛

还是建议自己好好去理解大作业

代码如下:

#include<stdio.h>
#include<string.h>
int ip=0;
int ir=0;
int flag=0; 
int mici( int m)
{
	int i=1;
	int x=0;
	for (x=0;x<m;x++)
	{
		i=i*2;
	}
	return i;
}
int logic1 (int m,int n)
{
	if (m&&n)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int logic2(int m,int n)
{
	if (m||n)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
int logic3(int m)
{
		return !m;
}
int  change (char code[],int m,int n)
{	int y=0;
	int i=m;
	int x=1;
	for (i=m;i<m+n;i++)
	{
		if (code[i]==49)
		{
			y=y+(code[i]-48)*mici(n-x);
		}
		x++;
	}
	if (n==16)
	{
		if (y<=32767)
		{
			return y;
		}
		else
		{
			return y-65536;
		}
	}
	return y;
}
void mission1(char code[],int num[],int mission,int first,int second, int number,int ax[])
{
	if (second==0)
	{
		ax[first]=number;
	}
	else
	{
		if (second>4)
		{
			ax[first]=num[ax[second]-16384];
		}
		else
		{
			num[ax[first]-16384]=ax[second];
		}
	}
}
void mission2(char code[],int num[],int mission,int first,int second, int number,int ax[])
{
	if (mission==2)
	{
		if (second==0)
		{
			ax[first]=ax[first]+number;
		 } 
		 else
		 {
		 	ax[first]=ax[first]+num[ax[second]-16384];
		 }
	}
	else if (mission==3)
	{
			if (second==0)
		{
			ax[first]=ax[first]-number;
		 } 
		 else
		 {
		 	ax[first]=ax[first]-num[ax[second]-16384];
		 }
	}
	else if (mission==4)
	{
		if (second==0)
		{
			ax[first]=ax[first]*number;
		 } 
		 else
		 {
		 	ax[first]=ax[first]*num[ax[second]-16384];
		 }
	}
	else
	{
		if (second==0)
		{
			ax[first]=ax[first]/number;
		 } 
		 else
		 {
		 	ax[first]=ax[first]/num[ax[second]-16384];
		 }
	}
}
void mission3(char code[],int num[],int mission,int first,int second, int number,int ax[])
{
	if (mission==6)
	{
		if (second==0)
		{
			ax[first]=logic1(ax[first],number);
		}
		else
		{
			ax[first]=logic1(ax[first],num[ax[second]-16384]);
		}
	}
	else if (mission==7)
	{
		if (second==0)
		{
			ax[first]=logic2(ax[first],number);
		}
		else
		{
			ax[first]=logic2(ax[first],num[ax[second]-16384]);
		}
	}
	else
	{
		if(second==0)
		{
			ax[first]=logic3(ax[first]);
		}
		else
		{
			num[ax[second]-16384]=logic3(num[ax[second]-16384]);
		}
	}
}
void mission4(char code[],int num[],int mission,int first,int second, int number,int ax[])
{
	if (second==0)
	{
		if (ax[first]==number)
		{
			flag=0;
		}
		if (ax[first]>number)
		{
			flag=1;
		}
		if (ax[first]<number)
		{
			flag=-1;
		}
	}
	else
	{
		if (ax[first]==num[ax[second]-16384])
		{
			flag=0;
		}
		 if (ax[first]>num[ax[second]-16384])
		{
			flag=1;
		}
		if (ax[first]<num[ax[second]-16384])
		{
			flag=-1;
		}
	}
}
void mission5(char code[],int num[],int mission,int first,int second, int number,int ax[])
{
	if (second==0)
	{
		ip=ip+number-4;
	}
	if (second==1)
	{
		if (flag==0)
		{
			ip=ip+number-4;
	}
	}
	if (second==2)
	{
		if (flag==1)
		{
			ip=ip+number-4;
		}
	}
	if (second==3)
	{
		if (flag==-1)
		{
			ip=ip+number-4;
		}
	}
}
void mission6(char code[],int num[],int mission,int first,int second, int number,int ax[])
{
	if (mission==11)
	{
		printf("in:\n");
		scanf("%d",&ax[first]);
	}
	else
	{
		printf("out: ");
		printf("%d\n",ax[first]);
	}
}
void choose(char code[],int num[],int mission,int first,int second, int number,int ax[])
{
	if (mission==1)
	{
		mission1(code,num,mission,first,second,number,ax);
	}
	else if (mission==2||mission==3||mission==4||mission==5)
	{
		mission2(code,num,mission,first,second,number,ax);
	}
	else if (mission==6||mission==7||mission==8)
	{
		mission3(code,num,mission,first,second,number,ax);
	}
	else if (mission==9)
	{
		mission4(code,num,mission,first,second,number,ax);
	}
	else if (mission==10)
	{
		mission5(code,num,mission,first,second,number,ax);
	}
	else if (mission==11||mission==12)
	{
		mission6(code,num,mission,first,second,number,ax);
	}
}
void analyze(char code[],int ax[],int num[])
{
	int mission=1;
	int first=0,second=0,number=0;
	int start=0;
	int i=1;
	int n=0;
	while (mission!=0)
	{	start=ip/4*33;
		ir=change(code,start,16);
		mission=change(code,start,8);
		start=start+8;
		first=change(code,start,4);
		start=start+4;
		second=change(code,start,4);
		start=start+4;
		number=change(code,start,16);
		ip=ip+4;
		choose (code,num,mission,first,second,number,ax);
		printf("ip = %d\n",ip);
		printf("flag = %d\n",flag);
		printf("ir = %d\n",ir);
		for (n=1;n<=4;n++)
		{	if (n==4)
			printf("ax%d = %d\n",n,ax[n]);
			else
			printf("ax%d = %d ",n,ax[n]);
		}
		for (n=5;n<=8;n++)
		{	if (n==8)
			printf("ax%d = %d\n",n,ax[n]);
			else
			printf("ax%d = %d ",n,ax[n]);
		}
		
	}
}
void output(char code[],int num[])
{
	int number=1; 
	ip=0;
	int start;
	int count=0;
	printf("\n");
	printf("codeSegment :\n");
	while (number!=0)
	{
		start=ip/4*33;
		number=change(code,start,32);
		ip=ip+4;
		count++;
		if (count%8==0)
		{
			printf("%d\n",number);
		}
		else
		printf("%d ",number);
	}
	int x=16*8-count;
	for (x;x>0;x--)
	{
		count++;
		if (count%8==0)
		{
			printf("0\n");
		}
		else
		printf("0 ");
	}
	printf("\n");
	printf("dataSegment :\n");
	count=0;
	int i=0;
	for (count=0;count<16*16*2;count=count+2)
	{
		i++;
		if (i%16==0)
		{
			printf("%d\n",num[count]);
		}
		else
		printf("%d ",num[count]);
	}
}
int main()
{
	FILE *fptr;
	fptr=fopen("dict.txt","r");
	char code[16384]={0};
	int num[32768]={0};
	int ax[9]={0};
	int i=0;
	for (i=0;!feof(fptr);i++)
	{
			code[i]=fgetc(fptr);
	}
	analyze(code,ax,num);
	output(code,num);
	return 0;
 } 

标签:int,BUPT,else,second,num,冯诺,依曼机,ax,first
来源: https://blog.csdn.net/m0_53036215/article/details/120219814