Exemplo n.º 1
0
main()
{
	FILE *f1 ,*f2, *f3 , *f4;
	char file1[20],file2[20],file3[20];
	printf("Enter source file name:");
	scanf("%s",file1);
	printf("Enter mnemonics file name:");
	scanf("%s",file3);
	printf("Enter destination file name:");
	scanf("%s",file2);
	f1 = fopen(file1 , "r");	//assembly code
	f2 = fopen(file3, "r");	//output of pass1
	f3 = fopen("opcode.txt", "r");	//nmemonics and registers-opcodes
	f4 = fopen(file2, "w");		//final binary output
	

		
	
	char ch , sh , c ,e, c1,d;

	char arr[17] , arr1[17] , arr2[17] ,arr3[17] ,line1[17],temp[17];
	int i=0, j=0 , k=0; int flag =0 , flag1 =0 , line ,  hexb, l=15;
	while(! feof(f1))
	{
		fscanf(f1 , "%s", arr);
		if(strcmp(arr,"0H")==0 || strcmp(arr,"1H")==0 || strcmp(arr,"2H")==0 || 
			strcmp(arr,"3H")==0 || strcmp(arr,"4H")==0 || strcmp(arr,"5H")==0 || 
			strcmp(arr,"6H")==0 || strcmp(arr,"7H")==0 || strcmp(arr,"8H")==0 ||
			strcmp(arr,"9H")==0 || strcmp(arr,"AH")==0 || strcmp(arr,"BH")==0 || 
			strcmp(arr,"CH")==0 || strcmp(arr,"DH")==0 || strcmp(arr,"EH")==0 || strcmp(arr,"FH")==0 ){
				hexa_to_binary(arr , f4);
			}
		
		while(! feof(f3))			//printing opcodes of mnemonics and registers
		{
			flag = 0;
			fscanf(f3 , "%s %s", arr1,temp);
			
			if(strcmp(arr , arr1) == 0)
			{
			
				flag =1;
				fprintf(f4 , "%s", temp);
				break;
			}
			
		}
		
		rewind(f3);
		
		if( !strcmp(arr, ";") || !strcmp(arr , ":"))			//encounter new line
		{
		     fprintf(f4,"\n");
		}	
		if(flag == 0)			//if hexadecimal(address or immediate data) or label
		{
              while((c = fgetc(f2)) != EOF)
	          {
		           if(c == '\n')				//conversion of labels to binary
		  			{
						flag1=0;
						fscanf(f2,"%s %s",arr2,line1);
						if(strcmp(arr2 , arr) == 0)
						{
								flag1 =1;
								fprintf(f4,"\n");
								conv_binary(line1 ,f4); break;
						}
					}
			}
			rewind(f2);
		
			/*if(flag1 ==0)				//conv of addresss and immediate data to binary
			{
				hexa_to_binary(arr , f4);
			}*/
		}
	}
	fclose(f1);
	fclose(f2);
	fclose(f3);
	fclose(f4);
	
}						
Exemplo n.º 2
0
main()
{
	FILE *f1 ,*f2, *f3 , *f4;
	f1 = fopen("code4.txt" , "r");	//assembly code
	f2 = fopen("pass1.txt", "r");	//output of pass1
	f3 = fopen("binary.txt", "r");	//nmemonics and registers-opcodes
	f4 = fopen("bin.txt", "w");		//final binary output
	

		
	
	char ch , sh , c ,e, c1,d;

	char arr[17] , arr1[17] , arr2[17] ,arr3[17] ,line1[17],temp[17];
	int i=0, j=0 , k=0; int flag =0 , flag1 =0 , line ,  hexb, l=15;
	while(! feof(f1))
	{
		fscanf(f1 , "%s", arr);
		
		while(! feof(f3))			//printing opcodes of mnemonics and registers
		{
			flag = 0;
			fscanf(f3 , "%s %s", arr1,temp);
			
			if(strcmp(arr , arr1) == 0)
			{
			
				flag =1;
				fprintf(f4 , "%s", temp);
				break;
			}
		}
		
		rewind(f3);
		if( !strcmp(arr, ";") || !strcmp(arr , ":"))			//encounter new line
		{
		     fprintf(f4,"\n");
		}	
		if(flag == 0)			//if hexadecimal(address or immediate data) or label
		{
              while((c = fgetc(f2)) != EOF)
	          {
		           if(c == '\n')				//conversion of labels to binary
		  			{
						flag1=0;
						fscanf(f2,"%s %s",arr2,line1);
						if(strcmp(arr2 , arr) == 0)
						{
								flag1 =1;
								fprintf(f4,"\n");
								conv_binary(line1 ,f4); break;
						}
					}
			}
			rewind(f2);
		
			if(flag1 ==0)				//conv of addresss and immediate data to binary
			{
				hexa_to_binary(arr , f4);
			}
		}
	}
	fclose(f1);
	fclose(f2);
	fclose(f3);
	fclose(f4);
	
}