0% found this document useful (0 votes)
107 views11 pages

S SW lAB

1. The document describes implementing the first pass of a two-pass assembler. It initializes the location counter, processes assembly statements, and writes symbol table and intermediate files. 2. It reads the assembly code, increments the location counter based on the operation, and writes labels and their addresses to the symbol table file. 3. It outputs the length of the assembled program as 20.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views11 pages

S SW lAB

1. The document describes implementing the first pass of a two-pass assembler. It initializes the location counter, processes assembly statements, and writes symbol table and intermediate files. 2. It reads the assembly code, increments the location counter based on the operation, and writes labels and their addresses to the symbol table file. 3. It outputs the length of the assembled program as 20.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

EX NO:1 SYMBOL TABLE GENERATION

AIM: To implement a Symbol table with functions to create, insert, modify, search and display in C language. ALGORITHM: 1. Start the program 2. Define the structure of the symbol table 3. Enter the choice for performing the operations in the symbol table 4. If choice is 1, search symbol table for the symbol to be inserted. If the symbol is already present display Duplicate Symbol, else insert symbol and corresponding address in the symbol table 5. If choice is 2, symbols present in the symbols table are displayed 6. If choice is 3, symbol to be deleted is searched in the symbol table, if found deletes else displays Not Found. 7. If choice is 5, the symbol to be modified is searched in the symbol table. The label or address or both can be modified. PROGRAM: #include<stdio.h> #include<conio.h> #include<alloc.h> #include<string.h> #define null 0 int size=0;

void insert(); void del(); int search(char lab[]); void modify(); void display(); struct symbtab {char label[10]; int addr; struct symbtab *next; }; struct symbtab *first,*last; void main() {int op; int y; char la[10]; clrscr(); do {printf("\nSYMBOL TABLE IMPLEMENTATION\n"); printf("1.INSERT\n"); printf("2.DISPLAY\n"); printf("3.DELETE\n"); printf("4.SEARCH\n"); printf("5.MODIFY\n"); printf("6.END\n"); printf("\nEnter your option: "); scanf("%d",&op); switch(op) {case 1: insert(); display(); break; case 2: display(); break; case 3: del(); display(); break; case 4: printf("Enter the label to be searched: "); scanf("%s",la); y=search(la); if(y==1) printf("The label is already in the symbol table\n"); else

printf("The label is not found in the symbol tablel\n"); break; case 5: modify(); display(); break; case 6: break; }}while(op<6); getch(); }void insert() {int n; char l[10]; printf("Enter the label: "); scanf("%s",l); n=search(l); if(n==1) printf("The label is already in the symbol table. Duplicate cant be inserted\n"); else {struct symbtab *p; p=malloc(sizeof(struct symbtab)); strcpy(p->label,l); printf("Enter the address: "); scanf("%d",&p->addr); p->next=null; if(size==0) {first=p; last=p; }else {last->next=p; last=p; }size++; }}void display() {int i; struct symbtab *p; p=first; printf("LABEL\tADDRESS\n"); for(i=0;i<size;i++) {printf("%s\t%d\n",p->label,p->addr); p=p->next; }}int search(char lab[]) {int i,flag=0; struct symbtab *p; p=first; for(i=0;i<size;i++)

{if(strcmp(p->label,lab)==0) {flag=1; }p=p->next; }return flag; }void modify() {char l[10],nl[10]; int add,choice,i,s; struct symbtab *p; p=first; printf("What do you want to modify?\n"); printf("1.Only the label\n"); printf("2.Only the address of a particular label\n"); printf("3.Both the label and address\n"); printf("Enter your choice: "); scanf("%d",&choice); switch(choice) {case 1: printf("Enter the old label\n"); scanf("%s",l); printf("Enter the new label: "); scanf("%s",nl); s=search(l); if(s==0) printf("\nNo such label"); else {for(i=0;i<size;i++) {if(strcmp(p->label,l)==0) {strcpy(p->label,nl); }p=p->next; }}break; case 2: printf("Enter the label whose address is to be modified: "); scanf("%s",l); printf("Enter the new address: "); scanf("%d",&add); s=search(l); if(s==0) printf("\nNo such label"); else {for(i=0;i<size;i++) {if(strcmp(p->label,l)==0) {p->addr=add; }p=p->next; }}break;

case 3: printf("Enter the old label: "); scanf("%s",l); printf("Enter the new label: "); scanf("%s",nl); printf("Enter the new address: "); scanf("%d",&add); s=search(l); if(s==0) printf("\nNo such label"); else {for(i=0;i<size;i++) {if(strcmp(p->label,l)==0) {strcpy(p->label,nl); p->addr=add; }p=p->next; }}break; }}void del() {int a; char l[10]; struct symbtab *p,*q; p=first; printf("Enter the label to be deleted: "); scanf("%s",l); a=search(l); if(a==0) printf("Label not found\n"); else {if(strcmp(first->label,l)==0) first=first->next; else if(strcmp(last->label,l)==0) {q=p->next; while(strcmp(q->label,l)!=0) {p=p->next; q=q->next; }p->next=null; last=p; } else {q=p->next; while(strcmp(q->label,l)!=0) {p=p->next; q=q->next; }p->next=q->next;

}size--; }} OUTPUT: Enter the address: 1000 LABEL ADDRESS ADD 1000

SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END

Enter your option: 2 LABEL ADDRESS ADD 1000

SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY

6.END

Enter your option: 3 Enter the label to be deleted: ADD LABEL ADDRESS SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END Enter your option: 4 Enter the label to be searched: ADD The label is not found in the symbol tablel SYMBOL TABLE IMPLEMENTATION 1.INSERT 2.DISPLAY 3.DELETE 4.SEARCH 5.MODIFY 6.END

Enter your option: 6

EX NO:2 PASS ONE OF A TWO PASS ASSEMBLER

Implement a Pass I of Two Pass Assembler Aim: To write a C program to perform pass1 of a two assembler. Algorithm:

1. 2. 3. 4. 5. 6. 7. 8. 9.

Start the assembly process by processing the assembly program statements. Location counter is initialized to the starting address if OPCODE = "START" else if it is initialized to 0.It is incremented depending upon the size of next consecutive instruction. If there is any symbol present in lable field, location counter is entered into symbol table.The opcode table must be present in the memory during pass1. Search the OPTAB.If it is found add 3 to locctr. If not found in OPTAB, update the location value by checking whether the opcode is WORD,RESW,RESB or BYTE. Write the program line with locctr value to intermediate file. Read the next input line. Steps3 to 7 is carried out till the end statement is reached. Finally save the value of 1 locctr-starting address) and program length.

10. Stop.

11. .

#include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() {char opcode[10],mnemonic[10],operand[10],label[10],code[10]; int locctr,start,length; FILE *fp1,*fp2,*fp3,*fp4; clrscr();

fp1=fopen("input.dat","r"); fp2=fopen("symtab.dat","w"); fp3=fopen("out.dat","w"); fp4=fopen("optab.dat","r"); fscanf(fp1,"%s%s%s",label,opcode,operand); if(strcmp(opcode,"START")==0) {start=atoi(operand); locctr=start; fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand); }else locctr=0; while(strcmp(opcode,"END")!=0) {fprintf(fp3,"%d\t",locctr); if(strcmp(label,"**")!=0) fprintf(fp2,"%s\t%d\n",label,locctr); rewind(fp4); fscanf(fp4,"%s",mnemonic); while(strcmp(mnemonic,"END")!=0) {if(strcmp(opcode,mnemonic)==0) {locctr+=3; break; }fscanf(fp4,"%s",mnemonic); }if(strcmp(opcode,"WORD")==0) locctr+=3; else if(strcmp(opcode,"RESW")==0) locctr+=(3*(atoi(operand))); else if(strcmp(opcode,"RESB")==0) locctr+=(atoi(operand)); else if(strcmp(opcode,"BYTE")==0) ++locctr; fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand); fscanf(fp1,"%s%s%s",label,opcode,operand); }fprintf(fp3,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand); length=locctr-start; printf("\nThe length of the program is %d",length); fclose(fp1); fclose(fp2); fclose(fp3); fclose(fp4); getch(); }

INPUT: INPUT.DAT: MAIN START BEGIN LDA ** ADD ** LDCH ** STCH NUM1 WORD NUM2 RESW CHAR1 BYTE CHAR2 RESB ** END OPTAB.DAT: ADD 18 SUB 1C MUL 20 DIV 24 LDA 00 LDB 68 LDX 04 LDCH 50 STA 0C STB 78 STX 10 STCH 54 J 3C JSUB 48 RSUB 4C JEQ 30 JLT 38 JGT 34 START * END * SYMTAB.DAT: BEGIN 2000 NUM1 2012 NUM2 2015 CHAR1 2018 CHAR2 2019 2000 NUM1 NUM2 CHAR1 CHAR2 5 1 C'A' 1 BEGIN

OUTPUT: The length of the program is 20

You might also like