Exemple #1
0
static char* getChildString(const char* sect, const char* key)
{
    char        buf[1024];
    char*       ret;

    GetPrivateProfileStringA(sect, key, "-", buf, sizeof(buf), child_file);
    if (buf[0] == '\0' || (buf[0] == '-' && buf[1] == '\0')) return NULL;
    assert(!(strlen(buf) & 1));
    ret = decodeA(buf);
    return ret;
}
Exemple #2
0
//The core function of the assembler
int parser(char *input, char *output)
{
	FILE *fin;
	char line[MAX_CHAR_PER_LINE];
	int i=0,j,k;
	char type;
	char str[MAX_LINES][MAX_CHAR_PER_LINE];
	memset(str,'\0',sizeof(str));
	fin=fopen(input,"rt");
	if(fin==NULL) 
 	{ 
		printf("Error on open input file\n"); 
		exit(1); 
 	}
	printf("Please wait ...\n");
//Delete the whitespace and comments first,
// and then store the command into the array str[].
	while(fgets(line,MAX_CHAR_PER_LINE,fin)!=NULL)
	{
		if(line[0]==13)
		continue;
		k=0;
		for(j=0;line[j]!=13;j++)
		{	
			if(line[j]==' '||line[j]=='\t')
			{
				continue;
			}
			else if(line[j]=='/')
			{
				break;
			}
			else
			str[i][k++]=line[j];
		}
		if(k!=0)
		{
			i++;
		}
	}
	fclose(fin);
	// First pass of the str[], adding the symbol into the symboltable
	int rom=0;
	for(int m=0;m<i;m++)
	{
		type=commandtype(str[m]);
		if(type=='s')
		{
			firstpass(str[m],rom);
		}
		else
		rom++;
	}
	//Second pass of the str[], 
	// then decoding the A,C,L commands into 16-bits binary
	// finally printf the 16 bits into the ".hack" file
	FILE *fout;
	fout=fopen(output,"wt");
	char code[i][17];
	memset(code,'\0',sizeof(code));
	for(int m=0;m<i;m++)
	{
		type=commandtype(str[m]);
		if(type=='a')
		decodeA(str[m],code[m]);
		if(type=='c')
		decodeC(str[m],code[m]);
		if (type=='l')
		decodeL(str[m],code[m]);
		if(type=='a'||type=='c'||type=='l')
	 	fprintf(fout, "%s\n", code[m]);
	}
	fclose(fout);
	printf("Process complete\n");
	return 0;
}