Pass-I of Assembler in Two pass Assembler (code), Study notes of System Programming

C program to implement PASS 1 of a two pass Assembler.

Typology: Study notes

2016/2017

Uploaded on 11/28/2017

shah-janki
shah-janki 🇮🇳

4.5

(2)

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Practical – 6
AIM: Write a program to implement PASS 1 of a two pass Assembler.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
struct symtab
{
char sym[10];
int symadd;
};
struct symtab st[20];
void main()
{
char opcode[10],mnemonic[3],op1[10],op2[10],label[10],code[10],ic[10];
int locctr,start,length,symcnt=0,symid=0;
FILE *finput,*foutput,*fsymtab,*foptab;
clrscr();
finput=fopen("C:/SP_Prog/SP/INPUT.txt","r");
fsymtab=fopen("C:/SP_Prog/SP/SYMTAB.txt","w");
foutput=fopen("C:/SP_Prog/SP/OUTPUT.txt","w");
foptab=fopen("C:/SP_Prog/SP/OPTAB.txt","r");
fscanf(finput,"%s%s%s%s",label,opcode,op1,op2);
char icop1[10],icop2[10];
if(strcmp(opcode,"START")==0)
{
start=atoi(op1);
locctr=start;
strcpy(icop1,"(C,");
strcat(icop1,op1);
strcat(icop1,")");
fprintf(foutput,"%s\t\t%s\t\t%s\n","(AD-01)",icop1,"");
fscanf(finput,"%s%s%s%s",label,opcode,op1,op2);
}
else
locctr=0;
int i;
pf3
pf4
pf5

Partial preview of the text

Download Pass-I of Assembler in Two pass Assembler (code) and more Study notes System Programming in PDF only on Docsity!

Practical – 6

AIM: Write a program to implement PASS 1 of a two pass Assembler.

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

#include<ctype.h>

struct symtab

char sym[10];

int symadd;

struct symtab st[20];

void main()

char opcode[10],mnemonic[3],op1[10],op2[10],label[10],code[10],ic[10];

int locctr,start,length,symcnt=0,symid=0;

FILE finput,foutput,fsymtab,foptab;

clrscr();

finput=fopen("C:/SP_Prog/SP/INPUT.txt","r");

fsymtab=fopen("C:/SP_Prog/SP/SYMTAB.txt","w");

foutput=fopen("C:/SP_Prog/SP/OUTPUT.txt","w");

foptab=fopen("C:/SP_Prog/SP/OPTAB.txt","r");

fscanf(finput,"%s%s%s%s",label,opcode,op1,op2);

char icop1[10],icop2[10];

if(strcmp(opcode,"START")==0)

start=atoi(op1);

locctr=start;

strcpy(icop1,"(C,");

strcat(icop1,op1);

strcat(icop1,")");

fprintf(foutput,"%s\t\t%s\t\t%s\n","(AD-01)",icop1,"");

fscanf(finput,"%s%s%s%s",label,opcode,op1,op2);

else

locctr=0;

int i;

while(strcmp(opcode,"END")!=0)

if(strcmp(opcode,"ORIGIN")==0)

locctr=atoi(op1);

strcpy(ic,"(AD,03)");

if(strcmp(label,"**")!=0)

symcnt=symcnt + 1;

if(strcmp(opcode,"EQU")==0)

if(isdigit(op1[0]));

fprintf(fsymtab,"%s\t%d\n",label,locctr);

symid=symid+1;

fprintf(fsymtab,"%d\t\t%s\t\t%d\n",symid,label,locctr);

rewind(foptab);

fscanf(foptab,"%s%s",code,ic);

while(strcmp(code,NULL)!=0)

if(strcmp(opcode,code)==0)

locctr+=1;

break;

fscanf(foptab,"%s%s",code,ic);

length=locctr-start;

printf("The length of the program is %d",length);

fclose(finput);

fclose(fsymtab);

fclose(foutput);

fclose(foptab);

getch();

OUTPUT:

INPUT.TXT

Label OPCODE OPERAND1 OPERAND

** START 101 **

** READ N **

** MOVER BREG ONE

** MOVEM BREG TERM

AGAIN MULT BREG TERM

** MOVER CREG TERM

** ADD CREG TERM

** MOVEM CREG TERM

ABC EQU 210 **

** COMP CREG N

** BC LE AGAIN

** ORIGIN 201 **

** MOVEM BREG RESULT

** PRINT RESULT **

** STOP ** **

N DS 1 **

RESULT DS 1 **

ONE DC 1 **

TERM DS 1 **

** END ** **

OUTPUT.TXT

(AD-01) (C,101)

(IS,09) N

(IS,04) (2) ONE

(IS,05) (2) TERM

(IS,03) (2) TERM

(IS,04) (3) TERM

(IS,01) (3) TERM

(IS,05) (3) TERM

(AD,04) (C,210)

(IS,06) (3) N

(IS,07) LE AGAIN

(AD,03) (C,201)

(IS,05) (2) RESULT

(IS,10) RESULT

(IS,00) **

(DL,02) (C,1)

(DL,02) (C,1)

(DL,01) (C,1)

(DL,02) (C,1)

OPTAB.TXT

STOP (IS,00)

READ (IS,09)

ADD (IS,01)

SUB (IS,02)

MULT (IS,03)

MOVER (IS,04)

MOVEM (IS,05)

COMP (IS,06)

BC (IS,07)

DIV (IS,08)

PRINT (IS,10)

START (AD,01)

ORIGIN (AD,03)

DC (DL,01)

DS (DL,02)

EQU (AD,04)