BUPT-大作业 多核版
作者:互联网
如下:
#include<stdio.h>
#include<windows.h>
#include<string.h>
HANDLE hMutex;
HANDLE Mutex[32768];
int ip[3]={0};//程序计数器
int ir[3]={0};//指令寄存器
int flag[3]={0};//标志寄存器
char code1[16384];//代码段一
char code2[16384];//代码段二
int num[32768];//数据段
int ax[9]={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[],int judge)//比较运算
{
if (second==0)
{
if (ax[first]==number)
{
flag[judge]=0;
}
if (ax[first]>number)
{
flag[judge]=1;
}
if (ax[first]<number)
{
flag[judge]=-1;
}
}
else
{
if (ax[first]==num[ax[second]-16384])
{
flag[judge]=0;
}
if (ax[first]>num[ax[second]-16384])
{
flag[judge]=1;
}
if (ax[first]<num[ax[second]-16384])
{
flag[judge]=-1;
}
}
}
void mission5(char code[],int num[],int mission,int first,int second, int number,int ax[],int judge)//跳转指令
{
if (second==0)
{
ip[judge]=ip[judge]+number-4;
}
if (second==1)
{
if (flag[judge]==0)
{
ip[judge]=ip[judge]+number-4;
}
}
if (second==2)
{
if (flag[judge]==1)
{
ip[judge]=ip[judge]+number-4;
}
}
if (second==3)
{
if (flag[judge]==-1)
{
ip[judge]=ip[judge]+number-4;
}
}
}
void mission6(char code[],int num[],int mission,int first,int second, int number,int ax[],int judge)//输入输出
{
if (mission==11)
{
printf("in:\n");
scanf("%d",&ax[first]);
}
else
{ printf("id = %d ",judge);
printf("out: ");
printf("%d\n",ax[first]);
}
}
void mission7(char code[],int num[],int mission,int first,int second, int number,int ax[])//互斥对象申请
{
WaitForSingleObject(Mutex[number-16384],INFINITE);
}
void mission8(char code[],int num[],int mission,int first,int second, int number,int ax[])//互斥对象释放
{
ReleaseMutex(Mutex[number-16384]);
}
void mission9(char code[],int num[],int mission,int first,int second, int number,int ax[])//休眠
{
Sleep(number);
}
void choose(char code[],int num[],int mission,int first,int second, int number,int ax[],int judge)//分工
{
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,judge);
}
else if (mission==10)
{
mission5(code,num,mission,first,second,number,ax,judge);
}
else if (mission==11||mission==12)
{
mission6(code,num,mission,first,second,number,ax,judge);
}
else if (mission==13)
{
mission7(code,num,mission,first,second,number,ax);
}
else if (mission==14)
{
mission8(code,num,mission,first,second,number,ax);
}
else if (mission==15)
{
mission9(code,num,mission,first,second,number,ax);
}
}
void analyze(char code[],int num[], int judge)//指令分析函数
{
int mission=1;
int first=0,second=0,number=0;
int start=0;
int i=1;
int n=0;
while (mission!=0)
{ start=ip[judge]/4*33;
ir[judge]=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[judge]=ip[judge]+4;
choose(code,num,mission,first,second,number,ax,judge);
WaitForSingleObject(hMutex,INFINITE);
if (judge==1)
{
printf("id = 1\n");
printf("ip = %d\n",ip[judge]);
printf("flag = %d\n",flag[judge]);
printf("ir = %d\n",ir[judge]);
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]);
}
}
if (judge==2)
{
printf("id = 2\n");
printf("ip = %d\n",ip[judge]+256);
printf("flag = %d\n",flag[judge]);
printf("ir = %d\n",ir[judge]);
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]);
}
}
ReleaseMutex(hMutex);
}
}
void output(int num[])//打印代码段和数据段
{
int number=1;
int cnt=0;
int start;
int count=0;
printf("\n");
printf("codeSegment :\n");
while (number!=0)
{
start=cnt/4*33;
number=change(code1,start,32);
cnt=cnt+4;
count++;
if (count%8==0)
{
printf("%d\n",number);
}
else
printf("%d ",number);
}
int x=8*8-count;
for (x;x>0;x--)
{
count++;
if (count%8==0)
{
printf("0\n");
}
else
printf("0 ");
}
number=1;
cnt=0;
count=0;
while (number!=0)
{
start=cnt/4*33;
number=change(code2,start,32);
cnt=cnt+4;
count++;
if (count%8==0)
{
printf("%d\n",number);
}
else
printf("%d ",number);
}
x=8*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]);
}
}
DWORD WINAPI hThread1(LPVOID pPararneter)
{
analyze(code1,num,1);
}
DWORD WINAPI hThread2(LPVOID pPararneter)
{
analyze(code2,num,2);
}
int main()//main函数
{ num[0]=100;
FILE *fptr;
fptr=fopen("C:\\Users\\a2023\\Desktop\\test.txt","r");//文件一的读取
hMutex=CreateMutex(NULL,FALSE,NULL); //请求互斥变量
int i=0;
for (i=0;!feof(fptr);i++)
{
code1[i]=fgetc(fptr);
}
fptr=fopen("C:\\Users\\a2023\\Desktop\\test2.txt","r");//文件二的读取
for (i=0;!feof(fptr);i++)
{
code2[i]=fgetc(fptr);
}
i=0;
while (i<32768)
{
Mutex[i]=CreateMutex(NULL,FALSE,NULL);
i++;
}
HANDLE Thread1=CreateThread(NULL,0,hThread1,NULL,0,NULL);//线程一的创立
HANDLE Thread2=CreateThread(NULL,0,hThread2,NULL,0,NULL);//线程2的创立
WaitForSingleObject(Thread1,INFINITE);
WaitForSingleObject(Thread2,INFINITE);
CloseHandle(Thread1);
CloseHandle(Thread2);
output(num);//打印
return 0;
}//程序结束
标签:核版,int,BUPT,作业,number,else,second,ax,first 来源: https://blog.csdn.net/m0_53036215/article/details/120219824