老师让用C编一个简单的编译器
这是我编写的代码
可不知道为什么在VC运行输入时它说内存无法为WRITTEN
而在TC运行时,第一个WHILE循环无法终止:
#include"stdio.h"
#define N 100
void welcome(void);/*显示欢迎信息*/
void connect(void);/*显示编译信息*/
void computer_dump(long int [],long int ,long int ,long int ,long int ,long int );/*显示计算机的存储*/
main()
{
long int memory[N]={0}/*内存*/, accumulator=0/*累加器*/;
long int count1=0,count=0/*指令总数*/,instructionCounter=0/*跟踪正在执行指令的内存单元*/, instructionRegister=0/*下一条要执行的指令*/, operationCode=0/*当前正在进行的操作*/, operand=0/*指令所操作的内存单元*/;
welcome();
do{
printf("%02ld ? ",count);
scanf("%ld",&memory[count]);
count++;
}
while(memory[count-1]!=-99999};/*读取编译代码*/
memory[count-1]=0;
connect();
for(count1=0;count1<(count-1);count1++)
{
if(memory[count1]<-9999||memory[count1]>+9999)
{printf("***Add machine spillage***\n"); break;}/*确认代码合法*/
instructionRegister=memory[count1];/*读入下一条将执行的指令*/
operationCode=instructionRegister/100;/*读取操作指令*/
operand=instructionRegister%100;/*读取被操作的内存单元*/
{ switch(operationCode) {/*执行指令
case 10: printf("(?) "); scanf("%d",&memory[operand]);/*读取操作*/
break;
case 11: printf("%d",memory[operand]);/*打印操作*/
break;
case 20: accumulator=memory[operand];/*加载到累加器操作*/
break;
case 21: memory[operand]=accumulator;/*从累加器中存储操作*/
break;
case 30: accumulator+=memory[operand];/*加法*/
break;
case 31: accumulator-=memory[operand];/*减法*/
break;
case 32: if(memory[operand]!=0) accumulator/=memory[operand];
else printf("*** Attempt to divided by zero ***\n");/*除法*/
break;
case 33: accumulator*=memory[operand];/*乘法*/
break;
case 40: instructionCounter=operand;/*转移到指定内存*/
break;
case 41: if(accumulator<0) instructionCounter=operand;/*若累加器为负,转移到指定内存*/
break;
case 42: if(accumulator==0) instructionCounter=operand;/*若累加器为0,转移到指定内存*/
break;
case 43: printf("***Simpletron execution terminated***\n");/*终止程序*/
computer_dump(memory,accumulator,instructionCounter,instructionRegister,operationCode,operand);/*打印表格*/
break;
default: printf("*** You input illegal operation code ***\n");/*错误操作码*/
break;
}}
}
return 0;
}
void welcome(void)/*显示欢迎信息*/
{
printf("***Welcome to Simpletron***\n");
printf("***Please enter your program one instruction***\n");
printf("***(or data word) at a time. I will type the***\n");
printf("***location number and a question mark(?)***\n");
printf("***You then type the word for that lacation.***\n");
printf("***Type the sentinel -99999 to stop entering***\n");
printf("***your program***\n");
}
void connect(void)/*显示编译信息*/
{
printf("***Program loading completed***\n");
printf("***Program execution begins***\n");
}
void computer_dump(long int m[],long int p1,long int p2,long int p3,long int p4,long int p5)/*显示计算机的存储*/
{
int i=0,j=0,k=0;
printf("寄存器:\n");
printf("accumulator %+05d\n",p1);
printf("instructionCounter %02d\n",p2);
printf("instructionRegister %+05d\n",p3);
printf("operationCode %02d\n",p4);
printf("operand %02d\n",p5);
printf(" 内存:\n");
printf(" ");
for(;k<=9;k++)
printf("%6d",k);
printf("\n");
for(;i<=9;i++)
{ printf("%4d",i*10);
for(j=0;j<=9;j++)
printf(" %+05d",m[i*10+1]);
printf("\n");
}
}
求大大指点,如果有其他错误的话,也请一并提出
[此贴子已经被作者于2006-3-7 22:23:17编辑过]