//下面四个函数用于检验information数组里内容的类别
int Is_instruction(unsigned char * information,int &num){
char *parameter[]={"BR","ADD","LD","ST","JSR","AND","LDR","STR","RTI","NOT","LDI","STI","JMP","EMPTY_UNDEFINED","LEA","TRAP","GETC","OUT","PUTS","IN","PUTSP","HALT","BRP","BRZ","BRN","BRNZ","BRZP","BRNP","BRNZP","RET","JSRR"};
int i=0;
for(;i<31;++i){
if(!strcmp((char *)information,parameter[i])){
num=i;
return 1;
}
}
return 0;
}
int Is_pesudo(unsigned char * information,int &num){
char *parameter[]={".FILL",".BLKW",".STRINGZ"};
int i=0;
for(;i<3;++i){
if(!strcmp((char *)information,parameter[i])){
num=i;
return 1;
}
}
return 0;
}
int Is_label(unsigned char * &information) {
unsigned char *checker=information;
if(!((*checker>='A'&&*checker<='Z')||(*checker>='a'&&*checker<='z')||*checker=='_')){
if(*checker==';')
return 0;
else{
printf("Can't recognize %s.Maybe an invalib label.\n",information);
exit(1);
}
}
++checker;
while((*checker>='A'&&*checker<='Z')||(*checker>='a'&&*checker<='z')||*checker=='_'||(*checker>='0'&&*checker<='9'))
++checker;
if(*checker==':'&&*(checker+1)==NULL){
*checker=NULL;
return 1;
}
else
if(*checker==NULL)
return 1;
else{
printf("Can't recognize %s.Maybe an invalib label.\n",information);
exit(1);
}
}
int Is_comment(unsigned char * information,unsigned char * &in_now){
if(*information!=';'){
printf("Maybe a bug.\n"); //NOTE
system("pause");
return 0;
}
else{
in_now=in_now-strlen((char *)information)+1;
return 1;
}
}
//Jump_comment:用于使in_now跳过注释部分
void Jump_comment(unsigned char *&in_now){
while(*in_now!='\n')
++in_now;
++in_now;
}
//以下是解析不同指令的函数,即将正确的指令转换为以字符串表示的二进制编码
//若发现指令不正确,程序会输出相应的错误信息,并执行exit(1)
//其中有部分函数极其类似,有注释说明
//not,jmp,jsrr的解析方式类似
int not_operate(unsigned char * &in_now, unsigned char * &out_now){
convert_deci_b(9,out_now,4);
if(*in_now!=EOF){
Jump_Separating(in_now);
if(*in_now!='R'&&*in_now!='r'){
printf("Wrong NOT instruction.\n");
exit(1);
}
++in_now;
if(*in_now>='0'&&*in_now<='7'){
convert_deci_b((int)*in_now-48,out_now,3);
++in_now;
}
else{
printf("Wrong NOT instruction.\n");
exit(1);
}
Jump_Separating(in_now);
if(*in_now==',')
++in_now;
Jump_Separating(in_now);
if(*in_now!='R'&&*in_now!='r'){
printf("Wrong NOT instruction.\n");
exit(1);
}
++in_now;
if(*in_now>='0'&&*in_now<='7'){
convert_deci_b((int)*in_now-48,out_now,3);
++in_now;
}
else{
printf("Wrong NOT instruction.\n");
exit(1);
}
convert_deci_b(63,out_now,6);
return 0;
}
else{
printf("The .asm is end without .END\n");
exit(1);
}
}
int jmp_operate(unsigned char * &in_now,unsigned char * &out_now){
convert_deci_b(96,out_now,7);
if(*in_now!=EOF){
Jump_Separating(in_now);
if(*in_now!='R'&&*in_now!='r'){
printf("Wrong JMP instruction.\n");
exit(1);
}
++in_now;
if(*in_now>='0'&&*in_now<='7'){
convert_deci_b((int)*in_now-48,out_now,3);
++in_now;
}
else{
printf("Wrong JMP instruction.\n");
exit(1);
}
convert_deci_b(0,out_now,6);
return 0;
}
else{
printf("The .asm is end without .END\n");
exit(1);
}
}
int jsrr_operate(unsigned char * &in_now,unsigned char * &out_now){
convert_deci_b(32,out_now,7);
if(*in_now!=EOF){
Jump_Separating(in_now);
if(*in_now!='R'&&*in_now!='r'){
printf("Wrong JSRR instruction.\n");
exit(1);
}
++in_now;
if(*in_now>='0'&&*in_now<='7'){
convert_deci_b((int)*in_now-48,out_now,3);
++in_now;
}
else{
printf("Wrong JSRR instruction.\n");
exit(1);
}
convert_deci_b(0,out_now,6);
return 0;
}
else{
printf("The .asm is end without .END\n");
exit(1);
}
}
//add,and,ldr,str,trap指令的解析方式类似
int add_operate(unsigned char *&in_now, unsigned char * &out_now){
int num_tmp;
unsigned char information[21]="\0";
convert_deci_b(1,out_now,4);
if(*in_now!=EOF){
Jump_Separating(in_now);
if(*in_now!='R'&&*in_now!='r'){
printf("Wrong ADD instruction.\n");
exit(1);
}
++in_now;
if(*in_now>='0'&&*in_now<='7'){
convert_deci_b((int)*in_now-48,out_now,3);
++in_now;
}
else{
printf("Wrong ADD instruction.\n");
exit(1);
}
Jump_Separating(in_now);
if(*in_now==',')
++in_now;
Jump_Separating(in_now);
if(*in_now!='R'&&*in_now!='r'){
printf("Wrong ADD instruction.\n");
exit(1);
}
++in_now;
if(*in_now>='0'&&*in_now<='7'){
convert_deci_b((int)*in_now-48,out_now,3);
++in_now;
}
else{
printf("Wrong ADD instruction.\n");
exit(1);
}
Jump_Separating(in_now);
if(*in_now==',')
++in_now;
Jump_Separating(in_now);
if(*in_now=='R'||*in_now=='r'){
++in_now;
if(*in_now>='0'&&*in_now<='7'){
convert_deci_b(0,out_now,3);
convert_deci_b((int)*in_now-48,out_now,3);
++in_now;
return 0;
}
else{
printf("Wrong ADD instruction.\n");
exit(1);
}
}
else
if(*in_now=='#'||*in_now=='X'||*in_now=='x'||*in_now=='B'||*in_now=='b'||(*in_now>='0'&&*in_now<='9')||*in_now=='-'){
Extract(in_now,information);
convert_char_deci(information,num_tmp);
if(num_tmp>15||num_tmp<-16){
printf("This imm in the ADD instruction is too large.\n");
exit(1);
}
else{
convert_deci_b(1,out_now,1);
convert_deci_b(num_tmp,out_now,5);
return 0;
}
}
else{
printf("Wrong ADD instruction.\n");
exit(1);
}
}
else{
printf("The .asm is end without .END\n");
exit(1);
}
}
int and_operate(unsigned char *&in_now, unsigned char * &out_now){
int num_tmp;
unsigned char information[21]="\0";
convert_deci_b(5,out_now,4);
if(*in_now!=EOF){
Jump_Separating(in_now);
if(*in_now!='R'&&*in_now!='r'){
printf("Wrong AND instruction.\n");
exit(1);
}
++in_now;
if(*in_now>='0'&&*in_now<='7'){
convert_deci_b((int)*in_now-48,out_now,3);
++in_now;
}
else{
printf("Wrong AND instruction.\n");
exit(1);
}
Jump_Separating(in_now);
if(*in_now==',')
++in_now;
Jump_Separating(in_now);
if(*in_now!='R'&&*in_now!='r'){
printf("Wrong AND instruction.\n");
exit(1);
}
++in_now;
if(*in_now>='0'&&*in_now<='7'){
convert_deci_b((int)*in_now-48,out_now,3);
++in_now;
}
else{
printf("Wrong AND instruction.\n");
exit(1);
}
Jump_Separating(in_now);
if(*in_now==',')
++in_now;
Jump_Separating(in_now);
if(*in_now=='R'||*in_now=='r'){
++in_now;
if(*in_now>='0'&&*in_now<='7'){
convert_deci_b(0,out_now,3);
convert_deci_b((int)*in_now-48,out_now,3);
++in_now;
return 0;
}
else{
printf("Wrong AND instruction.\n");
exit(1);
}
}
else
if(*in_now=='#'||*in_now=='X'||*in_now=='x'||*in_now=='B'||*in_now=='b'||(*in_now>='0'&&*in_now<='9')||*in_now=='-'){
Extract(in_now,information);
convert_char_deci(information,num_tmp);
if(num_tmp>15||num_tmp<-16){
printf("This imm in the AND instruction is too large.\n");
exit(1);
}
else{
convert_deci_b(1,out_now,1);
convert_deci_b(num_tmp,out_now,5);
return 0;
}
}
else{
printf("Wrong AND instruction.\n");
exit(1);
}
}
else{
printf("The .asm is end without .END\n");
exit(1);
}
}
int ldr_operate(unsigned char *&in_now, unsigned char * &out_now){
int num_tmp;
unsigned char information[21]="\0";
convert_deci_b(6,out_now,4);
if(*in_now!=EOF){
Jump_Separating(in_now);
if(*in_now!='R'&&*in_now!='r'){
printf("Wrong LDR instruction.\n");
exit(1);
}
++in_now;
if(*in_now>='0'&&*