Important alert: (current site time 9/17/2014 7:13:07 AM EDT)
 

VB icon

PASS 2 OF 2-PASS ASSEMBLER

Email
Submitted on: 9/2/2011 4:02:23 PM
By: Rekha Ram Chaudhary  
Level: Intermediate
User Rating: Unrated
Compatibility: C
Views: 13441
author picture
(About the author)
 
     To write a C-program to implement pass-2 assembler ie object code creation.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: PASS 2 OF 2-PASS ASSEMBLER
// Description:To write a C-program to implement pass-2 assembler ie object code creation.
// By: Rekha Ram Chaudhary
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=13643&lngWId=3//for details.//**************************************

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp1,*fp2,*fp3,*fp4;
char label[10],opcode[10],operand[10],symbol[10],code[10];
char mnemonic[5],character,add[10],objectcode[10];
int flag,flag1,locctr,location,loc;
clrscr();
fp1=fopen("out.dat","r");
fp2=fopen("p2out.dat","w");
fp3=fopen("optab.dat","r");
fp4=fopen("symtab.dat","r");
if(fp1==NULL)
{
printf("The file cannot be open");
}
if(fp2==NULL)
{
printf("The file cannot be open");
}
if(fp3==NULL)
{
printf("The file cannot be open");
}
if(fp4==NULL)
{
printf("The file cannot be open");
}
fscanf(fp1,"%s%s%s",label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);
}
while(strcmp(opcode,"END")!=0)
{
flag=0;
fscanf(fp3,"%s%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{
if((strcmp(opcode,code)==0)&&(strcmp(mnemonic,"*"))!=0)
{
flag=1;
break;
}
fscanf(fp3,"%s%s",code,mnemonic);
}
if(flag==1)
{
flag1=0;
rewind(fp4);
while(!feof(fp4))
{
fscanf(fp4,"%s%d",symbol,&loc);
if(strcmp(symbol,operand)==0)
{
flag=1;
break;
}
}
if(flag1==1)
{
itoa(loc,add,10);
strcpy(objectcode,strcat(mnemonic,add));
}
}
else if(strcmp(opcode,"BYTE")==0 || strcmp(opcode,"WORD")==0)
{
if((operand[0]=='C')||(operand[0]=='X'))
{
character=operand[2];
itoa(character,add,16);
strcpy(objectcode,add);
}
else
{
itoa(atoi(operand),add,10);
strcpy(objectcode,add);
}
}
strcpy(objectcode,"\0");
fprintf(fp2,"%s\t%s\t%s\t%s\n",label,opcode,operand,locctr,objectcode);
fscanf(fp1,"%d%s%s%s",&locctr,label,opcode,operand);
}
fprintf(fp2,"%s\t%s\t%s\n",label,opcode,operand,locctr);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
getch();
}
INPUT FILE:
Out.dat
	**		START	2000
2000	**		LDA		FIVE
2003	**		STA		ALPHA
2006	**		LDCH		CHARZ
2009	**		STCH		C1
2012	ALPHA	RESW		1
2015	FIVE		WORD	5
2018 	CHARZ	BYTE		C’Z’
2019 	C1		RESB		1
2020	**		END		**
Optab.dat
START	 *
LDA 		 03
STA		 0F
LDCH		 53
STCH 		 57
END 		 *
Symtab.dat
ALPHA	2012
FIVE		2015
CHARZ	2018
C1		2019
OUTPUT FILE:
P2out.dat
**		START	2000				
** 		LDA		FIVE		2000		032015
** 		STA 		ALPHA	2003		0F2012
** 		LDCH 		CHARZ	2006		532018
** 		STCH 		C1		2009		572019
ALPHA 	RESW 	1		2012		
FIVE 		WORD 	5		2015		5
CHARZ 	BYTE 		C'Z'		2018		5a
C1 		RESB 		1		2019
** 		END 		**		2020


Other 9 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.