SlideShare a Scribd company logo
SEMINAR ON 
“SINGLE PASS 
ASSEMBLERS”
ASSEMBLER OVERVIEW 
•TRANSLATE ASSEMBLY LANGUAGE 
PROGRAMS TO OBJECT PROGRAMS OR 
MACHINE CODE IS CALLED AN 
ASSEMBLER.
ASSEMBLERS 
• AN ASSEMBLER IS A PROGRAM THAT ACCEPTS AS INPUT AN ASSEMBLY 
LANGUAGE PROGRAM (SOURCE) AND PRODUCES ITS MACHINE LANGUAGE 
EQUIVALENT (OBJECT CODE) ALONG WITH THE INFORMATION FOR THE 
LOADER. 
Assembler 
Object 
Code 
Loader 
Executable 
Linker Code 
Fig. : Role of Assemblers
AN ASSEMBLER DOES THE FOLLOWING: 
1. GENERATE MACHINE INSTRUCTIONS 
• EVALUATE THE MNEMONICS TO PRODUCE THEIR MACHINE 
CODE 
• EVALUATE THE SYMBOLS, LITERALS, ADDRESSES TO 
PRODUCE THEIR EQUIVALENT MACHINE ADDRESSES 
• CONVERT THE DATA CONSTANTS INTO THEIR MACHINE 
REPRESENTATIONS
ONE PASS ASSEMBLERS 
• ONE-PASS ASSEMBLERS ARE USED WHEN 
• IT IS NECESSARY OR DESIRABLE TO AVOID A SECOND PASS 
OVER THE SOURCE PROGRAM 
• THE EXTERNAL STORAGE FOR THE INTERMEDIATE FILE 
BETWEEN TWO PASSES IS SLOW OR IS INCONVENIENT TO 
USE 
• MAIN PROBLEM 
 FORWARD REFERENCES 
• DATA ITEMS 
• LABELS ON INSTRUCTIONS
FORWARD REFERENCE 
• FOR ANY SYMBOL THAT HAS NOT YET BEEN DEFINED 
1. OMIT THE ADDRESS TRANSLATION 
2. INSERT THE SYMBOL INTO SYMTAB, AND MARK THIS SYMBOL UNDEFINED 
3. THE ADDRESS THAT REFERS TO THE UNDEFINED SYMBOL IS ADDED TO A 
LIST OF FORWARD REFERENCES ASSOCIATED WITH THE SYMBOL TABLE 
ENTRY 
4. WHEN THE DEFINITION FOR A SYMBOL IS ENCOUNTERED, THE PROPER 
ADDRESS FOR THE SYMBOL IS THEN INSERTED INTO ANY INSTRUCTIONS 
PREVIOUS GENERATED ACCORDING TO THE FORWARD REFERENCE LIST
SOLUTIONS FOR ONE PASS ASSEMBLER 
TWO METHODS CAN BE USED: 
• ELIMINATING FORWARD REFERENCES: 
EITHER ALL LABELS USED IN FORWARD REFERENCES ARE DEFINED IN THE SOURCE 
PROGRAM BEFORE THEY ARE REFERENCED, OR FORWARD REFERENCES TO DATA ITEMS 
ARE PROHIBITED. 
• GENERATING THE OBJECT CODE IN MEMORY: 
NO OBJECT PROGRAM IS WRITTEN OUT AND NO LOADER IS NEEDED. THE PROGRAM NEEDS 
TO BE RE-ASSEMBLED EVERY TIME.
ALGORITHM 
STEP 1 : START THE PROGRAM EXECUTION. 
STEP 2 : ASSEMBLER SIMPLY GENERATE OBJECT CODE AS IT SCANS THE SOURCE CODE. 
STEP 3 : IF THE INSTRUCTION OPERAND IS A SYMBOL TEXT, HAS NOT YET BEEN DEFINED, THE 
OPERANDS ADDRESS IS OMITTED. 
STEP 4 : WHEN NEARLY HALF THE PROGRAM TRANSLATION IS OVER, SOME OF THE FORWARD 
REFERENCE PROBLEM ARE EXISTED. 
STEP 5 : COMBINE THE PROCESS TO THE END OF THE PROGRAM TO FILL FORWARD REFERENCE 
PROPERTY. 
STEP 6 : AT THE END OF THE PROGRAM, THE SYMBOL TABLE ENTRIES WITH ‘X’ ARE 
UNDEFINED. 
STEP 7 : STOP THE PROGRAM EXECUTION.
Single Pass Assembler
TO WRITE A C PROGRAM TO IMPLEMENT SINGLE 
PASS ASSEMBLER. 
INPUT 
ENTER YOUR ASSEMBLY INSTRUCTIONS 
LDA 5000 
SUB Z 
STA 9988 
HLT 
*
PROGRAM 
#INCLUDE<STDIO.H> 
#INCLUDE<CONIO.H> 
#INCLUDE<STDLIB.H> 
VOID MAIN() 
{ 
FILE *F1,*F2; 
CHAR CH,STR[30],STR1[10],STR2[30],CSTR[15];INT I,J,NUM,Q,R; 
CLRSCR(); 
PRINTF("ENTER YOUR ASSEMBLY INSTRUCTIONSN"); 
F1=FOPEN("ASIN","W"); 
WHILE(1) 
{ CH=GETCHAR(); 
IF(CH=='*') 
BREAK; 
FPUTC(CH,F1); 
} 
FCLOSE(F1); 
F1=FOPEN("ASIN","R"); 
F2=FOPEN("ASOUT","W");
WHILE(1) 
{ CH=GETCHAR(); 
IF(CH=='*') 
BREAK; 
FPUTC(CH,F1); 
} 
FCLOSE(F1); 
F1=FOPEN("ASIN","R"); 
F2=FOPEN("ASOUT","W"); 
WHILE(1) 
{ 
FGETS(STR,25,F1); 
STRNCPY(STR1,STR,3); 
STR1[3]='0'; 
J=0
FOR(I=3;I<STRLEN(STR);I++) 
{S 
TR2[J]=STR[I]; 
J++; 
}S 
TR2[J]='0'; 
IF((STRCMP(STR1,"LDA"))==0) 
{ 
FPUTS("3AT",F2); 
FPUTS(STR2,F2); 
} ELSE IF((STRCMP(STR1,"MOV"))==0) 
{ 
FPUTS("47N",F2); 
} ELSE IF((STRCMP(STR1,"ADD"))==0) 
{ 
FPUTS("80N",F2);
} ELSE IF((STRCMP(STR1,"SUB"))==0) 
{ 
FPUTS("90N",F2); 
} ELSE IF((STRCMP(STR1,"HLT"))==0) 
{ 
FPUTS("76N",F2); BREAK; 
} ELSE IF((STRCMP(STR1,"STA"))==0) 
{ 
FPUTS("32T",F2); 
NUM=ATOI(STR2); Q=NUM/100; R=NUM%100; 
IF(R==0) 
FPUTS("00T",F2); 
ELSE 
FPUTS(ITOA(R,CSTR,10),F2); FPUTS("T",F2); 
FPUTS(ITOA(Q,CSTR,10),F2); 
FPUTS("N",F2); 
} ELSE 
{ 
FPUTS("ERRORN",F2); 
}} 
FCLOSE(F1); 
FCLOSE(F2); 
F2=FOPEN("ASOUT","R"); 
PRINTF("NTHE OBJECT CODE 
CONTENTSN"); 
CH=FGETC(F2); 
WHILE(CH!=EOF) 
{ 
PUTCHAR(CH); 
CH=FGETC(F2); 
} 
FCLOSE(F2); 
GETCH(); 
}
INPUT OUTPUT 
ENTER YOUR ASSEMBLY INSTRUCTIONS THE OBJECT CODE CONTENTS 
LDA 5000 3A 00 50 
SUB Z 90 
STA 9988 32 88 99 
HLT 76
Single Pass Assembler

More Related Content

Single Pass Assembler

  • 1. SEMINAR ON “SINGLE PASS ASSEMBLERS”
  • 2. ASSEMBLER OVERVIEW •TRANSLATE ASSEMBLY LANGUAGE PROGRAMS TO OBJECT PROGRAMS OR MACHINE CODE IS CALLED AN ASSEMBLER.
  • 3. ASSEMBLERS • AN ASSEMBLER IS A PROGRAM THAT ACCEPTS AS INPUT AN ASSEMBLY LANGUAGE PROGRAM (SOURCE) AND PRODUCES ITS MACHINE LANGUAGE EQUIVALENT (OBJECT CODE) ALONG WITH THE INFORMATION FOR THE LOADER. Assembler Object Code Loader Executable Linker Code Fig. : Role of Assemblers
  • 4. AN ASSEMBLER DOES THE FOLLOWING: 1. GENERATE MACHINE INSTRUCTIONS • EVALUATE THE MNEMONICS TO PRODUCE THEIR MACHINE CODE • EVALUATE THE SYMBOLS, LITERALS, ADDRESSES TO PRODUCE THEIR EQUIVALENT MACHINE ADDRESSES • CONVERT THE DATA CONSTANTS INTO THEIR MACHINE REPRESENTATIONS
  • 5. ONE PASS ASSEMBLERS • ONE-PASS ASSEMBLERS ARE USED WHEN • IT IS NECESSARY OR DESIRABLE TO AVOID A SECOND PASS OVER THE SOURCE PROGRAM • THE EXTERNAL STORAGE FOR THE INTERMEDIATE FILE BETWEEN TWO PASSES IS SLOW OR IS INCONVENIENT TO USE • MAIN PROBLEM  FORWARD REFERENCES • DATA ITEMS • LABELS ON INSTRUCTIONS
  • 6. FORWARD REFERENCE • FOR ANY SYMBOL THAT HAS NOT YET BEEN DEFINED 1. OMIT THE ADDRESS TRANSLATION 2. INSERT THE SYMBOL INTO SYMTAB, AND MARK THIS SYMBOL UNDEFINED 3. THE ADDRESS THAT REFERS TO THE UNDEFINED SYMBOL IS ADDED TO A LIST OF FORWARD REFERENCES ASSOCIATED WITH THE SYMBOL TABLE ENTRY 4. WHEN THE DEFINITION FOR A SYMBOL IS ENCOUNTERED, THE PROPER ADDRESS FOR THE SYMBOL IS THEN INSERTED INTO ANY INSTRUCTIONS PREVIOUS GENERATED ACCORDING TO THE FORWARD REFERENCE LIST
  • 7. SOLUTIONS FOR ONE PASS ASSEMBLER TWO METHODS CAN BE USED: • ELIMINATING FORWARD REFERENCES: EITHER ALL LABELS USED IN FORWARD REFERENCES ARE DEFINED IN THE SOURCE PROGRAM BEFORE THEY ARE REFERENCED, OR FORWARD REFERENCES TO DATA ITEMS ARE PROHIBITED. • GENERATING THE OBJECT CODE IN MEMORY: NO OBJECT PROGRAM IS WRITTEN OUT AND NO LOADER IS NEEDED. THE PROGRAM NEEDS TO BE RE-ASSEMBLED EVERY TIME.
  • 8. ALGORITHM STEP 1 : START THE PROGRAM EXECUTION. STEP 2 : ASSEMBLER SIMPLY GENERATE OBJECT CODE AS IT SCANS THE SOURCE CODE. STEP 3 : IF THE INSTRUCTION OPERAND IS A SYMBOL TEXT, HAS NOT YET BEEN DEFINED, THE OPERANDS ADDRESS IS OMITTED. STEP 4 : WHEN NEARLY HALF THE PROGRAM TRANSLATION IS OVER, SOME OF THE FORWARD REFERENCE PROBLEM ARE EXISTED. STEP 5 : COMBINE THE PROCESS TO THE END OF THE PROGRAM TO FILL FORWARD REFERENCE PROPERTY. STEP 6 : AT THE END OF THE PROGRAM, THE SYMBOL TABLE ENTRIES WITH ‘X’ ARE UNDEFINED. STEP 7 : STOP THE PROGRAM EXECUTION.
  • 10. TO WRITE A C PROGRAM TO IMPLEMENT SINGLE PASS ASSEMBLER. INPUT ENTER YOUR ASSEMBLY INSTRUCTIONS LDA 5000 SUB Z STA 9988 HLT *
  • 11. PROGRAM #INCLUDE<STDIO.H> #INCLUDE<CONIO.H> #INCLUDE<STDLIB.H> VOID MAIN() { FILE *F1,*F2; CHAR CH,STR[30],STR1[10],STR2[30],CSTR[15];INT I,J,NUM,Q,R; CLRSCR(); PRINTF("ENTER YOUR ASSEMBLY INSTRUCTIONSN"); F1=FOPEN("ASIN","W"); WHILE(1) { CH=GETCHAR(); IF(CH=='*') BREAK; FPUTC(CH,F1); } FCLOSE(F1); F1=FOPEN("ASIN","R"); F2=FOPEN("ASOUT","W");
  • 12. WHILE(1) { CH=GETCHAR(); IF(CH=='*') BREAK; FPUTC(CH,F1); } FCLOSE(F1); F1=FOPEN("ASIN","R"); F2=FOPEN("ASOUT","W"); WHILE(1) { FGETS(STR,25,F1); STRNCPY(STR1,STR,3); STR1[3]='0'; J=0
  • 13. FOR(I=3;I<STRLEN(STR);I++) {S TR2[J]=STR[I]; J++; }S TR2[J]='0'; IF((STRCMP(STR1,"LDA"))==0) { FPUTS("3AT",F2); FPUTS(STR2,F2); } ELSE IF((STRCMP(STR1,"MOV"))==0) { FPUTS("47N",F2); } ELSE IF((STRCMP(STR1,"ADD"))==0) { FPUTS("80N",F2);
  • 14. } ELSE IF((STRCMP(STR1,"SUB"))==0) { FPUTS("90N",F2); } ELSE IF((STRCMP(STR1,"HLT"))==0) { FPUTS("76N",F2); BREAK; } ELSE IF((STRCMP(STR1,"STA"))==0) { FPUTS("32T",F2); NUM=ATOI(STR2); Q=NUM/100; R=NUM%100; IF(R==0) FPUTS("00T",F2); ELSE FPUTS(ITOA(R,CSTR,10),F2); FPUTS("T",F2); FPUTS(ITOA(Q,CSTR,10),F2); FPUTS("N",F2); } ELSE { FPUTS("ERRORN",F2); }} FCLOSE(F1); FCLOSE(F2); F2=FOPEN("ASOUT","R"); PRINTF("NTHE OBJECT CODE CONTENTSN"); CH=FGETC(F2); WHILE(CH!=EOF) { PUTCHAR(CH); CH=FGETC(F2); } FCLOSE(F2); GETCH(); }
  • 15. INPUT OUTPUT ENTER YOUR ASSEMBLY INSTRUCTIONS THE OBJECT CODE CONTENTS LDA 5000 3A 00 50 SUB Z 90 STA 9988 32 88 99 HLT 76