<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>νιятυαℓ ¢ℓαѕѕ яσσм</title>
	<atom:link href="http://quoans.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://quoans.wordpress.com</link>
	<description>Just another virtual space  to share our knowledge… Especially for the future techies :-)</description>
	<lastBuildDate>Wed, 07 Dec 2011 09:46:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='quoans.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/6adc7ecac40bbd0de57690e1b030a650?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>νιятυαℓ ¢ℓαѕѕ яσσм</title>
		<link>http://quoans.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://quoans.wordpress.com/osd.xml" title="νιятυαℓ ¢ℓαѕѕ яσσм" />
	<atom:link rel='hub' href='http://quoans.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Non Deterministic Finite Automata</title>
		<link>http://quoans.wordpress.com/2010/06/24/non-deterministic-finite-automata/</link>
		<comments>http://quoans.wordpress.com/2010/06/24/non-deterministic-finite-automata/#comments</comments>
		<pubDate>Wed, 23 Jun 2010 19:41:03 +0000</pubDate>
		<dc:creator>Renjith S</dc:creator>
				<category><![CDATA[System Programming]]></category>
		<category><![CDATA[DFA]]></category>
		<category><![CDATA[Implementation of NFA using C]]></category>
		<category><![CDATA[NFA]]></category>
		<category><![CDATA[NFA using C]]></category>
		<category><![CDATA[regular Expression]]></category>

		<guid isPermaLink="false">http://quoans.wordpress.com/?p=248</guid>
		<description><![CDATA[The same program can be used for DFA.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=248&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote>
<h3><span style="color:#003366;"><em> The same program can be used for DFA.</em></span></h3>
</blockquote>
<p><span id="more-248"></span></p>
<p><pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

struct state{
  char final;
  int value;
  struct transition *transList;
};

struct stateList{
  int value;
  struct state *statePtr;
  struct stateList *next;
};

struct transition{
  char symbol;
  struct state *statePtr;
  struct transition *next;
};

int dfa(char * input, struct state *s);
void freemem(struct stateList * sList);
int main(){
  struct stateList *sList = NULL, *sListPtr=NULL, *tmpListPtr = NULL;
  struct state *sPtr, *tmpStatePtr;
  struct transition *tPtr,*tmpTransPtr;
  int stateNo;
  char final='N';
  char symbol=' ';
  char input[80];
  do{
    printf(&quot;Enter State No (0 to Quit ): &quot;);
    scanf(&quot;%d&quot;,&amp;stateNo);
    getchar();
    if(stateNo != 0){
	printf(&quot;Final State ? (Y?N) &quot;);
	scanf(&quot;%c&quot;,&amp;final);
	if(sList == NULL){
	  sList = (struct stateList*) malloc(sizeof(struct stateList));
	  sList -&gt; value = stateNo;
	  sList -&gt; statePtr = (struct state *) malloc (sizeof(struct state));
	  sList -&gt; statePtr -&gt; value = stateNo;
	  sList -&gt; statePtr -&gt; final = final;
	  sList -&gt; statePtr -&gt; transList = NULL;
	  sList -&gt; next= NULL;
	}else{
	  sListPtr = sList;
	  while(sListPtr -&gt; next != NULL){
	    sListPtr = sListPtr-&gt;next;
	  }
	  sListPtr-&gt;next = (struct stateList*) malloc(sizeof(struct stateList));
	  sListPtr = sListPtr-&gt;next;
	  sListPtr -&gt; value = stateNo;
	  sListPtr -&gt; statePtr = (struct state *) malloc (sizeof(struct state));
	  sListPtr -&gt; statePtr -&gt; value = stateNo;
	  sListPtr -&gt; statePtr -&gt; final = final;
	  sListPtr -&gt; statePtr -&gt; transList = NULL;
	  sListPtr -&gt; next= NULL;
	  }
	}
  }while(stateNo != 0);
  
  sListPtr = sList;
  while(sListPtr != NULL){
    printf(&quot;\n\nEnter Transitions from state %d \n&quot;,sListPtr-&gt;value);
    printf(&quot;To State (0 to Quit) : &quot;);
    scanf(&quot;%d&quot;,&amp;stateNo);
    while(stateNo != 0){
	tmpListPtr = sList;
	while(tmpListPtr != NULL){
	  if(tmpListPtr-&gt; value == stateNo){
	    break;
	  }
	  tmpListPtr = tmpListPtr -&gt;next;
	}


	if(tmpListPtr == NULL){
	  printf(&quot;Invalid State\n&quot;);
	}else{
	  getchar();
	  printf(&quot;Enter symbol : &quot;);
	  scanf(&quot;%c&quot;,&amp;symbol);
	  sPtr = sListPtr -&gt; statePtr;
	 
	  if(sPtr-&gt;transList == NULL){
	    sPtr-&gt;transList = (struct transition *) malloc (sizeof (struct transition));
	    tPtr = sPtr-&gt;transList;
	  }else{
	    tPtr = sPtr-&gt;transList;
	    while(tPtr-&gt;next != NULL){
		tPtr = tPtr-&gt;next;
	    }
	    tPtr -&gt; next = (struct transition *) malloc (sizeof (struct transition));
	    tPtr = tPtr -&gt;next;  
	  }
	  tPtr -&gt; symbol = symbol;
	  tPtr -&gt; statePtr = tmpListPtr-&gt;statePtr;
	  tPtr -&gt; next = NULL;
	  
	}
	printf(&quot;\nTo State (0 to Quit) : &quot;);
	scanf(&quot;%d&quot;,&amp;stateNo);
    }
    sListPtr = sListPtr -&gt; next;
  }
  if(sList != NULL){
    printf(&quot;\nEnter String : &quot;);
    scanf(&quot;%s&quot;,input);
    while(strcmp(input,&quot;q&quot;) != 0){
	if( dfa(input,sList-&gt;statePtr )){
	  printf(&quot;String Accepted.&quot;);
	}
	else{
	  printf(&quot;String Not accepted!!! . &quot;);
	}
	printf(&quot;\nEnter String (q to Quit): &quot;);
	scanf(&quot;%s&quot;,input);
    }
  }  
  
  freemem(sList);
}

int dfa(char * input, struct state *s){
  struct transition * t;
  int accept =0;
  // printf(&quot;\nInput: %s\n&quot;,input);
  // printf(&quot;Value : %d\n&quot;,s-&gt;value);
  //printf(&quot;Final: %c\n&quot;,s-&gt;final);

  if(s == NULL){
    return 0;
  }else{
    t = s -&gt; transList;
    if(input[0] == '&#092;&#048;' &amp;&amp; s -&gt; final == 'Y'){
	return 1;
    }
    
    while( t != NULL &amp;&amp; accept ==0){
	//	printf(&quot;State Transition symbol : %c\n&quot;,t-&gt;symbol);
	if(t -&gt; symbol == input[0]){
	  //printf(&quot;Transist to the state : %d\n&quot;,t-&gt;statePtr-&gt;value);
	  accept= dfa(input+1,t-&gt;statePtr); 
	  }
	t = t -&gt; next;
    }
  }
  return accept;
} 

void freemem(struct stateList * sList){
  struct stateList *tsList;
  struct state * tstate;
  struct transition *trans, *trans2;
  while(sList != NULL){
    tsList = sList-&gt;next;
    
    tstate = sList -&gt; statePtr;
    trans = tstate -&gt; transList;
    while(trans != NULL){
	trans2 = trans-&gt;next;
	free(trans);
	trans = trans2;
    }
    free(tstate);
    free(sList);
    sList = tsList;
  }
}

Sample output for regExpression: (a|b)*abb

Enter State No (0 to Quit ): 1
Final State ? (Y?N) N
Enter State No (0 to Quit ): 2
Final State ? (Y?N) N
Enter State No (0 to Quit ): 3
Final State ? (Y?N) N
Enter State No (0 to Quit ): 4
Final State ? (Y?N) Y
Enter State No (0 to Quit ): 0


Enter Transitions from state 1 
To State (0 to Quit) : 1
Enter symbol : a

To State (0 to Quit) : 1
Enter symbol : b

To State (0 to Quit) : 2
Enter symbol : a

To State (0 to Quit) : 0


Enter Transitions from state 2 
To State (0 to Quit) : 3
Enter symbol : b

To State (0 to Quit) : 0


Enter Transitions from state 3 
To State (0 to Quit) : 4
Enter symbol : b

To State (0 to Quit) : 0


Enter Transitions from state 4 
To State (0 to Quit) : 0

Enter String : abb
String Accepted.
Enter String (q to Quit): aaaabb 
String Accepted.
Enter String (q to Quit): aaaabbbbbb
String Not accepted!!! . 
Enter String (q to Quit): quit
String Not accepted!!! . 
Enter String (q to Quit): q


</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quoans.wordpress.com/248/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/quoans.wordpress.com/248/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/quoans.wordpress.com/248/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/quoans.wordpress.com/248/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/quoans.wordpress.com/248/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/quoans.wordpress.com/248/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/quoans.wordpress.com/248/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/quoans.wordpress.com/248/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/quoans.wordpress.com/248/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/quoans.wordpress.com/248/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/quoans.wordpress.com/248/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/quoans.wordpress.com/248/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/quoans.wordpress.com/248/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/quoans.wordpress.com/248/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=248&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://quoans.wordpress.com/2010/06/24/non-deterministic-finite-automata/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1dd47264df9d720f9ebdca2b1a0beb14?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Renjith S</media:title>
		</media:content>
	</item>
		<item>
		<title>Macro Intergrated Assembler</title>
		<link>http://quoans.wordpress.com/2010/06/17/macro-intergrated-assembler/</link>
		<comments>http://quoans.wordpress.com/2010/06/17/macro-intergrated-assembler/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 19:09:22 +0000</pubDate>
		<dc:creator>Renjith S</dc:creator>
				<category><![CDATA[System Programming]]></category>

		<guid isPermaLink="false">http://quoans.wordpress.com/?p=225</guid>
		<description><![CDATA[This is simply the combination of 2-Pass assembler and a Macro Processor .<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=225&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote>
<h3><span style="color:#003366;"><em> This is simply the combination of 2-Pass assembler and a Macro Processor .</em></span></h3>
</blockquote>
<p><span id="more-225"></span></p>
<p><pre class="brush: cpp;">
 
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;errno.h&gt;

#define TRUE 1
#define FALSE 0
#define lenOPTAB 7

struct{
    char opcode[32];
    char hexcode[3];
}OPTAB[]={
    {&quot;ADD&quot;,&quot;08&quot;},
    {&quot;SUB&quot;,&quot;1C&quot;},
    {&quot;MUL&quot;,&quot;20&quot;},
    {&quot;DIV&quot;,&quot;24&quot;},
    {&quot;LDA&quot;,&quot;AC&quot;},
    {&quot;END&quot;,&quot;00&quot;},
    {&quot;MEND&quot;,&quot;FF&quot;},

};

typedef struct deftab{
    char statement[80];
    struct deftab * next;
} DEFTAB;

typedef struct ntab{
    char name[80];
    DEFTAB * start;
    DEFTAB * end;
    struct ntab * next;
}NAMETAB;

NAMETAB *nameTab;

char expanding = FALSE;

struct{
    char label[32];
    int address;
}SYMTAB[100];


typedef struct{
    char label[32];
    char opcode[32];
    char operand[32];

}Token;

typedef struct{
    int address;
    Token src;
    char hexcode[30];
}asmlist;

int lenSRC = 0;
int lenSYMTAB = 0;
int startAddr =0;

void tokenize( char * line, Token *tok);
int  getline(FILE *fp,char * line);
char isOPCODE( char * str);

int pass1(FILE *srcfp, FILE *intfp);
void pass2(FILE *intrfp, FILE *lstfp, FILE *hexfp);

char searchSYMTAB( char * str);
void insertSYMTAB( char * label,int locCntr);
void tokenizeIntr( char * line, asmlist *list);
void initializeTextRec(char *textRec,asmlist list);
void writeObjTextRec(FILE *fp, char *str);

int  getAddress(char *symbol);
char * getHexOperand(char * str, char *hexOperand);
char * getOpcodeHex(char *op);
int operandLen(char *str);

void writeintrfile(asmlist list,FILE *fp);
void writelistfile(FILE *fp, asmlist list);
void writeobjheader(FILE *fp,asmlist list);
void writeEnd(FILE *fp);

void macroexpand(FILE *srcfp,FILE *outfp);
void processline(Token tok,FILE *srcfp,FILE *outfp);
NAMETAB * searchNAMETAB(char *str);
void define(Token tok,FILE *fp);
void expand(Token tok,FILE *srcfp, FILE *outfp);
void replaceARG(char *line,char ARGTAB[10][80]);
int readline(FILE *fp,char * line);
NAMETAB*  putInNAMETAB(Token tok);
void getDefLine (char *line, char ARGTAB[10][80], DEFTAB *def);
void putPrototypeInDEFTAB(Token tok,DEFTAB *def);
int getparameter(char str[80],char pList[10][80]);
void subParmeter(char *line,int pCount, char pList[10][80]);
void writeline(Token tok,FILE *fp);
void cleanMem();

int  main(int argc, char * argv[]){
    FILE *srcfp, *lstfp, *intrfp, *hexfp,*outfp;
    char lstfname[30], intrfname[30],hexfname[30];
    char outfilename[80];

    if (argc != 2){
	printf(&quot;Invalid usage !!!&quot;);
	return;
    }

    // generate filename for list,intermediate and hexfiles.
    if( strstr(argv[1],&quot;.&quot;) ){
	strcpy(lstfname,argv[1]);
	strcpy(strstr(lstfname,&quot;.&quot;),&quot;.lst&quot;);
	strcpy(intrfname,argv[1]);
	strcpy(strstr(intrfname,&quot;.&quot;),&quot;.int&quot;);
	strcpy(hexfname,argv[1]);
	strcpy(strstr(hexfname,&quot;.&quot;),&quot;.hex&quot;);
	strcpy(outfilename,argv[1]);
	strcpy(strstr(outfilename,&quot;.&quot;),&quot;_expanded.asm&quot;);

    }else{
	strcpy(lstfname,argv[1]);
	strcat(lstfname,&quot;.lst&quot;);
	strcpy(intrfname,argv[1]);
	strcat(intrfname,&quot;.int&quot;);
	strcpy(hexfname,argv[1]);
	strcat(hexfname,&quot;.hex&quot;);
	strcpy(outfilename,argv[1]);
	strcat(outfilename,&quot;_expanded.asm&quot;);

    }


    srcfp=fopen(argv[1],&quot;r&quot;);
    if(srcfp == NULL){
	printf(&quot;!!! Error. Unable to open the src file. &quot;);
	return;
    }

    outfp = fopen(outfilename,&quot;w&quot;);
    if(outfp == NULL){
	printf(&quot;Unable to open outfile.\n&quot;);
	fclose(srcfp);
	return;
    }
    nameTab = NULL;
    macroexpand(srcfp,outfp);
    cleanMem();
    fclose(srcfp);
    fclose(outfp);

    srcfp=fopen(outfilename,&quot;r&quot;);
    if(srcfp == NULL){
	printf(&quot;!!! Error. Unable to open the src file. &quot;);
	return;
    }


    intrfp=fopen(intrfname,&quot;w&quot;);
    if(intrfp == NULL){
	printf(&quot;!!! Error. Unable to open the intermediate file. &quot;);
	fclose(srcfp);
	return;
    }

    lstfp=fopen(lstfname,&quot;w&quot;);
    if(lstfp == NULL){
	printf(&quot;!!! Error. Unable to open the list file. &quot;);
	fclose(srcfp);
	fclose(intrfp);
	return;
    }

    hexfp=fopen(hexfname,&quot;w&quot;);
    if(hexfp == NULL){
	printf(&quot;!!! Error. Unable to open the hex file. &quot;);
	fclose(srcfp);
	fclose(intrfp);
	fclose(lstfp);
	return;
    }

    // pass 1 generate intermediate file
    if(! pass1(srcfp,intrfp)){
	fclose(srcfp);
	fclose(intrfp);
	return;
    }

    fclose(srcfp);
    fclose(intrfp);

    intrfp=fopen(intrfname,&quot;r&quot;);
    if(intrfp == NULL){
	printf(&quot;!!! Error. Unable to open the intermediate file. &quot;);
	fclose(srcfp);
	return;
    }

    //generate list file and hexfile
    pass2(intrfp,lstfp,hexfp);

    fclose(lstfp);
    fclose(hexfp);
}

int pass1(FILE *srcfp, FILE *intrfp){
    char line[80];
    char *cptr;

    Token tok;
    int locCntr;
    asmlist list;
    if(! getline(srcfp,line)) return;  // read first line

    tokenize(line,&amp;tok);  // tokenize the line

    if( strcasecmp(tok.opcode,&quot;START&quot;) == 0){
	locCntr = strtoul(tok.operand,0,16);
	list.address = locCntr;
	list.src = tok;
	writeintrfile(list,intrfp);
	if(!getline(srcfp,line)) return;
	tokenize(line,&amp;tok);

    }else{
	locCntr =0;
    }

    startAddr=locCntr;
    lenSRC = locCntr;

    while( strcasecmp(tok.opcode,&quot;END&quot;) != 0){

	// check for comments. comments start with a '.'

	if( line[0] != '.'){

	    list.address = locCntr;
	    list.src = tok;
	    writeintrfile(list,intrfp); // writes intermediate file.

	    // check for label
	    if(strlen(tok.label) != 0){
		if(searchSYMTAB(tok.label)){
		    printf(&quot;Error : duplicate symbol @ %s&quot;,line);
		    return 0;
		}else{
		    //insert symboltable entry
		    insertSYMTAB(tok.label,locCntr);  
		}
	    }

	    //Finds instruction length
	    if( isOPCODE(tok.opcode)){
		locCntr = locCntr +3;
	    }
	    else if( strcasecmp(tok.opcode,&quot;WORD&quot;) == 0){
		locCntr = locCntr +3;
	    }
	    else if( strcasecmp(tok.opcode,&quot;RESW&quot;) == 0){
		// operand length is specified in the instruction itself
		if(tok.operand[0] == 'X'){
		    cptr=strtok(tok.operand,&quot; '&quot;);
		    if(cptr != NULL)
			locCntr = locCntr + 3* strtoul(cptr,0,16);
		}
		else{
		    locCntr = locCntr + 3* strtoul(tok.operand,0,10);
		}
	    }
	    else if( strcasecmp(tok.opcode,&quot;RESB&quot;) == 0){
		// operand length is in instruction itself
		if(tok.operand[0] == 'X'){
		    cptr=strtok(tok.operand,&quot; '&quot;);
		    if(cptr != NULL)
			locCntr = locCntr + strtoul(cptr,0,16);
		}
		else{
		    locCntr = locCntr +  strtoul(tok.operand,0,10);
		}
	    }
	    else if( strcasecmp(tok.opcode,&quot;BYTE&quot;) == 0){
		//length of the operand
		locCntr = locCntr + operandLen(tok.operand);
	    }
	    else{
		printf(&quot;Error Invalid operation code. @ %s &quot;,line);
		return 0;
	    }
	}
	if(!getline(srcfp,line)) return 0;
	tokenize(line,&amp;tok);
    }

    list.address = locCntr;
    list.src = tok;
    writeintrfile(list,intrfp);

    lenSRC = locCntr - lenSRC;

    return 1;
}

void pass2(FILE *intrfp, FILE *lstfp, FILE *hexfp){
    char line[80];
    asmlist list;
    Token *tok;
    tok = &amp;(list.src);

    char textRec[80];
    int operandAddr=0;
    char hexOperand[80];
    char hexObj[80],temp[80];
    int Index=9;

    if(!getline(intrfp,line)) return;
    tokenizeIntr(line,&amp;list);//tokenize the intermediate code

    if (strcasecmp(tok-&gt;opcode,&quot;START&quot;) == 0 ){
	writelistfile(lstfp,list);

	if(!getline(intrfp,line)) return;
	tokenizeIntr(line,&amp;list);
    }
    writeobjheader(hexfp,list);  //H+Pgm_Name+Start_Addr+Length
    initializeTextRec(textRec,list); //  T+Starting address

    while(strcasecmp(tok-&gt;opcode,&quot;END&quot;) != 0){
	// Valid Opcode  
	if(isOPCODE(tok-&gt;opcode)){

	    if(strlen(tok -&gt; operand) != 0){
		if(searchSYMTAB(tok-&gt;operand)){
		    // get symbol address
		    operandAddr=getAddress(tok-&gt;operand);
		    //opcode+label address
		    sprintf(hexObj,&quot;%s%04X&quot;,getOpcodeHex(tok -&gt; opcode), operandAddr);
		}
		else{
		    if( getHexOperand(tok-&gt;operand,hexOperand) == NULL ){
			printf(&quot;Invalid operand @ %s&quot;,line);
		    }
		    //opcode+operand value
		    sprintf(hexObj,&quot;%s%s&quot;,getOpcodeHex(tok -&gt; opcode),hexOperand);
		}
	    }
	}

	else{
	    if(strcasecmp(tok -&gt; opcode,&quot;BYTE&quot;) == 0 ||	\
	       strcasecmp(tok -&gt; opcode,&quot;WORD&quot;) == 0){
		getHexOperand(tok-&gt;operand,hexOperand);
		//operand
		sprintf(hexObj,&quot;%s&quot;,hexOperand);
	    }
	    else{
		getHexOperand(tok-&gt;operand,hexOperand);
		//operand
		sprintf(hexObj,&quot;%s&quot;,hexOperand);

	    }
	}

	if((Index+strlen(hexObj)) &gt; 69){
	    sprintf(textRec+7,&quot;%02X%s&quot;,(strlen(textRec)-9)/2,textRec+9);//place length in lenField
	    writeObjTextRec(hexfp,textRec);
	    initializeTextRec(textRec,list);
	}

	strncpy(temp,textRec+1,6);
	Index = ((list.address - strtoul(temp,0,16))*2 )+9;

	sprintf(textRec+Index,&quot;%s&quot;,hexObj);
	Index =Index + strlen(hexObj);
	strcpy(list.hexcode,hexObj);

	writelistfile(lstfp,list);

	if(!getline(intrfp,line)) return;
	tokenizeIntr(line,&amp;list);

    }
    sprintf(textRec+7,&quot;%02X%s&quot;,(strlen(textRec)-9)/2,textRec+9);
    writeObjTextRec(hexfp,textRec);
    writeEnd(hexfp);
} 


void insertSYMTAB( char * label,int locCntr){
    strcpy(SYMTAB[lenSYMTAB].label,label);
    SYMTAB[lenSYMTAB].address = locCntr;
    lenSYMTAB ++;
    return;
}

int getline(FILE *fp,char * line){
    char ch;
    int cnt=0;
    while ( (ch = fgetc(fp)) != EOF &amp;&amp; ch != '\n' ){
	line[cnt++] = ch;
    }
    line[cnt] = '&#092;&#048;';

    if(line [0] == '&#092;&#048;')
	return 0;
    else
	return 1;
}


void tokenizeIntr( char * line, asmlist *list){
    char *chr;
    Token *tok;
    tok = &amp; (list -&gt; src);
    list -&gt; address = 0;
    strcpy(list-&gt;hexcode,&quot;&quot;);
    strcpy(tok -&gt; label, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; opcode, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; operand, &quot;&#092;&#048;&quot;);

    chr =strtok(line, &quot; \t&quot;);

    if( chr != NULL){
	list -&gt; address = strtoul(chr,0,16);
	chr = strtok(NULL, &quot; \t&quot;);

	if ( isOPCODE(chr)){
	    strcpy(tok -&gt; opcode,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; operand, chr);
	}else{
	    strcpy(tok-&gt;label,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; opcode,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; operand, chr);
	}
    }
    //printf(&quot; %s | %s | %s \n&quot;, tok-&gt; label, tok-&gt; opcode, tok -&gt; operand);
    return;
}

char searchSYMTAB( char * str){
    int i;
    for (i=0; i &lt; lenSYMTAB; i++){
	if( strcasecmp(SYMTAB[i].label,str) == 0)
	    return 1;
    }
    return 0;
}
void writeintrfile(asmlist list,FILE *fp){
    fprintf(fp,&quot;%X\t%s\t%s\t%s\n&quot;,list.address,list.src.label,list.src.opcode,list.src.operand);
    return;
}
void writelistfile(FILE *fp, asmlist list){
    fprintf(fp,&quot;%06X\t%s\t%s\t%s\t%s\n&quot;,list.address,list.src.label,list.src.opcode,list.src.operand,list.hexcode);
}

void writeobjheader(FILE *fp,asmlist list){
    char line[80];
    int i;

    line [0]='H';
    strncpy(line+1,list.src.label,6);
    sprintf(line+7,&quot;%06X&quot;,list.address);
    sprintf(line+13,&quot;%06X&quot;,lenSRC);
    line[19]='&#092;&#048;';

    for( i=0; i&lt; 19; i++){
	if(line[i] =='&#092;&#048;')
	    line[i]=' ';
    }
    fprintf(fp,&quot;%s\n&quot;,line);

}

void writeEnd(FILE *fp){

    fprintf(fp,&quot;E%06X&quot;,startAddr);

}
void initializeTextRec( char *textRec,asmlist list){
    int i;
    for(i=0;i&lt;80;i++){
	textRec[i]='0';
    }
    textRec[0]='T';
    sprintf(textRec+1,&quot;%06X&quot;,list.address);
    sprintf(textRec+7,&quot;%02X&quot;,00);
    return;
}

int getAddress(char *symbol){
    int i;
    for(i=0; i&lt; lenSYMTAB; i++){
	if(strcasecmp(SYMTAB[i].label,symbol) == 0)
	    return SYMTAB[i].address;
    }
    return 0;
}
char * getOpcodeHex(char *op){
    int i;
    for(i=0; i&lt; lenOPTAB; i++){
	if(strcasecmp(OPTAB[i].opcode,op) == 0)
	    return OPTAB[i].hexcode;
    }
    return &quot;00&quot;;
}

int operandLen(char *str){
    char *cptr;
    //Hex Operand
    if(str[0] == 'X' || str[0]=='C'){
	cptr = strtok(str+1,&quot; '&quot;);
	if(cptr != NULL){
	    return strlen(cptr);
	}
	else{
	    return 0;
	}
    }
    return 0;
}

char * getHexOperand(char * strOperand, char *hexOperand){
    int i;
    char *cptr;
    char str[80];
    strcpy(str,strOperand);
    //Hex Operand
    if(str[0] == 'X'){
	cptr = strtok(str+1,&quot; '&quot;);
	if(cptr != NULL){
	    sprintf(hexOperand,&quot;%04X&quot;,(unsigned int)strtoul(cptr,0,16));
	}
	else{
	    return NULL;
	}
    }
    //Charector operand
    else if(str[0] == 'C'){

	cptr = strtok(str+1,&quot; '&quot;);
	if(cptr != NULL){

	    for( i=0; cptr[i] != '&#092;&#048;'; i++){
		sprintf(hexOperand+(2*i),&quot;%02X&quot;,cptr[i]);
	    }
	    hexOperand[2*i]='&#092;&#048;';

	}
	else{
	    return NULL;
	}
    }
    //absolute value.
    else if(str[0] == '#'){
	if( searchSYMTAB(str+1)){
	    sprintf(hexOperand,&quot;%04X&quot;,getAddress(str+1));

	}else{
	    errno =0;
	    sprintf(hexOperand,&quot;%04d&quot;,(int)strtoul(str,0,10));

	    if(errno == EINVAL){
		return NULL;
	    }
	}
    }
    else{

	sprintf(hexOperand,&quot;%06X&quot;,(unsigned int)strtoul(str,0,16));

    }
    return hexOperand;

}

void writeObjTextRec(FILE *fp, char *str){
    fprintf(fp,&quot;%s\n&quot;,str);
}


void macroexpand(FILE *srcfp,FILE *outfp){
    char line[80];
    Token tok;

    expanding = FALSE;
    readline(srcfp,line);
    tokenize(line,&amp;tok);

    while( strcasecmp(tok.opcode,&quot;END&quot;) != 0){
	processline(tok,srcfp,outfp);
	readline(srcfp,line);
	tokenize(line,&amp;tok);
    }
    writeline(tok,outfp);
    return;
}

void processline(Token tok,FILE *srcfp,FILE *outfp){
    if(searchNAMETAB(tok.opcode)){
	expand(tok,srcfp,outfp);
    }else if (strcasecmp(tok.opcode,&quot;MACRO&quot;) == 0){
	define(tok,srcfp);
    }
    else{
	writeline(tok,outfp);
    }
}

NAMETAB * searchNAMETAB(char *str){
    NAMETAB *nTab;
    nTab = nameTab;
    while(nTab != NULL){
	if(strcasecmp(nTab-&gt;name,str)== 0){
	    return nTab;
	}
	nTab = nTab-&gt;next;
    }
    return NULL;
}

void define(Token tok,FILE *fp){
    int level=1,pCount;
    int i;
    char line[80],line1[80];
    char pList[10][80]; //contain parameters
    NAMETAB *nTab;

    DEFTAB *def,*tmpdef,*start,*end;
    nTab = putInNAMETAB(tok);

    def =(DEFTAB*) malloc(sizeof(DEFTAB));
    def-&gt;next =NULL;
    start = def;

    putPrototypeInDEFTAB(tok,def);

    pCount = getparameter(tok.operand,pList);

    while(level &gt; 0){
	readline(fp,line);
	strcpy(line1,line);
	tokenize(line1,&amp;tok);

	if(line[0] != '.') {//not a comment line

	    subParmeter(line,pCount,pList);

	    //enter line to deftab;
	    tmpdef =(DEFTAB*) malloc(sizeof(DEFTAB));
	    strcpy(tmpdef-&gt;statement,line);      
	    tmpdef -&gt;next = NULL;
	    def-&gt;next = tmpdef;
	    def = def-&gt;next;

	    if(strcasecmp(tok.opcode,&quot;MACRO&quot;)==0){
		level = level+1;
	    }else if(strcasecmp(tok.opcode,&quot;MEND&quot;)==0){
		level = level-1;
	    }
	}
    }

    nTab-&gt;start = start;
    nTab-&gt;end   = def;

}

void expand(Token tok,FILE *srcfp, FILE *outfp){
    char ARGTAB[10][80];
    int pCount;
    char line[80];

    NAMETAB *nTab;
    DEFTAB *def;

    expanding = TRUE;
    pCount = getparameter(tok.operand,ARGTAB);
    nTab = searchNAMETAB(tok.opcode);

    def = nTab-&gt;start;
    def = def -&gt; next;
    getDefLine(line,ARGTAB,def);

    tokenize(line,&amp;tok);
    def = def-&gt;next;  
    while(strcasecmp(tok.opcode,&quot;MEND&quot;) != 0){
	processline(tok,srcfp,outfp);
	getDefLine(line,ARGTAB,def);
	tokenize(line,&amp;tok);
	def = def-&gt;next;
    }

    expanding = FALSE;

}

void getDefLine (char *line, char ARGTAB[10][80], DEFTAB *def){
    char str[80];
    Token tok;

    strcpy(str,def-&gt;statement);
    tokenize(str,&amp;tok);
    replaceARG(tok.operand,ARGTAB);
    sprintf(line,&quot;%s %s %s&quot;,tok.label, tok.opcode, tok.operand);
    return;
}

void replaceARG(char *line,char ARGTAB[10][80]){
    char *str, tail[80];
    int count;
    str = strstr(line,&quot;?&quot;);
    while( str ){
	strcpy(tail,str+2);
	count = (*(str+1) - 0x30);
	strcpy(str,ARGTAB[count]);
	strcat(str,tail); 
	str = strstr(line,&quot;?&quot;);   

    }
    return;
}

int readline(FILE *fp,char * line){
    char ch;
    int cnt=0;
    while ( (ch = fgetc(fp)) != EOF &amp;&amp; ch != '\n' ){
	line[cnt++] = ch;
    }
    line[cnt] = '&#092;&#048;';

    if(line [0] == '&#092;&#048;')
	return 0;
    else
	return 1;
}

NAMETAB*  putInNAMETAB(Token tok){
    NAMETAB *ptrNameTab=NULL;
    if(nameTab == NULL){
	ptrNameTab = (NAMETAB*) malloc(sizeof(NAMETAB));

	ptrNameTab -&gt; start = NULL;
	ptrNameTab -&gt; end   = NULL;
	ptrNameTab -&gt; next  = NULL;
	nameTab = ptrNameTab;
    }else{
	ptrNameTab = nameTab;
	while(ptrNameTab -&gt;next == NULL){
	    ptrNameTab = ptrNameTab -&gt; next;
	}
	ptrNameTab -&gt; next = (NAMETAB*) malloc(sizeof(NAMETAB));
	ptrNameTab = ptrNameTab-&gt; next;
	ptrNameTab -&gt; start = NULL;
	ptrNameTab -&gt; end   = NULL;
	ptrNameTab -&gt;next = NULL;

    }

    strcpy(ptrNameTab-&gt;name,tok.label);
    return ptrNameTab;

}
void putPrototypeInDEFTAB(Token tok,DEFTAB *def){
    sprintf(def-&gt;statement,&quot;%s %s %s&quot;,tok.label,tok.opcode,tok.operand);
}

int getparameter(char str[80],char pList[10][80]){
    char *chr;
    int cnt=0;
    chr = strtok(str,&quot;.?, \n\t&quot;);

    while(chr != NULL){
	strcpy(pList[cnt],chr);
	cnt++;
	chr= strtok(NULL,&quot;.?, \n\t&quot;);
    }
    return cnt;
}

void subParmeter(char *line,int pCount, char pList[10][80]){
    int i;
    char *str;
    char *tail;
    for(i=0; i&lt;pCount; i++){
	str = strstr(line,pList[i]);
	if(str != NULL){
	    tail = str+strlen(pList[i]);
	    sprintf(str,&quot;?%d&quot;,i);
	    strcat(str,tail);
	}
    }
}

void writeline(Token tok,FILE *fp){
    fprintf(fp,&quot;%s %s %s\n&quot;,tok.label,tok.opcode,tok.operand);
    return;
}
void tokenize( char * line, Token *tok){
    char *chr;
    strcpy(tok -&gt; label, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; opcode, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; operand, &quot;&#092;&#048;&quot;);

    chr =strtok(line, &quot; \t&quot;);

    if( chr != NULL){
	//check for opcode
	if ( isOPCODE(chr) || searchNAMETAB(chr) ){
	    strcpy(tok -&gt; opcode,chr); // instruction opcode+operand
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; operand, chr);
	}else{  // instruction label+opcode+operand
	    strcpy(tok-&gt;label,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; opcode,chr);
	    chr = strtok(NULL, &quot;\n\t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; operand, chr);
	}
    }
    //  printf(&quot; %s | %s | %s \n&quot;, tok-&gt; label, tok-&gt; opcode, tok -&gt; operand);
    return;
}

char isOPCODE( char * str){
    int i;
    for (i=0; i &lt; lenOPTAB; i++){
	if( strcasecmp(OPTAB[i].opcode,str) == 0)
	    return 1;
    }
    return 0;
}

void cleanMem(){
    NAMETAB *nTab,*tmp;
    DEFTAB *def,*tmpDef;
    nTab = nameTab;
    while(nTab != NULL){
	def = nTab-&gt;start;
	while(def != NULL){
	    tmpDef = def;
	    def = def-&gt; next;
	    free(tmpDef);
	}
	tmp = nTab;
	nTab = nTab-&gt;next;
	free(tmp);
    }
}  

Sample Output;
test.asm    &lt;-- Input
COPY  START 0000
TEST  MACRO &amp;ONE, &amp;&amp;TWO
	LDA &amp;ONE
	ADD THREE&lt;
MEND
FIRST	SUB   THREE
	TEST  LENGTH,RETADR
EOF   BYTE  C'eof'
THREE WORD  3
ZERO 	WORD 	0
RETADR	RESW	1
LENGTH	RESW	1
MESS1   BYTE  C'HELLO_WORLD'
	  END
test.lst
000000	COPY	START	0000
000000	FIRST	SUB	THREE	1C0009
000003		LDA	LENGTH	AC0012
000006		ADD	THREE	080009
000009	EOF	BYTE	C'eof'	656F66
000009	THREE	WORD	3	000003
00000C	ZERO	WORD	0	000000
00000F	RETADR	RESW	1	000001
000012	LENGTH	RESW	1	000001
000015	MESS1	BYTE	C'HELLO_WORLD'	48454C4C4F5F574F524C44
test.hex
HFIRST 000000000015
T000000151C0009AC0012080009000003000000000001000001
T0000150B48454C4C4F5F574F524C44
E000000
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quoans.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/quoans.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/quoans.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/quoans.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/quoans.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/quoans.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/quoans.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/quoans.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/quoans.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/quoans.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/quoans.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/quoans.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/quoans.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/quoans.wordpress.com/225/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=225&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://quoans.wordpress.com/2010/06/17/macro-intergrated-assembler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1dd47264df9d720f9ebdca2b1a0beb14?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Renjith S</media:title>
		</media:content>
	</item>
		<item>
		<title>Macro Processor</title>
		<link>http://quoans.wordpress.com/2010/06/17/macro-processor/</link>
		<comments>http://quoans.wordpress.com/2010/06/17/macro-processor/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 19:00:59 +0000</pubDate>
		<dc:creator>Renjith S</dc:creator>
				<category><![CDATA[System Programming]]></category>
		<category><![CDATA[c program for macro processor]]></category>
		<category><![CDATA[macro]]></category>
		<category><![CDATA[macro processor]]></category>
		<category><![CDATA[macro processor for SIC]]></category>
		<category><![CDATA[macro processor in C]]></category>
		<category><![CDATA[macro SIC]]></category>
		<category><![CDATA[SIC macro]]></category>

		<guid isPermaLink="false">http://quoans.wordpress.com/?p=219</guid>
		<description><![CDATA[SIC Macro Processor. Output is included.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=219&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote>
<h3><em> SIC Macro Processor. Output is included.</em></h3>
</blockquote>
<p><span id="more-219"></span></p>
<p><pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

#define TRUE 1
#define FALSE 0

#define lenOPTAB 7

struct{
    char opcode[32];
    char hexcode[3];
}OPTAB[]={
    {&quot;ADD&quot;,&quot;08&quot;},
    {&quot;SUB&quot;,&quot;1C&quot;},
    {&quot;MUL&quot;,&quot;20&quot;},
    {&quot;DIV&quot;,&quot;24&quot;},
    {&quot;LDA&quot;,&quot;AC&quot;},
    {&quot;END&quot;,&quot;00&quot;},
    {&quot;MEND&quot;,&quot;FF&quot;},

};

typedef struct deftab{
    char statement[80];
    struct deftab * next;
} DEFTAB;

typedef struct ntab{
    char name[80];
    DEFTAB * start;
    DEFTAB * end;
    struct ntab * next;
}NAMETAB;

NAMETAB *nameTab;

char expanding = FALSE;

typedef struct{
    char label[32];
    char opcode[32];
    char operand[32];
}Token;

void macroexpand(FILE *srcfp,FILE *outfp);
void processline(Token tok,FILE *srcfp,FILE *outfp);
NAMETAB * searchNAMETAB(char *str);
void define(Token tok,FILE *fp);
void expand(Token tok,FILE *srcfp, FILE *outfp);
void replaceARG(char *line,char ARGTAB[10][80]);
int readline(FILE *fp,char * line);
NAMETAB*  putInNAMETAB(Token tok);
void getline (char *line, char ARGTAB[10][80], DEFTAB *def);
void putPrototypeInDEFTAB(Token tok,DEFTAB *def);
int getparameter(char str[80],char pList[10][80]);
void subParmeter(char *line,int pCount, char pList[10][80]);
void writeline(Token tok,FILE *fp);
void tokenize( char * line, Token *tok);
char isOPCODE( char * str);
void cleanMem();
int main(int argc, char *argv[]){
    FILE *srcfp, *outfp;
    char outfilename[80];
    if(argc != 2){
	  printf(&quot;Invalid Usage !!!\n&quot;);
	  return;
    }
    // generate filename expanded file.
    if( strstr(argv[1],&quot;.&quot;) ){
	  strcpy(outfilename,argv[1]);
	  strcpy(strstr(outfilename,&quot;.&quot;),&quot;_expanded.asm&quot;);
    }else{
	  strcpy(outfilename,argv[1]);
	  strcat(outfilename,&quot;_expanded.asm&quot;);
    }

    srcfp = fopen(argv[1],&quot;r&quot;);
    if(srcfp == NULL){
	  printf(&quot;Unable to read the source file.\n&quot;);
	  return;
    }
    outfp = fopen(outfilename,&quot;w&quot;);
    if(outfp == NULL){
	  printf(&quot;Unable to open outfile.\n&quot;);
	  fclose(srcfp);
	  return;
    }
    nameTab = NULL;
    macroexpand(srcfp,outfp);
    cleanMem();
    fclose(srcfp);
    fclose(outfp);
}

void macroexpand(FILE *srcfp,FILE *outfp){
    char line[80];
    Token tok;

    expanding = FALSE;
    readline(srcfp,line);
    tokenize(line,&amp;tok);

    while( strcasecmp(tok.opcode,&quot;END&quot;) != 0){
	  processline(tok,srcfp,outfp);
	  readline(srcfp,line);
	  tokenize(line,&amp;tok);
    }
    writeline(tok,outfp);
    return;
}

void processline(Token tok,FILE *srcfp,FILE *outfp){
    if(searchNAMETAB(tok.opcode)){
	  expand(tok,srcfp,outfp);
    }else if (strcasecmp(tok.opcode,&quot;MACRO&quot;) == 0){
	  define(tok,srcfp);
    }
    else{
	  writeline(tok,outfp);
    }
}

NAMETAB * searchNAMETAB(char *str){
    NAMETAB *nTab;
    nTab = nameTab;
    while(nTab != NULL){
	  if(strcasecmp(nTab-&gt;name,str)== 0){
		return nTab;
	  }
	  nTab = nTab-&gt;next;
    }
    return NULL;
}

void define(Token tok,FILE *fp){
    int level=1,pCount;
    int i;
    char line[80],line1[80];
    char pList[10][80]; //contain parameters
    NAMETAB *nTab;

    DEFTAB *def,*tmpdef,*start,*end;
    nTab = putInNAMETAB(tok);

    def =(DEFTAB*) malloc(sizeof(DEFTAB));
    def-&gt;next =NULL;
    start = def;

    putPrototypeInDEFTAB(tok,def);

    pCount = getparameter(tok.operand,pList);

    while(level &gt; 0){
	  readline(fp,line);
	  strcpy(line1,line);
	  tokenize(line1,&amp;tok);

	  if(line[0] != '.') {//not a comment line

		subParmeter(line,pCount,pList);

		//enter line to deftab;
		tmpdef =(DEFTAB*) malloc(sizeof(DEFTAB));
		strcpy(tmpdef-&gt;statement,line);
		tmpdef -&gt;next = NULL;
		def-&gt;next = tmpdef;
		def = def-&gt;next;

		if(strcasecmp(tok.opcode,&quot;MACRO&quot;)==0){
		    level = level+1;
		}else if(strcasecmp(tok.opcode,&quot;MEND&quot;)==0){
		    level = level-1;
		}
	  }
    }

    nTab-&gt;start = start;
    nTab-&gt;end   = def;

}

void expand(Token tok,FILE *srcfp, FILE *outfp){
    char ARGTAB[10][80];
    int pCount;
    char line[80];

    NAMETAB *nTab;
    DEFTAB *def;

    expanding = TRUE;
    pCount = getparameter(tok.operand,ARGTAB);
    nTab = searchNAMETAB(tok.opcode);

    def = nTab-&gt;start;
    def = def -&gt; next;
    getline(line,ARGTAB,def);

    tokenize(line,&amp;tok);
    def = def-&gt;next;
    while(strcasecmp(tok.opcode,&quot;MEND&quot;) != 0){
	  processline(tok,srcfp,outfp);
	  getline(line,ARGTAB,def);
	  tokenize(line,&amp;tok);
	  def = def-&gt;next;
    }

    expanding = FALSE;

}

void getline (char *line, char ARGTAB[10][80], DEFTAB *def){
    char str[80];
    Token tok;

    strcpy(str,def-&gt;statement);
    tokenize(str,&amp;tok);
    replaceARG(tok.operand,ARGTAB);
    sprintf(line,&quot;%s %s %s&quot;,tok.label, tok.opcode, tok.operand);
    return;
}

void replaceARG(char *line,char ARGTAB[10][80]){
    char *str, tail[80];
    int count;
    str = strstr(line,&quot;?&quot;);
    while( str ){
	  strcpy(tail,str+2);
	  count = (*(str+1) - 0x30);
	  strcpy(str,ARGTAB[count]);
	  strcat(str,tail);
	  str = strstr(line,&quot;?&quot;);

    }
    return;
}

int readline(FILE *fp,char * line){
    char ch;
    int cnt=0;
    while ( (ch = fgetc(fp)) != EOF &amp;&amp; ch != '\n' ){
        line[cnt++] = ch;
    }
    line[cnt] = '&#092;&#048;';

    if(line [0] == '&#092;&#048;')
        return 0;
    else
        return 1;
}

NAMETAB*  putInNAMETAB(Token tok){
    NAMETAB *ptrNameTab=NULL;
    if(nameTab == NULL){
	  ptrNameTab = (NAMETAB*) malloc(sizeof(NAMETAB));

	  ptrNameTab -&gt; start = NULL;
	  ptrNameTab -&gt; end   = NULL;
	  ptrNameTab -&gt; next  = NULL;
	  nameTab = ptrNameTab;
    }else{
	  ptrNameTab = nameTab;
	  while(ptrNameTab -&gt;next == NULL){
		ptrNameTab = ptrNameTab -&gt; next;
	  }
	  ptrNameTab -&gt; next = (NAMETAB*) malloc(sizeof(NAMETAB));
	  ptrNameTab = ptrNameTab-&gt; next;
	  ptrNameTab -&gt; start = NULL;
	  ptrNameTab -&gt; end   = NULL;
	  ptrNameTab -&gt;next = NULL;

    }

    strcpy(ptrNameTab-&gt;name,tok.label);
    return ptrNameTab;

}
void putPrototypeInDEFTAB(Token tok,DEFTAB *def){
    sprintf(def-&gt;statement,&quot;%s %s %s&quot;,tok.label,tok.opcode,tok.operand);
}

int getparameter(char str[80],char pList[10][80]){
    char *chr;
    int cnt=0;
    chr = strtok(str,&quot;.?, \n\t&quot;);

    while(chr != NULL){
	  strcpy(pList[cnt],chr);
	  cnt++;
	  chr= strtok(NULL,&quot;.?, \n\t&quot;);
    }
    return cnt;
}

void subParmeter(char *line,int pCount, char pList[10][80]){
    int i;
    char *str;
    char *tail;
    for(i=0; i&lt;pCount; i++){
	  str = strstr(line,pList[i]);
	  if(str != NULL){
		tail = str+strlen(pList[i]);
		sprintf(str,&quot;?%d&quot;,i);
		strcat(str,tail);
	  }
    }
}

void writeline(Token tok,FILE *fp){
    fprintf(fp,&quot;%s %s %s\n&quot;,tok.label,tok.opcode,tok.operand);
    return;
}
void tokenize( char * line, Token *tok){
    char *chr;
    strcpy(tok -&gt; label, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; opcode, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; operand, &quot;&#092;&#048;&quot;);

    chr =strtok(line, &quot; \t&quot;);

    if( chr != NULL){
        //check for opcode
	  if ( isOPCODE(chr) || searchNAMETAB(chr) ){
		strcpy(tok -&gt; opcode,chr); // instruction opcode+operand
		chr = strtok(NULL, &quot; \t&quot;);
		if( chr != NULL )
		    strcpy(tok -&gt; operand, chr);
        }else{  // instruction label+opcode+operand
		strcpy(tok-&gt;label,chr);
		chr = strtok(NULL, &quot; \t&quot;);
		if( chr != NULL )
		    strcpy(tok -&gt; opcode,chr);
		chr = strtok(NULL, &quot;\n\t&quot;);
		if( chr != NULL )
		    strcpy(tok -&gt; operand, chr);
        }
    }
    //  printf(&quot; %s | %s | %s \n&quot;, tok-&gt; label, tok-&gt; opcode, tok -&gt; operand);
    return;
}

char isOPCODE( char * str){
    int i;
    for (i=0; i &lt; lenOPTAB; i++){
        if( strcasecmp(OPTAB[i].opcode,str) == 0)
		return 1;
    }
    return 0;
}

void cleanMem(){
    NAMETAB *nTab,*tmp;
    DEFTAB *def,*tmpDef;
    nTab = nameTab;
    while(nTab != NULL){
	  def = nTab-&gt;start;
	  while(def != NULL){
		tmpDef = def;
		def = def-&gt; next;
		free(tmpDef);
	  }
	  tmp = nTab;
	  nTab = nTab-&gt;next;
	  free(tmp);
    }
}

Sample Output;
test.asm    &lt;-- Input

COPY  START 0000
TEST  MACRO &amp;ONE, &amp;TWO
	LDA &amp;ONE
	ADD THREE
MEND
FIRST	SUB   THREE
	TEST  LENGTH,RETADR
EOF   BYTE  C'eof'
THREE WORD  3
ZERO 	WORD 	0
RETADR	RESW	1
LENGTH	RESW	1
MESS1   BYTE  C'HELLO_WORLD'
	  END

test_expanded.asm

COPY START 0000
FIRST SUB   THREE
 LDA LENGTH
 ADD THREE
EOF BYTE  C'eof'
THREE WORD  3
ZERO WORD 0
RETADR RESW 1
LENGTH RESW 1
MESS1 BYTE  C'HELLO_WORLD'
 END

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quoans.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/quoans.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/quoans.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/quoans.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/quoans.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/quoans.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/quoans.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/quoans.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/quoans.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/quoans.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/quoans.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/quoans.wordpress.com/219/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/quoans.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/quoans.wordpress.com/219/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=219&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://quoans.wordpress.com/2010/06/17/macro-processor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1dd47264df9d720f9ebdca2b1a0beb14?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Renjith S</media:title>
		</media:content>
	</item>
		<item>
		<title>Relocating Loader</title>
		<link>http://quoans.wordpress.com/2010/06/15/relocating-loader/</link>
		<comments>http://quoans.wordpress.com/2010/06/15/relocating-loader/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 18:24:42 +0000</pubDate>
		<dc:creator>Renjith S</dc:creator>
				<category><![CDATA[System Programming]]></category>
		<category><![CDATA[c program for relocating loader]]></category>
		<category><![CDATA[loader sic]]></category>
		<category><![CDATA[relocating loader]]></category>
		<category><![CDATA[relocating loader using c]]></category>

		<guid isPermaLink="false">http://quoans.wordpress.com/?p=212</guid>
		<description><![CDATA[Free memory list is hard coded &#38; first fit free block is selected<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=212&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote>
<h3><em> Free memory list is hard coded &amp; first fit free block is selected </em></h3>
</blockquote>
<p><span id="more-212"></span></p>
<p><pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#define MEMSIZE  8192 // 8K

struct record{
    char type;
    int start;
    int length;
    char content[80];
};

unsigned char memory[MEMSIZE];

struct freeMemoryList{
    int startAddr;
    int length;
} freeMemList[] = { {0x500,5},{0x1000,512}};
int lenfreeMemList = 2;

int getline(FILE *fp,char * line);
void header(char *line,char *name, int * start, int *length);
void getRecord(char *line,struct record *rec);
int findFreeMem(int length);
int load(FILE * hexfp);
void writeToMemFile(FILE *memfp);

int main(int argc, char * argv[]){
    FILE *hexfp, *memfp;
    char memfname[30];

    if (argc != 2){
	  printf(&quot;Invalid usage !!!&quot;);
	  return;
    }

    if( strstr(argv[1],&quot;.&quot;) ){
	  strcpy(memfname,argv[1]);
	  strcpy(strstr(memfname,&quot;.&quot;),&quot;.mem&quot;);

    }else{
	  strcpy(memfname,argv[1]);
	  strcat(memfname,&quot;.mem&quot;);

    }

    memfp = fopen(memfname,&quot;w&quot;);
    hexfp = fopen(argv[1],&quot;r&quot;);

    if(load(hexfp))
	  writeToMemFile(memfp);
    fclose(memfp);
    fclose(hexfp);
}

int load(FILE * hexfp){
    char line[80],name[10],strByte[3];
    int start, length;
    int baseAddr;
    int i,t;
    struct record rec;

    getline(hexfp,line);
    header(line,name,&amp;start,&amp;length);

    baseAddr=findFreeMem(length);

    if(baseAddr == -1){
	  printf(&quot;No free memory available.\n&quot;);
	  return 0;
    }

    getline(hexfp,line);
    getRecord(line,&amp;rec);
    do{
	  if(rec.type=='T'){
		for(i=0;i&lt;rec.length;i++){
		    strncpy(strByte,rec.content+(2*i),2);
		    strByte[2]='&#092;&#048;';
		    //address start with logical 0
		    memory[rec.start+i+baseAddr - start] = strtol(strByte,0,16);
		}
	  }else if(rec.type=='M'){
		int k, addr;

		//align to four bytes

		for( k=0;k&lt; rec.length / 4;k++){
		    addr= rec.start+baseAddr+ (2*k);

		    memory[addr] = memory[addr]+ (baseAddr/0x100);

		    memory[addr + 1] = memory[addr + 1]+ (baseAddr%0x100);
		}

	  }
	  getline(hexfp,line);
	  getRecord(line,&amp;rec);
    }while(rec.type != 'E');
    return 1;
}

//find first fit memory block.
int findFreeMem(int length){
    int i;
    for(i=0;i&lt;lenfreeMemList;i++){
	  if(freeMemList[i].length &gt;= length)
		return freeMemList[i].startAddr;
    }
    return -1;

}
void writeToMemFile(FILE *memfp){
    int i;
    fprintf(memfp,&quot;Address \t Contents\n&quot;);
    for(i=0; i&lt;MEMSIZE;i++){
	  if(i%16 == 0){
		fprintf(memfp,&quot;\n%06X\t&quot;,i);
	  }
	  if(i%4 == 0){
		fprintf(memfp,&quot; &quot;);
	  }
	  fprintf(memfp,&quot;%02X&quot;,memory[i]);

    }
    return;
}

void getRecord(char *line,struct record *rec){

    char temp[80];
    rec-&gt;type = line[0];
    if(line[0] == 'T'){
	  strncpy(temp,line+1,6);
	  temp[6]='&#092;&#048;';
	  rec-&gt;start = strtoul(temp,0,16);
	  strncpy(temp,line+7,2);
	  temp[2]='&#092;&#048;';
	  rec-&gt;length = strtoul(temp,0,16);
	  strncpy(rec-&gt;content,line+9, rec-&gt;length*2);
    }else if(line[0] == 'M'){
	  strncpy(temp,line+1,6);
	  temp[6]='&#092;&#048;';
	  rec-&gt;start = strtoul(temp,0,16);
	  strncpy(temp,line+7,2);
	  temp[2]='&#092;&#048;';
	  rec-&gt;length = strtoul(temp,0,16);
    }
    return ;
}

void header(char *line,char *name, int * start, int *length){
    char temp[10];
    if(line[0] == 'H'){
	  strncpy(name,line+1,6);
	  strncpy(temp,line+7,6);
	  *start = strtoul(temp,0,16);
	  strncpy(temp,line+13,6);
	  *length = strtoul(temp,0,16);
    }else{
	  start =0;
	  length =0;
    }
}
int getline(FILE *fp,char * line){
    char ch;
    int cnt=0;
    while ( (ch = fgetc(fp)) != EOF &amp;&amp; ch != '\n' ){
	  line[cnt++] = ch;
    }
    line[cnt] = '&#092;&#048;';

    if(line [0] == '&#092;&#048;')
	  return 0;
    else
	  return 1;
}

Sample Output:
test.hex &lt;--- Input

HFIRST 000000000037
T00000018AC0009AC0012656F66000003000000000001000001000004
T0000211648454C4C4F5F574F524C4468656C6C6F5F776F726C64
M00000104
M00000404
E000000

test.mem

Address          Contents

000000   00000000 00000000 00000000 00000000
000010   00000000 00000000 00000000 00000000
-----------------------------------------------------------------
-----------------------------------------------------------------
000FF0   00000000 00000000 00000000 00000000
001000   AC1009AC 1012656F 66000003 00000000
001010   00010000 01000004 00000000 00000000
001020   0048454C 4C4F5F57 4F524C44 68656C6C
001030   6F5F776F 726C6400 00000000 00000000
-----------------------------------------------------------------
-----------------------------------------------------------------
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quoans.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/quoans.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/quoans.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/quoans.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/quoans.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/quoans.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/quoans.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/quoans.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/quoans.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/quoans.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/quoans.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/quoans.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/quoans.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/quoans.wordpress.com/212/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=212&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://quoans.wordpress.com/2010/06/15/relocating-loader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1dd47264df9d720f9ebdca2b1a0beb14?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Renjith S</media:title>
		</media:content>
	</item>
		<item>
		<title>Assembler with relocating option</title>
		<link>http://quoans.wordpress.com/2010/06/15/assembler-with-relocating-option/</link>
		<comments>http://quoans.wordpress.com/2010/06/15/assembler-with-relocating-option/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 17:24:43 +0000</pubDate>
		<dc:creator>Renjith S</dc:creator>
				<category><![CDATA[System Programming]]></category>
		<category><![CDATA[assembler]]></category>
		<category><![CDATA[reloacting option]]></category>
		<category><![CDATA[SIC assembler]]></category>

		<guid isPermaLink="false">http://quoans.wordpress.com/?p=202</guid>
		<description><![CDATA[Two operand instruction are not considered. Sample output is included in the program.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=202&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote>
<h3><em>Two operand instruction are not considered. Sample output is included in the program.</em></h3>
</blockquote>
<p><span id="more-202"></span></p>
<p><pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;errno.h&gt;

#define lenOPTAB 6

struct{
    char opcode[32];
    char hexcode[3];
}OPTAB[]={
    {&quot;ADD&quot;,&quot;08&quot;},
    {&quot;SUB&quot;,&quot;1C&quot;},
    {&quot;MUL&quot;,&quot;20&quot;},
    {&quot;DIV&quot;,&quot;24&quot;},
    {&quot;MOV&quot;,&quot;AC&quot;},
    {&quot;END&quot;,&quot;00&quot;}
};

struct{
    char label[32];
    int address;
}SYMTAB[100];

struct{
    int address;
    int length;
} modifyRec[100];
int lenmodifyRec=0;
typedef struct{
    char label[32];
    char opcode[32];
    char operand[32];

}Token;

typedef struct{
    int address;
    Token src;
    char hexcode[30];
}asmlist;

int lenSRC = 0;
int lenSYMTAB = 0;
int startAddr =0;
int opAddr=0;

void tokenize( char * line, Token *tok);
int  getline(FILE *fp,char * line);
char isOPCODE( char * str);

int pass1(FILE *srcfp, FILE *intfp);
void pass2(FILE *intrfp, FILE *lstfp, FILE *hexfp);

char searchSYMTAB( char * str);
void insertSYMTAB( char * label,int locCntr);
void tokenizeIntr( char * line, asmlist *list);
void initializeTextRec(char *textRec,asmlist list);

int  getAddress(char *symbol);
char * getHexOperand(char * str, char *hexOperand);
char * getOpcodeHex(char *op);
int operandLen(char *str);

void writeintrfile(asmlist list,FILE *fp);
void writelistfile(FILE *fp, asmlist list);
void writeobjheader(FILE *fp,asmlist list);
void writeObjTextRec(FILE *fp, char *str);
void writeModifyRec(FILE *fp);
void writeEnd(FILE *fp);

int  main(int argc, char * argv[]){
    FILE *srcfp, *lstfp, *intrfp, *hexfp;
    char lstfname[30], intrfname[30],hexfname[30];
    if (argc != 2){
        printf(&quot;Invalid usage !!!&quot;);
        return;
    }

    // generate filename for list,intermediate and hexfiles.
    if( strstr(argv[1],&quot;.&quot;) ){
        strcpy(lstfname,argv[1]);
        strcpy(strstr(lstfname,&quot;.&quot;),&quot;.lst&quot;);
        strcpy(intrfname,argv[1]);
        strcpy(strstr(intrfname,&quot;.&quot;),&quot;.int&quot;);
        strcpy(hexfname,argv[1]);
        strcpy(strstr(hexfname,&quot;.&quot;),&quot;.hex&quot;);
    }else{
        strcpy(lstfname,argv[1]);
        strcat(lstfname,&quot;.lst&quot;);
        strcpy(intrfname,argv[1]);
        strcat(intrfname,&quot;.int&quot;);
        strcpy(hexfname,argv[1]);
        strcat(hexfname,&quot;.hex&quot;);
    }

    srcfp=fopen(argv[1],&quot;r&quot;);
    if(srcfp == NULL){
        printf(&quot;!!! Error. Unable to open the src file. &quot;);
        return;
    }

    intrfp=fopen(intrfname,&quot;w&quot;);
    if(intrfp == NULL){
        printf(&quot;!!! Error. Unable to open the intermediate file. &quot;);
        fclose(srcfp);
        return;
    }

    lstfp=fopen(lstfname,&quot;w&quot;);
    if(lstfp == NULL){
        printf(&quot;!!! Error. Unable to open the list file. &quot;);
        fclose(srcfp);
        fclose(intrfp);
        return;
    }

    hexfp=fopen(hexfname,&quot;w&quot;);
    if(hexfp == NULL){
        printf(&quot;!!! Error. Unable to open the hex file. &quot;);
        fclose(srcfp);
        fclose(intrfp);
        fclose(lstfp);
        return;
    }

    // pass 1 generate intermediate file
    if(! pass1(srcfp,intrfp)){
        fclose(srcfp);
        fclose(intrfp);
        return;
    }

    fclose(srcfp);
    fclose(intrfp);

    intrfp=fopen(intrfname,&quot;r&quot;);
    if(intrfp == NULL){
        printf(&quot;!!! Error. Unable to open the intermediate file. &quot;);
        fclose(srcfp);
        return;
    }

    //generate list file and hexfile
    pass2(intrfp,lstfp,hexfp);

    fclose(lstfp);
    fclose(hexfp);
}

int pass1(FILE *srcfp, FILE *intrfp){
    char line[80];
    char *cptr;

    Token tok;
    int locCntr;
    asmlist list;
    if(! getline(srcfp,line)) return;  // read first line

    tokenize(line,&amp;tok);  // tokenize the line

    if( strcasecmp(tok.opcode,&quot;START&quot;) == 0){
        locCntr = strtoul(tok.operand,0,16);
        list.address = locCntr;
        list.src = tok;
        writeintrfile(list,intrfp);
        if(!getline(srcfp,line)) return;
        tokenize(line,&amp;tok);

    }else{
        locCntr =0;
    }

    startAddr=locCntr;
    lenSRC = locCntr;

    while( strcasecmp(tok.opcode,&quot;END&quot;) != 0){

        // check for comments. comments start with a '.'

        if( line[0] != '.'){

		list.address = locCntr;
		list.src = tok;
		writeintrfile(list,intrfp); // writes intermediate file.

		// check for label
		if(strlen(tok.label) != 0){
		    if(searchSYMTAB(tok.label)){
			  printf(&quot;Error : duplicate symbol @ %s&quot;,line);
			  return 0;
		    }else{
			  //insert symboltable entry
			  insertSYMTAB(tok.label,locCntr);
		    }
		}

		//Finds instruction length
		if( isOPCODE(tok.opcode)){
		    locCntr = locCntr +3;
		}
		else if( strcasecmp(tok.opcode,&quot;WORD&quot;) == 0){
		    locCntr = locCntr +3;
		}
		else if( strcasecmp(tok.opcode,&quot;RESW&quot;) == 0){
		    // operand length is specified in the instruction itself
		    if(tok.operand[0] == 'X'){
			  cptr=strtok(tok.operand,&quot; '&quot;);
			  if(cptr != NULL)
				locCntr = locCntr + 3* strtoul(cptr,0,16);
		    }
		    else{
			  locCntr = locCntr + 3* strtoul(tok.operand,0,10);
		    }
		}
		else if( strcasecmp(tok.opcode,&quot;RESB&quot;) == 0){
		    // operand length is in instruction itself
		    if(tok.operand[0] == 'X'){
			  cptr=strtok(tok.operand,&quot; '&quot;);
			  if(cptr != NULL)
				locCntr = locCntr + strtoul(cptr,0,16);
		    }
		    else{
			  locCntr = locCntr +  strtoul(tok.operand,0,10);
		    }
		}
		else if( strcasecmp(tok.opcode,&quot;BYTE&quot;) == 0){
		    //length of the operand
		    locCntr = locCntr + operandLen(tok.operand);
		}
		else{
		    printf(&quot;Error Invalid operation code. @ %s &quot;,line);
		    return 0;
		}
        }
        if(!getline(srcfp,line)) return 0;
        tokenize(line,&amp;tok);
    }

    list.address = locCntr;
    list.src = tok;
    writeintrfile(list,intrfp);

    lenSRC = locCntr - lenSRC;

    return 1;
}

void pass2(FILE *intrfp, FILE *lstfp, FILE *hexfp){
    char line[80];
    asmlist list;
    Token *tok;
    tok = &amp;(list.src);

    char textRec[80];
    int operandAddr=0;
    char hexOperand[80];
    char hexObj[80],temp[80];
    int Index=9;

    if(!getline(intrfp,line)) return;
    tokenizeIntr(line,&amp;list);//tokenize the intermediate code

    if (strcasecmp(tok-&gt;opcode,&quot;START&quot;) == 0 ){
        writelistfile(lstfp,list);

        if(!getline(intrfp,line)) return;
        tokenizeIntr(line,&amp;list);
    }
    writeobjheader(hexfp,list);  //H+Pgm_Name+Start_Addr+Length
    initializeTextRec(textRec,list); //  T+Starting address

    while(strcasecmp(tok-&gt;opcode,&quot;END&quot;) != 0){

	  // Valid Opcode
        if(isOPCODE(tok-&gt;opcode)){

		if(strlen(tok -&gt; operand) != 0){
		    if(searchSYMTAB(tok-&gt;operand)){
			  modifyRec[lenmodifyRec].address=list.address+1;
			  modifyRec[lenmodifyRec].length=4;
			  lenmodifyRec++;

			  // get symbol address
			  operandAddr=getAddress(tok-&gt;operand);
			  //opcode+label address
			  sprintf(hexObj,&quot;%s%04X&quot;,getOpcodeHex(tok -&gt; opcode), operandAddr);
		    }
		    else{
			  opAddr=list.address+1;
			  if( getHexOperand(tok-&gt;operand,hexOperand) == NULL ){
				printf(&quot;Invalid operand @ %s&quot;,line);
			  }
			  //opcode+operand value
			  sprintf(hexObj,&quot;%s%s&quot;,getOpcodeHex(tok -&gt; opcode),hexOperand);
		    }
		}
        }
	  else{
		opAddr=list.address;
		if(strcasecmp(tok -&gt; opcode,&quot;BYTE&quot;) == 0 ||	\
		   strcasecmp(tok -&gt; opcode,&quot;WORD&quot;) == 0){

		    getHexOperand(tok-&gt;operand,hexOperand);
		    //operand
		    sprintf(hexObj,&quot;%s&quot;,hexOperand);
		}
		else{
		    getHexOperand(tok-&gt;operand,hexOperand);
		    //operand
		    sprintf(hexObj,&quot;%s&quot;,hexOperand);

		}
        }

        if((Index+strlen(hexObj)) &gt; 69){
		sprintf(textRec+7,&quot;%02X%s&quot;,(strlen(textRec)-9)/2,textRec+9);//place length in lenField
		writeObjTextRec(hexfp,textRec);
		initializeTextRec(textRec,list);
        }

        strncpy(temp,textRec+1,6);
        Index = ((list.address - strtoul(temp,0,16))*2 )+9;

        sprintf(textRec+Index,&quot;%s&quot;,hexObj);
        Index =Index + strlen(hexObj);
        strcpy(list.hexcode,hexObj);

        writelistfile(lstfp,list);

        if(!getline(intrfp,line)) return;
        tokenizeIntr(line,&amp;list);

    }
    sprintf(textRec+7,&quot;%02X%s&quot;,(strlen(textRec)-9)/2,textRec+9);
    writeObjTextRec(hexfp,textRec);
    writeModifyRec(hexfp);
    writeEnd(hexfp);
}

void insertSYMTAB( char * label,int locCntr){
    strcpy(SYMTAB[lenSYMTAB].label,label);
    SYMTAB[lenSYMTAB].address = locCntr;
    lenSYMTAB ++;
    return;
}

int getline(FILE *fp,char * line){
    char ch;
    int cnt=0;
    while ( (ch = fgetc(fp)) != EOF &amp;&amp; ch != '\n' ){
        line[cnt++] = ch;
    }
    line[cnt] = '&#092;&#048;';

    if(line [0] == '&#092;&#048;')
        return 0;
    else
        return 1;
}

void tokenize( char * line, Token *tok){
    char *chr;
    strcpy(tok -&gt; label, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; opcode, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; operand, &quot;&#092;&#048;&quot;);

    chr =strtok(line, &quot; \t&quot;);

    if( chr != NULL){
        //check for opcode
        if ( isOPCODE(chr)){
		strcpy(tok -&gt; opcode,chr); // instruction opcode+operand
		chr = strtok(NULL, &quot; \t&quot;);
		if( chr != NULL )
		    strcpy(tok -&gt; operand, chr);
        }else{  // instruction label+opcode+operand
		strcpy(tok-&gt;label,chr);
		chr = strtok(NULL, &quot; \t&quot;);
		if( chr != NULL )
		    strcpy(tok -&gt; opcode,chr);
		chr = strtok(NULL, &quot; \t&quot;);
		if( chr != NULL )
		    strcpy(tok -&gt; operand, chr);
        }
    }
    //printf(&quot; %s | %s | %s \n&quot;, tok-&gt; label, tok-&gt; opcode, tok -&gt; operand);
    return;
}

void tokenizeIntr( char * line, asmlist *list){
    char *chr;
    Token *tok;
    tok = &amp; (list -&gt; src);
    list -&gt; address = 0;
    strcpy(list-&gt;hexcode,&quot;&quot;);
    strcpy(tok -&gt; label, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; opcode, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; operand, &quot;&#092;&#048;&quot;);

    chr =strtok(line, &quot; \t&quot;);

    if( chr != NULL){
        list -&gt; address = strtoul(chr,0,16);
        chr = strtok(NULL, &quot; \t&quot;);

        if ( isOPCODE(chr)){
		strcpy(tok -&gt; opcode,chr);
		chr = strtok(NULL, &quot; \t&quot;);
		if( chr != NULL )
		    strcpy(tok -&gt; operand, chr);
        }else{
		strcpy(tok-&gt;label,chr);
		chr = strtok(NULL, &quot; \t&quot;);
		if( chr != NULL )
		    strcpy(tok -&gt; opcode,chr);
		chr = strtok(NULL, &quot; \t&quot;);
		if( chr != NULL )
		    strcpy(tok -&gt; operand, chr);
        }
    }
    //printf(&quot; %s | %s | %s \n&quot;, tok-&gt; label, tok-&gt; opcode, tok -&gt; operand);
    return;
}

char isOPCODE( char * str){
    int i;
    for (i=0; i &lt; lenOPTAB; i++){
        if( strcasecmp(OPTAB[i].opcode,str) == 0)
		return 1;
    }
    return 0;
}

char searchSYMTAB( char * str){
    int i;
    for (i=0; i &lt; lenSYMTAB; i++){
        if( strcasecmp(SYMTAB[i].label,str) == 0)
		return 1;
    }
    return 0;
}
void writeintrfile(asmlist list,FILE *fp){
    fprintf(fp,&quot;%X\t%s\t%s\t%s\n&quot;,list.address,list.src.label,list.src.opcode,list.src.operand);
    return;
}
void writelistfile(FILE *fp, asmlist list){
    fprintf(fp,&quot;%06X\t%s\t%s\t%s\t%s\n&quot;,list.address,list.src.label,list.src.opcode,list.src.operand,list.hexcode);
}

void writeobjheader(FILE *fp,asmlist list){
    char line[80];
    int i;

    line [0]='H';
    strncpy(line+1,list.src.label,6);
    sprintf(line+7,&quot;%06X&quot;,list.address);
    sprintf(line+13,&quot;%06X&quot;,lenSRC);
    line[19]='&#092;&#048;';

    for( i=0; i&lt; 19; i++){
        if(line[i] =='&#092;&#048;')
		line[i]=' ';
    }
    fprintf(fp,&quot;%s\n&quot;,line);

}

void writeEnd(FILE *fp){

    fprintf(fp,&quot;E%06X&quot;,startAddr);

}
void initializeTextRec( char *textRec,asmlist list){
    int i;
    for(i=0;i&lt;80;i++){
        textRec[i]='0';
    }
    textRec[0]='T';
    sprintf(textRec+1,&quot;%06X&quot;,list.address);
    sprintf(textRec+7,&quot;%02X&quot;,00);
    return;
}

int getAddress(char *symbol){
    int i;
    for(i=0; i&lt; lenSYMTAB; i++){
        if(strcasecmp(SYMTAB[i].label,symbol) == 0)
		return SYMTAB[i].address;
    }
    return 0;
}
char * getOpcodeHex(char *op){
    int i;
    for(i=0; i&lt; lenOPTAB; i++){
        if(strcasecmp(OPTAB[i].opcode,op) == 0)
		return OPTAB[i].hexcode;
    }
    return &quot;00&quot;;
}

int operandLen(char *str){
    char *cptr;
    //Hex Operand
    if(str[0] == 'X' || str[0]=='C'){
        cptr = strtok(str+1,&quot; '&quot;);
        if(cptr != NULL){
		return strlen(cptr);
        }
        else{
		return 0;
        }
    }
    return 0;
}

char * getHexOperand(char * strOperand, char *hexOperand){
    int i;
    char *cptr;
    char str[80];
    strcpy(str,strOperand);
    //Hex Operand
    if(str[0] == 'X'){
        cptr = strtok(str+1,&quot; '&quot;);
        if(cptr != NULL){
		sprintf(hexOperand,&quot;%04X&quot;,strtoul(cptr,0,16));
        }
        else{
		return NULL;
        }
    }
    //Charector operand
    else if(str[0] == 'C'){

        cptr = strtok(str+1,&quot; '&quot;);
        if(cptr != NULL){

		for( i=0; cptr[i] != '&#092;&#048;'; i++){
		    sprintf(hexOperand+(2*i),&quot;%02X&quot;,cptr[i]);
		}
		hexOperand[2*i]='&#092;&#048;';

        }
        else{
		return NULL;
        }
    }
    //absolute value.
    else if(str[0] == '#'){
	  if( searchSYMTAB(str+1)){
		sprintf(hexOperand,&quot;%04X&quot;,getAddress(str+1));
		modifyRec[lenmodifyRec].address=opAddr;
		modifyRec[lenmodifyRec].length=04;
		lenmodifyRec++;
        }else{
		errno =0;
		sprintf(hexOperand,&quot;%04d&quot;,strtoul(str,0,10));

		if(errno == EINVAL){
		    return NULL;
		}
        }
    }
    else{

        sprintf(hexOperand,&quot;%06X&quot;,strtoul(str,0,16));

    }
    return hexOperand;

}

void writeObjTextRec(FILE *fp, char *str){
    fprintf(fp,&quot;%s\n&quot;,str);
}

void writeModifyRec(FILE *fp){
    int i;

    for (i=0; i&lt; lenmodifyRec; i++){
	  fprintf(fp,&quot;M%06X%02X\n&quot;,modifyRec[i].address,modifyRec[i].length);
    }
}
Sample Output:
test.asm   &lt;--- Input

COPY    START 0000
FIRST    MOV   THREE
MOV #LENGTH
EOF       BYTE  C'eof'
THREE   WORD  3
ZERO 	WORD 	0
RETADR	RESW	1
LENGTH	RESW	1
BUFFER	RESW	4
MESS1      BYTE  C'HELLO_WORLD'
MESS2      BYTE  C'hello_world'
END

test.lst
000000	COPY	START	0000
000000	FIRST	MOV	THREE	AC0009
000003		MOV	#LENGTH	AC0012
000006	EOF	BYTE	C'eof'	656F66
000009	THREE	WORD	3	000003
00000C	ZERO	WORD	0	000000
00000F	RETADR	RESW	1	000001
000012	LENGTH	RESW	1	000001
000015	BUFFER	RESW	4	000004
000021	MESS1	BYTE	C'HELLO_WORLD'	48454C4C4F5F574F524C44
00002C	MESS2	BYTE	C'hello_world'	68656C6C6F5F776F726C64

test.hex
HFIRST 000000000037
T00000018AC0009AC0012656F66000003000000000001000001000004
T0000211648454C4C4F5F574F524C4468656C6C6F5F776F726C64
M00000104
M00000404
E000000
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quoans.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/quoans.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/quoans.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/quoans.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/quoans.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/quoans.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/quoans.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/quoans.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/quoans.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/quoans.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/quoans.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/quoans.wordpress.com/202/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/quoans.wordpress.com/202/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/quoans.wordpress.com/202/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=202&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://quoans.wordpress.com/2010/06/15/assembler-with-relocating-option/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1dd47264df9d720f9ebdca2b1a0beb14?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Renjith S</media:title>
		</media:content>
	</item>
		<item>
		<title>Absolute loader</title>
		<link>http://quoans.wordpress.com/2010/06/15/absolute-loader/</link>
		<comments>http://quoans.wordpress.com/2010/06/15/absolute-loader/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 11:24:27 +0000</pubDate>
		<dc:creator>Renjith S</dc:creator>
				<category><![CDATA[System Programming]]></category>
		<category><![CDATA[Absolute loader]]></category>
		<category><![CDATA[absolute loader in C]]></category>
		<category><![CDATA[SIC Absolute loader]]></category>

		<guid isPermaLink="false">http://quoans.wordpress.com/?p=195</guid>
		<description><![CDATA[Sample output is included in the program. Program name and size validation are not done.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=195&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote>
<h3><em> Sample output is included in the program. Program name and size validation are not done.</em></h3>
</blockquote>
<p><span id="more-195"></span></p>
<p><pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#define MEMSIZE  8192 // 8K
struct record{
    char type;
    int start;
    int length;
    char content[80];
};

unsigned char memory[MEMSIZE];

int getline(FILE *fp,char * line);
void header(char *line,char *name, int * start, int *length);
void getRecord(char *line,struct record *rec);
void writeToMemFile(FILE *memfp);
int load(FILE * hexfp);

int main(int argc, char * argv[]){
    FILE *hexfp, *memfp;
    char memfname[30];

    if (argc != 2){
	printf(&quot;Invalid usage !!!&quot;);
	return;
    }

    if( strstr(argv[1],&quot;.&quot;) ){
	strcpy(memfname,argv[1]);
	strcpy(strstr(memfname,&quot;.&quot;),&quot;.mem&quot;);

    }else{
	strcpy(memfname,argv[1]);
	strcat(memfname,&quot;.mem&quot;);

    }

    memfp = fopen(memfname,&quot;w&quot;);
    hexfp = fopen(argv[1],&quot;r&quot;);
    if(load(hexfp))
	writeToMemFile(memfp);
    fclose(memfp);
    fclose(hexfp);
}

int load(FILE * hexfp){
    char line[80],name[10],strByte[3];
    int start, length;
    int i,t;
    struct record rec;

    getline(hexfp,line);
    header(line,name,&amp;start,&amp;length);

    getline(hexfp,line);
    getRecord(line,&amp;rec);
    do{
	for(i=0;i&lt;rec.length;i++){
	    strncpy(strByte,rec.content+(2*i),2);
	    strByte[2]='&#092;&#048;';
	    memory[rec.start+i] = strtol(strByte,0,16);
	}

	getline(hexfp,line);
	getRecord(line,&amp;rec);
    }while(rec.type != 'E');
    return 1;
}

void writeToMemFile(FILE *memfp){
    int i;
    fprintf(memfp,&quot;Address \t Contents\n&quot;);
    for(i=0; i&lt;MEMSIZE;i++){
	if(i%16 == 0){
	    fprintf(memfp,&quot;\n%06X\t&quot;,i);
	}
	if(i%4 == 0){
	    fprintf(memfp,&quot; &quot;);
	}
	fprintf(memfp,&quot;%02X&quot;,memory[i]);

    }
    return;
}

void getRecord(char *line,struct record *rec){

    char temp[80];
    rec-&gt;type = line[0];
    if(line[0] == 'T'){
	strncpy(temp,line+1,6);
	temp[6]='&#092;&#048;';
	rec-&gt;start = strtoul(temp,0,16);
	strncpy(temp,line+7,2);
	temp[2]='&#092;&#048;';
	rec-&gt;length = strtoul(temp,0,16);
	strncpy(rec-&gt;content,line+9, rec-&gt;length*2);
    }
    return ;
}

void header(char *line,char *name, int * start, int *length){
    char temp[10];
    if(line[0] == 'H'){
	strncpy(name,line+1,6);
	strncpy(temp,line+7,6);
	*start = strtoul(temp,0,16);
	strncpy(temp,line+13,6);
	*length = strtoul(temp,0,16);
    }else{
	start =0;
	length =0;
    }
}
int getline(FILE *fp,char * line){
    char ch;
    int cnt=0;
    while ( (ch = fgetc(fp)) != EOF &amp;&amp; ch != '\n' ){
	line[cnt++] = ch;
    }
    line[cnt] = '&#092;&#048;';

    if(line [0] == '&#092;&#048;')
	return 0;
    else
	return 1;
}

Sample Output:

test.hex  &lt;-- Input
HFIRST 000000000037
T00000018AC0009AC0012656F66000003000000000001000001000004
T0000211648454C4C4F5F574F524C4468656C6C6F5F776F726C64
E000000

test.mem

Address 	 Contents

000000	 AC0009AC 0012656F 66000003 00000000
000010	 00010000 01000004 00000000 00000000
000020	 0048454C 4C4F5F57 4F524C44 68656C6C
000030	 6F5F776F 726C6400 00000000 00000000
000040	 00000000 00000000 00000000 00000000
000050	 00000000 00000000 00000000 00000000
000060	 00000000 00000000 00000000 00000000
000070	 00000000 00000000 00000000 00000000

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quoans.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/quoans.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/quoans.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/quoans.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/quoans.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/quoans.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/quoans.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/quoans.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/quoans.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/quoans.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/quoans.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/quoans.wordpress.com/195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/quoans.wordpress.com/195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/quoans.wordpress.com/195/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=195&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://quoans.wordpress.com/2010/06/15/absolute-loader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1dd47264df9d720f9ebdca2b1a0beb14?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Renjith S</media:title>
		</media:content>
	</item>
		<item>
		<title>One pass Assembler</title>
		<link>http://quoans.wordpress.com/2010/06/15/one-pass-assembler/</link>
		<comments>http://quoans.wordpress.com/2010/06/15/one-pass-assembler/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 11:19:31 +0000</pubDate>
		<dc:creator>Renjith S</dc:creator>
				<category><![CDATA[System Programming]]></category>
		<category><![CDATA[1 pass assember]]></category>
		<category><![CDATA[assember]]></category>
		<category><![CDATA[assember in C]]></category>
		<category><![CDATA[SIC one pass assemble]]></category>

		<guid isPermaLink="false">http://quoans.wordpress.com/?p=193</guid>
		<description><![CDATA[Two operand instruction are not considered. Sample output is included in the program.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=193&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote>
<h3><em>Two operand instruction are not considered. Sample output is included in the program.</em></h3>
</blockquote>
<p><span id="more-193"></span></p>
<p><pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;errno.h&gt;
#include &lt;stdlib.h&gt;

#define lenOPTAB 6

struct{
    char opcode[32];
    char hexcode[3];
}OPTAB[]={
    {&quot;ADD&quot;,&quot;08&quot;},
    {&quot;SUB&quot;,&quot;1C&quot;},
    {&quot;MUL&quot;,&quot;20&quot;},
    {&quot;DIV&quot;,&quot;24&quot;},
    {&quot;MOV&quot;,&quot;AC&quot;},
    {&quot;END&quot;,&quot;00&quot;}
};

struct{
    char label[32];
    int address;
    struct addrList * list;
}SYMTAB[100];

struct addrList{
    int address;
    struct addrList * next;
};

typedef struct{
    char label[32];
    char opcode[32];
    char operand[32];

}Token;

typedef struct{
    int address;
    Token src;
    char hexcode[30];
}asmlist;

int lenSRC = 0;
int lenSYMTAB = 0;
int startAddr =0;
int locCntr=0;
void tokenize( char * line, Token *tok);
int  getline(FILE *fp,char * line);
char isOPCODE( char * str);

int assembly(FILE *srcfp, FILE *lstfp,FILE *hexfp);

char searchSYMTAB( char * str);
void insertSYMTAB( char * label,int locCntr);
void initializeTextRec(char *textRec,asmlist list);
void insertSYMTABList(char *operand, int locCntr);
void insertSYMTABaddr(char *label, int address);

int  getAddress(char *symbol);
char * getHexOperand(char * str, char *hexOperand);
char * getOpcodeHex(char *op);
int operandLen(char *str);

void writeintrfile(asmlist list,FILE *fp);
void writelistfile(FILE *fp, asmlist list);
void writeobjheader(FILE *fp,asmlist list);
void writeObjTextRec(FILE *fp, char *str);
void writeUndefinedSym(FILE *hexfp);
void writeEnd(FILE *fp);

int  main(int argc, char * argv[]){
    FILE *srcfp, *lstfp, *intrfp, *hexfp;
    char lstfname[30], intrfname[30],hexfname[30];
    if (argc != 2){
	printf(&quot;Invalid usage !!!&quot;);
	return;
    }

    // generate filename for list,intermediate and hexfiles.
    if( strstr(argv[1],&quot;.&quot;) ){
	strcpy(lstfname,argv[1]);
	strcpy(strstr(lstfname,&quot;.&quot;),&quot;.lst&quot;);
	strcpy(hexfname,argv[1]);
	strcpy(strstr(hexfname,&quot;.&quot;),&quot;.hex&quot;);
    }else{
	strcpy(lstfname,argv[1]);
	strcat(lstfname,&quot;.lst&quot;);
	strcpy(hexfname,argv[1]);
	strcat(hexfname,&quot;.hex&quot;);
    }

    srcfp=fopen(argv[1],&quot;r&quot;);
    if(srcfp == NULL){
	printf(&quot;!!! Error. Unable to open the src file. &quot;);
	return;
    }

    lstfp=fopen(lstfname,&quot;w&quot;);
    if(lstfp == NULL){
	printf(&quot;!!! Error. Unable to open the list file. &quot;);
	fclose(srcfp);
	return;
    }

    hexfp=fopen(hexfname,&quot;w&quot;);
    if(hexfp == NULL){
	printf(&quot;!!! Error. Unable to open the hex file. &quot;);
	fclose(srcfp);
	fclose(lstfp);
	return;
    }

    assembly(srcfp,lstfp,hexfp);

    fclose(srcfp);
    fclose(lstfp);
    fclose(hexfp);
}

int assembly(FILE *srcfp, FILE *lstfp, FILE *hexfp){
    char line[80];
    char *cptr;

    Token tok;

    asmlist list;
    char value;
    int operandAddr=0;

    char textRec[80],hexObj[80],temp[80],hexOperand[80];
    int Index=9;

    if(! getline(srcfp,line)) return;  // read first line

    tokenize(line,&amp;tok);  // tokenize the line

    if( strcasecmp(tok.opcode,&quot;START&quot;) == 0){
	locCntr = strtoul(tok.operand,0,16);
	list.address = locCntr;
	list.src = tok;
	list.hexcode[0]='&#092;&#048;';
	writelistfile(lstfp,list);
	if(!getline(srcfp,line)) return;
	tokenize(line,&amp;tok);

    }else{
	locCntr =0;
    }

    startAddr=locCntr;
    lenSRC = locCntr;

    list.address = locCntr;
    writeobjheader(hexfp,list);  //H+Pgm_Name+Start_Addr+Length
    initializeTextRec(textRec,list); //  T+Starting address

    while( strcasecmp(tok.opcode,&quot;END&quot;) != 0){
	// check for comments. comments start with a '.'
	if( line[0] != '.'){
	    list.address = locCntr;
	    list.src = tok;

	    // check for label
	    if(strlen(tok.label) != 0){
		if(value = searchSYMTAB(tok.label)){
		    if(value == 2){
			insertSYMTABaddr(tok.label,locCntr);
		    }else{
			printf(&quot;Error : duplicate symbol @ %s&quot;,line);
			return 0;
		    }
		}else{
		    //insert symboltable entry
		    insertSYMTAB(tok.label,locCntr);
		}
	    }

	    if( isOPCODE(tok.opcode)){
		if(strlen(tok.operand) != 0){
		    if(searchSYMTAB(tok.operand) == 1){
			// get symbol address
			operandAddr=getAddress(tok.operand);
			//opcode+label address
			sprintf(hexObj,&quot;%s%04X&quot;,getOpcodeHex(tok.opcode), operandAddr);
		    }
		    else{
			if( tok.operand[0]!='C'&amp;&amp;tok.operand[0] != 'X'&amp;&amp;tok.operand[0] != '#'&amp;&amp; \
			    ( tok.operand[0] &lt;='0' || tok.operand[0] &gt;= '9') ){
			    insertSYMTABList(tok.operand,locCntr+1);
			    sprintf(hexObj,&quot;%s%04X&quot;,getOpcodeHex(tok.opcode), 0);
			}else{
			    getHexOperand(tok.operand,hexOperand) ;
			    //opcode+operand value
			    sprintf(hexObj,&quot;%s%s&quot;,getOpcodeHex(tok.opcode),hexOperand);
			}
		    }
		}
		locCntr = locCntr +3;
	    }
	    else if( strcasecmp(tok.opcode,&quot;WORD&quot;) == 0){
		getHexOperand(tok.operand,hexOperand);
		sprintf(hexObj,&quot;%s&quot;,hexOperand);
		locCntr = locCntr +3;
	    }
	    else if( strcasecmp(tok.opcode,&quot;RESW&quot;) == 0){
		getHexOperand(tok.operand,hexOperand);
		sprintf(hexObj,&quot;%s&quot;,hexOperand);

		// operand length is specified in the instruction itself
		if(tok.operand[0] == 'X'){
		    cptr=strtok(tok.operand,&quot; '&quot;);
		    if(cptr != NULL)
			locCntr = locCntr + 3* strtoul(cptr,0,16);
		}
		else{
		    locCntr = locCntr + 3* strtoul(tok.operand,0,10);
		}
	    }
	    else if( strcasecmp(tok.opcode,&quot;RESB&quot;) == 0){
		hexObj[0]='&#092;&#048;';
		// operand length is in instruction itself
		if(tok.operand[0] == 'X'){
		    cptr=strtok(tok.operand,&quot; '&quot;);
		    if(cptr != NULL)
			locCntr = locCntr + strtoul(cptr,0,16);
		}
		else{
		    locCntr = locCntr +  strtoul(tok.operand,0,10);
		}
	    }
	    else if( strcasecmp(tok.opcode,&quot;BYTE&quot;) == 0){
		//length of the operand
		getHexOperand(tok.operand,hexOperand);
		sprintf(hexObj,&quot;%s&quot;,hexOperand);
		locCntr = locCntr + operandLen(tok.operand);
	    }
	    else{
		printf(&quot;Error Invalid operation code. @ %s &quot;,line);
		return 0;
	    }

	}

	if((Index+strlen(hexObj)) &gt; 69){
	    sprintf(textRec+7,&quot;%02X%s&quot;,(strlen(textRec)-9)/2,textRec+9);//place length in lenField
	    writeObjTextRec(hexfp,textRec);
	    initializeTextRec(textRec,list);
	}

	strncpy(temp,textRec+1,6);
	Index = ((list.address - strtoul(temp,0,16))*2 )+9;

	sprintf(textRec+Index,&quot;%s&quot;,hexObj);
	Index =Index + strlen(hexObj);
	strcpy(list.hexcode,hexObj);
	writelistfile(lstfp,list);
	if(!getline(srcfp,line)) return 0;
	tokenize(line,&amp;tok);
    }

    sprintf(textRec+7,&quot;%02X%s&quot;,(strlen(textRec)-9)/2,textRec+9);
    writeObjTextRec(hexfp,textRec);
    writeUndefinedSym(hexfp);
    writeEnd(hexfp);
    return 1;
}

void writeUndefinedSym(FILE *hexfp){
    int i;
    asmlist list;
    char textRec[80];
    struct addrList *alist, *nextList;

    for (i=0; i &lt; lenSYMTAB; i++){
	//printf(&quot;%d\n&quot;,SYMTAB[i].list);
	if(SYMTAB[i].list != NULL){
	    alist = SYMTAB[i].list;
	    do{
		list.address = alist-&gt;address;
		initializeTextRec(textRec,list);
		sprintf(textRec+7,&quot;02%04X&quot;,SYMTAB[i].address);
		writeObjTextRec(hexfp,textRec);
		alist = alist-&gt;next;
	    }while(alist != NULL);

	}

    }
}
void insertSYMTABList(char *operand, int locCntr){
    int i;
    struct addrList *list, *nextList;
    list = (struct addrList *) malloc(sizeof(struct addrList));
    list-&gt;next = NULL;
    list-&gt;address =locCntr;

    for (i=0;i&lt;lenSYMTAB;i++){
	if(strcasecmp(SYMTAB[i].label,operand) == 0){
	    if(SYMTAB[i].list == NULL){
		SYMTAB[i].list = list;
	    }else{
		nextList = SYMTAB[i].list;
		while( nextList -&gt;next !=NULL){
		    nextList = nextList-&gt;next;
		}
		nextList-&gt;next =list;
	    }
	    break;
	}
    }
    if( i == lenSYMTAB){
	strcpy(SYMTAB[lenSYMTAB].label,operand);
	SYMTAB[lenSYMTAB].address = 0;
	SYMTAB[lenSYMTAB].list = list;
	lenSYMTAB ++;

    }
}
void insertSYMTABaddr(char *label, int address){
    int i;
    for (i=0; i &lt; lenSYMTAB; i++){
	if( strcasecmp(SYMTAB[i].label,label) == 0)
	    SYMTAB[i].address = address;
    }
    return ;
}

void insertSYMTAB( char * label,int locCntr){
    strcpy(SYMTAB[lenSYMTAB].label,label);
    SYMTAB[lenSYMTAB].address = locCntr;
    SYMTAB[lenSYMTAB].list = NULL;
    lenSYMTAB ++;
    return;
}

int getline(FILE *fp,char * line){
    char ch;
    int cnt=0;
    while ( (ch = fgetc(fp)) != EOF &amp;&amp; ch != '\n' ){
	line[cnt++] = ch;
    }
    line[cnt] = '&#092;&#048;';

    if(line [0] == '&#092;&#048;')
	return 0;
    else
	return 1;
}

void tokenize( char * line, Token *tok){
    char *chr;
    strcpy(tok -&gt; label, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; opcode, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; operand, &quot;&#092;&#048;&quot;);

    chr =strtok(line, &quot; \t&quot;);

    if( chr != NULL){
	//check for opcode
	if ( isOPCODE(chr)){
	    strcpy(tok -&gt; opcode,chr); // instruction opcode+operand
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; operand, chr);
	}else{  // instruction label+opcode+operand
	    strcpy(tok-&gt;label,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; opcode,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; operand, chr);
	}
    }
    //printf(&quot; %s | %s | %s \n&quot;, tok-&gt; label, tok-&gt; opcode, tok -&gt; operand);
    return;
}

char isOPCODE( char * str){
    int i;
    for (i=0; i &lt; lenOPTAB; i++){
	if( strcasecmp(OPTAB[i].opcode,str) == 0)
	    return 1;
    }
    return 0;
}

char searchSYMTAB( char * str){
    int i;
    for (i=0; i &lt; lenSYMTAB; i++){
	if( strcasecmp(SYMTAB[i].label,str) == 0){
	    if(SYMTAB[i].address == 0)
		return 2;
	    else
		return 1;
	}

    }
    return 0;
}

void writeintrfile(asmlist list,FILE *fp){
    fprintf(fp,&quot;%X\t%s\t%s\t%s\n&quot;,list.address,list.src.label,list.src.opcode,list.src.operand);
    return;
}

void writelistfile(FILE *fp, asmlist list){
    fprintf(fp,&quot;%06X\t%s\t%s\t%s\t%s\n&quot;,list.address,list.src.label,list.src.opcode,list.src.operand,list.hexcode);
}

void writeobjheader(FILE *fp,asmlist list){
    char line[80];
    int i;

    line [0]='H';
    strncpy(line+1,list.src.label,6);
    sprintf(line+7,&quot;%06X&quot;,list.address);
    sprintf(line+13,&quot;%06X&quot;,lenSRC);
    line[19]='&#092;&#048;';

    for( i=0; i&lt; 19; i++){
	if(line[i] =='&#092;&#048;')
	    line[i]=' ';
    }
    fprintf(fp,&quot;%s\n&quot;,line);

}

void writeEnd(FILE *fp){
    fprintf(fp,&quot;E%06X&quot;,startAddr);
}

void initializeTextRec( char *textRec,asmlist list){
    int i;
    for(i=0;i&lt;80;i++){
	textRec[i]='0';
    }
    textRec[0]='T';
    sprintf(textRec+1,&quot;%06X&quot;,list.address);
    sprintf(textRec+7,&quot;%02X&quot;,00);
    return;
}

int getAddress(char *symbol){
    int i;
    for(i=0; i&lt; lenSYMTAB; i++){
	if(strcasecmp(SYMTAB[i].label,symbol) == 0)
	    return SYMTAB[i].address;
    }
    return 0;
}
char * getOpcodeHex(char *op){
    int i;
    for(i=0; i&lt; lenOPTAB; i++){
	if(strcasecmp(OPTAB[i].opcode,op) == 0)
	    return OPTAB[i].hexcode;
    }
    return &quot;00&quot;;
}

int operandLen(char *str){
    char *cptr;
    //Hex Operand
    if(str[0] == 'X' || str[0]=='C'){
	cptr = strtok(str+1,&quot; '&quot;);
	if(cptr != NULL){
	    return strlen(cptr);
	}
	else{
	    return 0;
	}
    }
    return 0;
}

char * getHexOperand(char * strOperand, char *hexOperand){
    int i;
    char *cptr;
    char str[80];
    strcpy(str,strOperand);
    //Hex Operand
    if(str[0] == 'X'){
	cptr = strtok(str+1,&quot; '&quot;);
	if(cptr != NULL){
	    sprintf(hexOperand,&quot;%04X&quot;,strtoul(cptr,0,16));
	}
	else{
	    return NULL;
	}
    }
    //Charector operand
    else if(str[0] == 'C'){

	cptr = strtok(str+1,&quot; '&quot;);
	if(cptr != NULL){

	    for( i=0; cptr[i] != '&#092;&#048;'; i++){
		sprintf(hexOperand+(2*i),&quot;%02X&quot;,cptr[i]);
	    }
	    hexOperand[2*i]='&#092;&#048;';

	}
	else{
	    return NULL;
	}
    }
    //absolute value.
    else if(str[0] == '#'){

	if( searchSYMTAB(str+1) ==1){

	    sprintf(hexOperand,&quot;%04X&quot;,getAddress(str+1));

	}else if( searchSYMTAB(str+1) ==0 &amp;&amp; (str[1] &lt;='0' || str[1] &gt;= '9')){

	    insertSYMTABList(str+1,locCntr+1);
	    sprintf(hexOperand,&quot;%04X&quot;,0);

	}else{

	    errno =0;
	    sprintf(hexOperand,&quot;%04d&quot;,strtoul(str,0,10));

	    if(errno == EINVAL){
		return NULL;
	    }
	}
    }
    else{

	sprintf(hexOperand,&quot;%06X&quot;,strtoul(str,0,16));

    }
    return hexOperand;

}

void writeObjTextRec(FILE *fp, char *str){
    fprintf(fp,&quot;%s\n&quot;,str);
}

Sample Output:
test.asm   &lt;--- Input

COPY    START 0000
FIRST    MOV   THREE
MOV #LENGTH
EOF       BYTE  C'eof'
THREE   WORD  3
ZERO 	WORD 	0
RETADR	RESW	1
LENGTH	RESW	1
BUFFER	RESW	4
MESS1      BYTE  C'HELLO_WORLD'
MESS2      BYTE  C'hello_world'
END

test.lst

000000	COPY	START	0000
000000	FIRST	MOV	THREE	AC0000
000003		MOV	#LENGTH	AC0000
000006	EOF	BYTE	C'eof'	656F66
000009	THREE	WORD	3	000003
00000C	ZERO	WORD	0	000000
00000F	RETADR	RESW	1	000001
000012	LENGTH	RESW	1	000001
000015	BUFFER	RESW	4	000004
000021	MESS1	BYTE	C'HELLO_WORLD'	48454C4C4F5F574F524C44
00002C	MESS2	BYTE	C'hello_world'	68656C6C6F5F776F726C64

test.hex
T00000018AC0000AC0000656F66000003000000000001000001000004
T0000211648454C4C4F5F574F524C4468656C6C6F5F776F726C64
T000001020009
T000004020012
E000000
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quoans.wordpress.com/193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/quoans.wordpress.com/193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/quoans.wordpress.com/193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/quoans.wordpress.com/193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/quoans.wordpress.com/193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/quoans.wordpress.com/193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/quoans.wordpress.com/193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/quoans.wordpress.com/193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/quoans.wordpress.com/193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/quoans.wordpress.com/193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/quoans.wordpress.com/193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/quoans.wordpress.com/193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/quoans.wordpress.com/193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/quoans.wordpress.com/193/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=193&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://quoans.wordpress.com/2010/06/15/one-pass-assembler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1dd47264df9d720f9ebdca2b1a0beb14?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Renjith S</media:title>
		</media:content>
	</item>
		<item>
		<title>Two pass Assembler</title>
		<link>http://quoans.wordpress.com/2010/06/15/two-pass-assembler/</link>
		<comments>http://quoans.wordpress.com/2010/06/15/two-pass-assembler/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 10:48:27 +0000</pubDate>
		<dc:creator>Renjith S</dc:creator>
				<category><![CDATA[System Programming]]></category>
		<category><![CDATA[2 pass assembler]]></category>
		<category><![CDATA[assembler]]></category>
		<category><![CDATA[SIC assembler]]></category>
		<category><![CDATA[two pass assember]]></category>

		<guid isPermaLink="false">http://quoans.wordpress.com/?p=181</guid>
		<description><![CDATA[Two operand instruction are not considered. Sample output is included in the program.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=181&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<blockquote>
<h3><em>Two operand instruction are not considered. Sample output is included in the program.</em></h3>
</blockquote>
<pre><span id="more-181"></span>

<pre class="brush: cpp;">

#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;errno.h&gt;

#define lenOPTAB 6

struct{
    char opcode[32];
    char hexcode[3];
}OPTAB[]={
    {&quot;ADD&quot;,&quot;08&quot;},
    {&quot;SUB&quot;,&quot;1C&quot;},
    {&quot;MUL&quot;,&quot;20&quot;},
    {&quot;DIV&quot;,&quot;24&quot;},
    {&quot;MOV&quot;,&quot;AC&quot;},
    {&quot;END&quot;,&quot;00&quot;}
};

struct{
    char label[32];
    int address;
}SYMTAB[100];

typedef struct{
    char label[32];
    char opcode[32];
    char operand[32];

}Token;

typedef struct{
    int address;
    Token src;
    char hexcode[30];
}asmlist;

int lenSRC = 0;
int lenSYMTAB = 0;
int startAddr =0;

void tokenize( char * line, Token *tok);
int  getline(FILE *fp,char * line);
char isOPCODE( char * str);

int pass1(FILE *srcfp, FILE *intfp);
void pass2(FILE *intrfp, FILE *lstfp, FILE *hexfp);

char searchSYMTAB( char * str);
void insertSYMTAB( char * label,int locCntr);
void tokenizeIntr( char * line, asmlist *list);
void initializeTextRec(char *textRec,asmlist list);
void writeObjTextRec(FILE *fp, char *str);

int  getAddress(char *symbol);
char * getHexOperand(char * str, char *hexOperand);
char * getOpcodeHex(char *op);
int operandLen(char *str);

void writeintrfile(asmlist list,FILE *fp);
void writelistfile(FILE *fp, asmlist list);
void writeobjheader(FILE *fp,asmlist list);
void writeEnd(FILE *fp);

int  main(int argc, char * argv[]){
    FILE *srcfp, *lstfp, *intrfp, *hexfp;
    char lstfname[30], intrfname[30],hexfname[30];
    if (argc != 2){
        printf(&quot;Invalid usage !!!&quot;);
        return;
    }

    // generate filename for list,intermediate and hexfiles.
    if( strstr(argv[1],&quot;.&quot;) ){
        strcpy(lstfname,argv[1]);
        strcpy(strstr(lstfname,&quot;.&quot;),&quot;.lst&quot;);
        strcpy(intrfname,argv[1]);
        strcpy(strstr(intrfname,&quot;.&quot;),&quot;.int&quot;);
        strcpy(hexfname,argv[1]);
        strcpy(strstr(hexfname,&quot;.&quot;),&quot;.hex&quot;);
    }else{
        strcpy(lstfname,argv[1]);
        strcat(lstfname,&quot;.lst&quot;);
        strcpy(intrfname,argv[1]);
        strcat(intrfname,&quot;.int&quot;);
        strcpy(hexfname,argv[1]);
        strcat(hexfname,&quot;.hex&quot;);
    }

    srcfp=fopen(argv[1],&quot;r&quot;);
    if(srcfp == NULL){
        printf(&quot;!!! Error. Unable to open the src file. &quot;);
        return;
    }

    intrfp=fopen(intrfname,&quot;w&quot;);
    if(intrfp == NULL){
        printf(&quot;!!! Error. Unable to open the intermediate file. &quot;);
        fclose(srcfp);
        return;
    }

    lstfp=fopen(lstfname,&quot;w&quot;);
    if(lstfp == NULL){
        printf(&quot;!!! Error. Unable to open the list file. &quot;);
        fclose(srcfp);
        fclose(intrfp);
        return;
    }

    hexfp=fopen(hexfname,&quot;w&quot;);
    if(hexfp == NULL){
        printf(&quot;!!! Error. Unable to open the hex file. &quot;);
        fclose(srcfp);
        fclose(intrfp);
        fclose(lstfp);
        return;
    }

    // pass 1 generate intermediate file
    if(! pass1(srcfp,intrfp)){
        fclose(srcfp);
        fclose(intrfp);
        return;
    }

    fclose(srcfp);
    fclose(intrfp);

    intrfp=fopen(intrfname,&quot;r&quot;);
    if(intrfp == NULL){
        printf(&quot;!!! Error. Unable to open the intermediate file. &quot;);
        fclose(srcfp);
        return;
    }

    //generate list file and hexfile
    pass2(intrfp,lstfp,hexfp);

    fclose(lstfp);
    fclose(hexfp);
}

int pass1(FILE *srcfp, FILE *intrfp){
    char line[80];
    char *cptr;

    Token tok;
    int locCntr;
    asmlist list;
    if(! getline(srcfp,line)) return;  // read first line

    tokenize(line,&amp;tok);  // tokenize the line

    if( strcasecmp(tok.opcode,&quot;START&quot;) == 0){
        locCntr = strtoul(tok.operand,0,16);
        list.address = locCntr;
        list.src = tok;
        writeintrfile(list,intrfp);
        if(!getline(srcfp,line)) return;
        tokenize(line,&amp;tok);

    }else{
        locCntr =0;
    }

    startAddr=locCntr;
    lenSRC = locCntr;

    while( strcasecmp(tok.opcode,&quot;END&quot;) != 0){

        // check for comments. comments start with a '.'

        if( line[0] != '.'){

	    list.address = locCntr;
	    list.src = tok;
	    writeintrfile(list,intrfp); // writes intermediate file.

	    // check for label
	    if(strlen(tok.label) != 0){
		if(searchSYMTAB(tok.label)){
		    printf(&quot;Error : duplicate symbol @ %s&quot;,line);
		    return 0;
		}else{
		    //insert symboltable entry
		    insertSYMTAB(tok.label,locCntr);
		}
	    }

	    //Finds instruction length
	    if( isOPCODE(tok.opcode)){
		locCntr = locCntr +3;
	    }
	    else if( strcasecmp(tok.opcode,&quot;WORD&quot;) == 0){
		locCntr = locCntr +3;
	    }
	    else if( strcasecmp(tok.opcode,&quot;RESW&quot;) == 0){
		// operand length is specified in the instruction itself
		if(tok.operand[0] == 'X'){
		    cptr=strtok(tok.operand,&quot; '&quot;);
		    if(cptr != NULL)
			locCntr = locCntr + 3* strtoul(cptr,0,16);
		}
		else{
		    locCntr = locCntr + 3* strtoul(tok.operand,0,10);
		}
	    }
	    else if( strcasecmp(tok.opcode,&quot;RESB&quot;) == 0){
		// operand length is in instruction itself
		if(tok.operand[0] == 'X'){
		    cptr=strtok(tok.operand,&quot; '&quot;);
		    if(cptr != NULL)
			locCntr = locCntr + strtoul(cptr,0,16);
		}
		else{
		    locCntr = locCntr +  strtoul(tok.operand,0,10);
		}
	    }
	    else if( strcasecmp(tok.opcode,&quot;BYTE&quot;) == 0){
		//length of the operand
		locCntr = locCntr + operandLen(tok.operand);
	    }
	    else{
		printf(&quot;Error Invalid operation code. @ %s &quot;,line);
		return 0;
	    }
        }
        if(!getline(srcfp,line)) return 0;
        tokenize(line,&amp;tok);
    }

    list.address = locCntr;
    list.src = tok;
    writeintrfile(list,intrfp);

    lenSRC = locCntr - lenSRC;

    return 1;
}

void pass2(FILE *intrfp, FILE *lstfp, FILE *hexfp){
    char line[80];
    asmlist list;
    Token *tok;
    tok = &amp;(list.src);

    char textRec[80];
    int operandAddr=0;
    char hexOperand[80];
    char hexObj[80],temp[80];
    int Index=9;

    if(!getline(intrfp,line)) return;
    tokenizeIntr(line,&amp;list);//tokenize the intermediate code

    if (strcasecmp(tok-&gt;opcode,&quot;START&quot;) == 0 ){
        writelistfile(lstfp,list);

        if(!getline(intrfp,line)) return;
        tokenizeIntr(line,&amp;list);
    }
    writeobjheader(hexfp,list);  //H+Pgm_Name+Start_Addr+Length
    initializeTextRec(textRec,list); //  T+Starting address

    while(strcasecmp(tok-&gt;opcode,&quot;END&quot;) != 0){
        // Valid Opcode
        if(isOPCODE(tok-&gt;opcode)){

	    if(strlen(tok -&gt; operand) != 0){
		if(searchSYMTAB(tok-&gt;operand)){
		    // get symbol address
		    operandAddr=getAddress(tok-&gt;operand);
		    //opcode+label address
		    sprintf(hexObj,&quot;%s%04X&quot;,getOpcodeHex(tok -&gt; opcode), operandAddr);
		}
		else{
		    if( getHexOperand(tok-&gt;operand,hexOperand) == NULL ){
			printf(&quot;Invalid operand @ %s&quot;,line);
		    }
		    //opcode+operand value
		    sprintf(hexObj,&quot;%s%s&quot;,getOpcodeHex(tok -&gt; opcode),hexOperand);
		}
	    }
        }

        else{
	    if(strcasecmp(tok -&gt; opcode,&quot;BYTE&quot;) == 0 ||	\
	       strcasecmp(tok -&gt; opcode,&quot;WORD&quot;) == 0){
		getHexOperand(tok-&gt;operand,hexOperand);
		//operand
		sprintf(hexObj,&quot;%s&quot;,hexOperand);
	    }
	    else{
		getHexOperand(tok-&gt;operand,hexOperand);
		//operand
		sprintf(hexObj,&quot;%s&quot;,hexOperand);

	    }
        }

        if((Index+strlen(hexObj)) &gt; 69){
	    sprintf(textRec+7,&quot;%02X%s&quot;,(strlen(textRec)-9)/2,textRec+9);//place length in lenField
	    writeObjTextRec(hexfp,textRec);
	    initializeTextRec(textRec,list);
        }

        strncpy(temp,textRec+1,6);
        Index = ((list.address - strtoul(temp,0,16))*2 )+9;

        sprintf(textRec+Index,&quot;%s&quot;,hexObj);
        Index =Index + strlen(hexObj);
        strcpy(list.hexcode,hexObj);

        writelistfile(lstfp,list);

        if(!getline(intrfp,line)) return;
        tokenizeIntr(line,&amp;list);

    }
    sprintf(textRec+7,&quot;%02X%s&quot;,(strlen(textRec)-9)/2,textRec+9);
    writeObjTextRec(hexfp,textRec);
    writeEnd(hexfp);
} 

void insertSYMTAB( char * label,int locCntr){
    strcpy(SYMTAB[lenSYMTAB].label,label);
    SYMTAB[lenSYMTAB].address = locCntr;
    lenSYMTAB ++;
    return;
}

int getline(FILE *fp,char * line){
    char ch;
    int cnt=0;
    while ( (ch = fgetc(fp)) != EOF &amp;&amp; ch != '\n' ){
        line[cnt++] = ch;
    }
    line[cnt] = '&#092;&#048;';

    if(line [0] == '&#092;&#048;')
        return 0;
    else
        return 1;
}

void tokenize( char * line, Token *tok){
    char *chr;
    strcpy(tok -&gt; label, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; opcode, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; operand, &quot;&#092;&#048;&quot;);

    chr =strtok(line, &quot; \t&quot;);

    if( chr != NULL){
        //check for opcode
        if ( isOPCODE(chr)){
	    strcpy(tok -&gt; opcode,chr); // instruction opcode+operand
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; operand, chr);
        }else{  // instruction label+opcode+operand
	    strcpy(tok-&gt;label,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; opcode,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; operand, chr);
        }
    }
    //printf(&quot; %s | %s | %s \n&quot;, tok-&gt; label, tok-&gt; opcode, tok -&gt; operand);
    return;
}

void tokenizeIntr( char * line, asmlist *list){
    char *chr;
    Token *tok;
    tok = &amp; (list -&gt; src);
    list -&gt; address = 0;
    strcpy(list-&gt;hexcode,&quot;&quot;);
    strcpy(tok -&gt; label, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; opcode, &quot;&#092;&#048;&quot;);
    strcpy(tok -&gt; operand, &quot;&#092;&#048;&quot;);

    chr =strtok(line, &quot; \t&quot;);

    if( chr != NULL){
        list -&gt; address = strtoul(chr,0,16);
        chr = strtok(NULL, &quot; \t&quot;);

        if ( isOPCODE(chr)){
	    strcpy(tok -&gt; opcode,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; operand, chr);
        }else{
	    strcpy(tok-&gt;label,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; opcode,chr);
	    chr = strtok(NULL, &quot; \t&quot;);
	    if( chr != NULL )
		strcpy(tok -&gt; operand, chr);
        }
    }
    //printf(&quot; %s | %s | %s \n&quot;, tok-&gt; label, tok-&gt; opcode, tok -&gt; operand);
    return;
}

char isOPCODE( char * str){
    int i;
    for (i=0; i &lt; lenOPTAB; i++){
        if( strcasecmp(OPTAB[i].opcode,str) == 0)
	    return 1;
    }
    return 0;
}

char searchSYMTAB( char * str){
    int i;
    for (i=0; i &lt; lenSYMTAB; i++){
        if( strcasecmp(SYMTAB[i].label,str) == 0)
	    return 1;
    }
    return 0;
}
void writeintrfile(asmlist list,FILE *fp){
    fprintf(fp,&quot;%X\t%s\t%s\t%s\n&quot;,list.address,list.src.label,list.src.opcode,list.src.operand);
    return;
}
void writelistfile(FILE *fp, asmlist list){
    fprintf(fp,&quot;%06X\t%s\t%s\t%s\t%s\n&quot;,list.address,list.src.label,list.src.opcode,list.src.operand,list.hexcode);
}

void writeobjheader(FILE *fp,asmlist list){
    char line[80];
    int i;

    line [0]='H';
    strncpy(line+1,list.src.label,6);
    sprintf(line+7,&quot;%06X&quot;,list.address);
    sprintf(line+13,&quot;%06X&quot;,lenSRC);
    line[19]='&#092;&#048;';

    for( i=0; i&lt; 19; i++){
        if(line[i] =='&#092;&#048;')
	    line[i]=' ';
    }
    fprintf(fp,&quot;%s\n&quot;,line);

}

void writeEnd(FILE *fp){

    fprintf(fp,&quot;E%06X&quot;,startAddr);

}
void initializeTextRec( char *textRec,asmlist list){
    int i;
    for(i=0;i&lt;80;i++){
        textRec[i]='0';
    }
    textRec[0]='T';
    sprintf(textRec+1,&quot;%06X&quot;,list.address);
    sprintf(textRec+7,&quot;%02X&quot;,00);
    return;
}

int getAddress(char *symbol){
    int i;
    for(i=0; i&lt; lenSYMTAB; i++){
        if(strcasecmp(SYMTAB[i].label,symbol) == 0)
	    return SYMTAB[i].address;
    }
    return 0;
}
char * getOpcodeHex(char *op){
    int i;
    for(i=0; i&lt; lenOPTAB; i++){
        if(strcasecmp(OPTAB[i].opcode,op) == 0)
	    return OPTAB[i].hexcode;
    }
    return &quot;00&quot;;
}

int operandLen(char *str){
    char *cptr;
    //Hex Operand
    if(str[0] == 'X' || str[0]=='C'){
        cptr = strtok(str+1,&quot; '&quot;);
        if(cptr != NULL){
	    return strlen(cptr);
        }
        else{
	    return 0;
        }
    }
    return 0;
}

char * getHexOperand(char * strOperand, char *hexOperand){
    int i;
    char *cptr;
    char str[80];
    strcpy(str,strOperand);
    //Hex Operand
    if(str[0] == 'X'){
        cptr = strtok(str+1,&quot; '&quot;);
        if(cptr != NULL){
	    sprintf(hexOperand,&quot;%04X&quot;,strtoul(cptr,0,16));
        }
        else{
	    return NULL;
        }
    }
    //Charector operand
    else if(str[0] == 'C'){

        cptr = strtok(str+1,&quot; '&quot;);
        if(cptr != NULL){

	    for( i=0; cptr[i] != '&#092;&#048;'; i++){
		sprintf(hexOperand+(2*i),&quot;%02X&quot;,cptr[i]);
	    }
	    hexOperand[2*i]='&#092;&#048;';

        }
        else{
	    return NULL;
        }
    }
    //absolute value.
    else if(str[0] == '#'){
        if( searchSYMTAB(str+1)){
	    sprintf(hexOperand,&quot;%04X&quot;,getAddress(str+1));

        }else{
	    errno =0;
	    sprintf(hexOperand,&quot;%04d&quot;,strtoul(str,0,10));

	    if(errno == EINVAL){
		return NULL;
	    }
        }
    }
    else{

        sprintf(hexOperand,&quot;%06X&quot;,strtoul(str,0,16));

    }
    return hexOperand;

}

void writeObjTextRec(FILE *fp, char *str){
    fprintf(fp,&quot;%s\n&quot;,str);
}

Sample Output:
test.asm

COPY    START 0000
FIRST    MOV   THREE
MOV #LENGTH
EOF       BYTE  C'eof'
THREE   WORD  3
ZERO 	WORD 	0
RETADR	RESW	1
LENGTH	RESW	1
BUFFER	RESW	4
MESS1      BYTE  C'HELLO_WORLD'
MESS2      BYTE  C'hello_world'
END

test.lst

000000	COPY	START	0000
000000	FIRST	MOV	THREE	AC0009
000003		MOV	#LENGTH	AC0012
000006	EOF	BYTE	C'eof'	656F66
000009	THREE	WORD	3	000003
00000C	ZERO	WORD	0	000000
00000F	RETADR	RESW	1	000001
000012	LENGTH	RESW	1	000001
000015	BUFFER	RESW	4	000004
000021	MESS1	BYTE	C'HELLO_WORLD'	48454C4C4F5F574F524C44
00002C	MESS2	BYTE	C'hello_world'	68656C6C6F5F776F726C64

test.hex

HFIRST 000000000037
T00000018AC0009AC0012656F66000003000000000001000001000004
T0000211648454C4C4F5F574F524C4468656C6C6F5F776F726C64
E000000
</pre>

<!--more-->
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quoans.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/quoans.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/quoans.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/quoans.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/quoans.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/quoans.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/quoans.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/quoans.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/quoans.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/quoans.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/quoans.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/quoans.wordpress.com/181/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/quoans.wordpress.com/181/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/quoans.wordpress.com/181/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=181&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://quoans.wordpress.com/2010/06/15/two-pass-assembler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1dd47264df9d720f9ebdca2b1a0beb14?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Renjith S</media:title>
		</media:content>
	</item>
		<item>
		<title>Object Relational Mapping –API for C++</title>
		<link>http://quoans.wordpress.com/2009/07/30/object-relational-mapping-%e2%80%93api-for-c/</link>
		<comments>http://quoans.wordpress.com/2009/07/30/object-relational-mapping-%e2%80%93api-for-c/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 04:37:36 +0000</pubDate>
		<dc:creator>Renjith S</dc:creator>
				<category><![CDATA[MINI PROJECT]]></category>

		<guid isPermaLink="false">http://quoans.wordpress.com/?p=174</guid>
		<description><![CDATA[Object relational mapping (ORM) is the automated and transparent persistence of objects in a C++ application to the tables in a relational database. ORM is a modeling and code generation tool that connects to database table and views using the metadata that describes mapping between objects and database. Using this API, the programmer can specify [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=174&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Object relational mapping (ORM) is the automated and transparent persistence of objects in a C++ application to the tables in a relational database. ORM is a modeling and code generation tool that connects to database table and views using the metadata that describes mapping between objects and database. Using this API, the programmer can specify single-row insert, update, load and delete operations, queries and stored procedure calls as method to objects. It works by mapping structures from domain model to the structures in the relational database.<br />
The application interacts with the ORM APIs and the domain model classes, abstracted from the underlying SQL as compared to the traditional Tuples approach, In Tupple approach the application simply reads the record from RDBMS to record sets and then it works with these record sets, where Data aren’t read to objects. </p>
<p>The solution consists of the following,<br />
An API for performing basic creates, read, update and delete operations of persistent classes<br />
A language for specifying queries (object oriented expression language) that refers to classes and properties of the classes.<br />
A facility for specifying mapping metadata</p>
<p>Development Platform:<br />
Operating system	: 	Linux<br />
Compilers		: 	gcc-compatible C++<br />
Database		: 	MySQL</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quoans.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/quoans.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/quoans.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/quoans.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/quoans.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/quoans.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/quoans.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/quoans.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/quoans.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/quoans.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/quoans.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/quoans.wordpress.com/174/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/quoans.wordpress.com/174/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/quoans.wordpress.com/174/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=174&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://quoans.wordpress.com/2009/07/30/object-relational-mapping-%e2%80%93api-for-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1dd47264df9d720f9ebdca2b1a0beb14?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Renjith S</media:title>
		</media:content>
	</item>
		<item>
		<title>Graph seaching BFS and DFS</title>
		<link>http://quoans.wordpress.com/2009/05/15/graph-seaching-bfs-and-dfs/</link>
		<comments>http://quoans.wordpress.com/2009/05/15/graph-seaching-bfs-and-dfs/#comments</comments>
		<pubDate>Fri, 15 May 2009 11:00:37 +0000</pubDate>
		<dc:creator>Renjith S</dc:creator>
				<category><![CDATA[Lab]]></category>
		<category><![CDATA[bfs]]></category>
		<category><![CDATA[breadth first search]]></category>
		<category><![CDATA[depth first search]]></category>
		<category><![CDATA[dfs]]></category>
		<category><![CDATA[graph representation using adjacency list]]></category>
		<category><![CDATA[graph seaching]]></category>
		<category><![CDATA[graph searching algorithms]]></category>
		<category><![CDATA[graph searching using adjacency list]]></category>

		<guid isPermaLink="false">http://quoans.wordpress.com/2009/05/15/graph-seaching-bfs-and-dfs/</guid>
		<description><![CDATA[The following program search the graph, represented using adjacency list, using BFS and DFS algorithms and displays the path.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=147&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3><span style="color:#000080;">The following program search the graph, represented using adjacency list, using BFS and DFS algorithms and displays the path.</span></h3>
<pre><span id="more-147"></span><br />
<pre class="brush: cpp;">
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define Q_SIZE      100
#define S_SIZE      100

#define VISITED        1       //MOD 01
#define NOTVISITED 0       //MOD 01

struct vertex_node {
    int data,status;
    struct vertex_node *next;
    struct adj_link *adj;
};

struct adj_link {
    struct vertex_node *next;
    struct adj_link *adj;
};

typedef struct vertex_node v_node;
typedef struct adj_link a_link;

v_node * create   ();
void     bfs      ( v_node * );
void     dfs      ( v_node * );
char     getchoice();
void     free_node( v_node * );
void     free_link( a_link * );
 
int main(){
    char choice;
    v_node *head=NULL;
    printf(&quot;Creating the Graph in Adjacency List Representation \n&quot;);
    head = create();
    do{
	  choice = getchoice();

	  switch (choice){
	  case '1':
		bfs(head);
		break;
	  case '2':
		dfs(head);
		break;
	  case '3':
		free_node(head);
		break;
	  default:
		printf(&quot;Invalid Choice !!! \n&quot;);
	  }
    }while(choice != '3');
    return;
}
/******************************************************************
This funciton will display the menu and get user's choice.
******************************************************************/
char getchoice(){
    char temp[80];
    printf (&quot;\n&quot;);
    printf (&quot;1: Breadth First Search\n&quot;);
    printf (&quot;2: Depth First Search\n&quot;);
    printf (&quot; 3: Exit \n\n&quot;);
    printf (&quot;Choice ? &quot;);
    scanf(&quot;%s&quot;,temp);
  
    return temp[0];
}

/******************************************************************
This funciton create a graph using adjacentcylist.
******************************************************************/
v_node * create(){
    int tmp_data;
    v_node * head, *node, *adj_node;
    a_link * link, *p_link;
    head = NULL;

    //Read the vertex list.
    printf(&quot;Node in the Graph (Type Ctrl-D to END): \n&quot;);

    while( ( scanf ( &quot;%d&quot;,&amp;tmp_data )) != EOF){

	  node = ( v_node* ) malloc ( sizeof(v_node) );

	  node -&gt; data = tmp_data;
	  node -&gt; status = NOTVISITED;
	  node -&gt; next = NULL;
	  node -&gt; adj = NULL;

	  if ( head == NULL ){
		head     = node;
		adj_node = node;
	  }
	  else{
		adj_node -&gt; next = node;
		adj_node = node;
	  }
    }

    //Read the edge list.
    node = head;

    while( node != NULL ){
	  p_link = NULL;
	  printf (&quot;Links from %d node (Type Ctrl-D to END \n&quot;,node -&gt; data);   //MOD 01
	  while ( ( scanf(&quot;%d&quot;,&amp;tmp_data)) != EOF){
		link = (a_link *) malloc (sizeof(a_link));
		link -&gt; adj = NULL;
		if ( p_link == NULL){
		    node -&gt; adj = link;
		    p_link = link;
		}
		else{
		    p_link -&gt; adj = link;
		    p_link = link;
		}

		// give a cross link to corresponding vertex
		adj_node = head;
		while ( adj_node != NULL){
		    if ( adj_node -&gt; data == tmp_data){
			  link -&gt; next = adj_node;
		    }
		    adj_node = adj_node -&gt; next;
		}
	  }
	  node = node -&gt; next;
    }
    return head;
}


void bfs ( v_node * head){
    int queue[Q_SIZE], q_front=-1, q_rear=-1;
    v_node * node, *adj_node;
    a_link * link;
    node = head;

    while ( node != NULL){
	  node -&gt; status = NOTVISITED;
	  node = node -&gt; next;
    
    }

    node = head;

    q_rear++;
    queue[q_rear] = node-&gt; data;
    q_front++;
    node -&gt; status = VISITED;

    while ( q_front &lt;= q_rear ) {
	  node = head;

	  while ( node != NULL){
		if ( node -&gt; data == queue[q_front]){
		    break;
		}
		node = node -&gt; next;
	  }

	  link = node -&gt; adj;

	  while ( link != NULL ){
		adj_node = link -&gt; next;
		if( adj_node -&gt; status == NOTVISITED ){
		    q_rear ++;
		    queue[q_rear] = adj_node -&gt; data;

		    adj_node -&gt; status = VISITED;
		}
		link = link -&gt; adj;
	  }
	  q_front++;
    }
    q_front =0;

    printf (&quot;Breadth First Search Path: \n&quot;);

    while ( q_front &lt;= q_rear){
	  printf ( &quot;%d &quot;,queue[q_front] );
	  q_front++;
    }

    printf(&quot;\n&quot;);
    return;
}

void dfs ( v_node * head){
    v_node *node, *adj_node;
    a_link *link;
    int stack[S_SIZE],top=0;

    node = head;
    while ( node != NULL ){
	  node -&gt; status = NOTVISITED;
	  node = node -&gt; next;
    }

    node = head;
    stack[top] = node -&gt; data;
    node -&gt; status = VISITED;

    printf(&quot;Depth First Search Path: \n&quot;);
    while ( top &gt;= 0){
	  node = head;
	  while ( node != NULL){
		if ( node -&gt; data == stack[top]){
		    break;
		}
		node = node -&gt; next;
	  }

	  printf(&quot;%d &quot;,stack[top]);
	  top--;
	  link = node -&gt; adj;
	  while ( link != NULL){
		adj_node= link -&gt; next;
		if( adj_node -&gt; status == NOTVISITED){
		    top++;
		    stack[top] = adj_node -&gt; data;
		    adj_node -&gt; status = VISITED;
		}
		link = link-&gt;adj;
	  }
    }
    printf(&quot;\n&quot;);
    return;
}

void free_node (v_node *node){
    if (node -&gt; next != NULL){
	  free_node (node -&gt; next);
    }
    if(node -&gt; adj != NULL){
    	free_link(node-&gt;adj);
    }    
    free(node);
  
    return;
}

void free_link (a_link *link){
    if(link -&gt; adj != NULL){             //MOD 01
	  free_link(link -&gt; adj);
    }
    free(link);
    return;
}
 
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/quoans.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/quoans.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/quoans.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/quoans.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/quoans.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/quoans.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/quoans.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/quoans.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/quoans.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/quoans.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/quoans.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/quoans.wordpress.com/147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/quoans.wordpress.com/147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/quoans.wordpress.com/147/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=quoans.wordpress.com&amp;blog=6497009&amp;post=147&amp;subd=quoans&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://quoans.wordpress.com/2009/05/15/graph-seaching-bfs-and-dfs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1dd47264df9d720f9ebdca2b1a0beb14?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Renjith S</media:title>
		</media:content>
	</item>
	</channel>
</rss>
