two pass assembler

20
WALCHAND INSTITUTE OF TECHNOLOGY, SOLAPUR PRESENTING A SEMINAR ON TWO PASS ASSEMBLERS

Upload: satyamevjayte-haxor

Post on 14-Jun-2015

4.133 views

Category:

Software


9 download

DESCRIPTION

Its an complete presentation of how two pass assembler works,two pass assembler program,comparison between one pass and two pass.

TRANSCRIPT

Page 1: Two pass Assembler

WALCHAND INSTITUTE OF TECHNOLOGY, SOLAPUR

PRESENTING A SEMINAR ON

TWO PASS ASSEMBLERS

Page 2: Two pass Assembler

TWO PASS ASSEMBLER

• Processing the source program into two passes.

• The internal tables and subroutines that are used only during Pass 1.

• The SYMTAB, LITTAB, and OPTAB are used by both passes.

• The main problems to assemble a program in one pass involves forward references.

Pass 1

Forward references table String storage buffer Partially configured object file

Assembly Language

Pass 2 MachineLanguage

Page 3: Two pass Assembler

• PASS 1

• Assign addresses to all statements in the program.

• Addresses of symbolic labels are stored.

• Some assemble directives will be processed.

• PASS 2

• Translate opcode and symbolic operands.

• Generate data values defined by BYTE,WORD etc.

• Assemble directives will be processed.

• Write the object program and assembly listing.

Page 4: Two pass Assembler

DATA STRUCTURES

• Two major data structures:• Operation Code Table (OPTAB): is used to look up mnemonic operation

codes and translate them to their machine language equivalents

• Symbol Table (SYMTAB): is used to store values (addresses) assigned to labels

• Variable:• Location Counter (LOCCTR) is used to help the assignment of addresses

• LOCCTR is initialized to the beginning address specified in the START statement

• The length of the assembled instruction or data area to be generated is added to LOCCTR

Page 5: Two pass Assembler

Algorithm for Pass 1 of Assembler(3/1)

read first input lineif OPCODE=‘START’ then

begin save #[OPERAND] as starting address initialize LOCCTR to starting address write line to intermediate file read next input line

endelse

initialize LOCCTR to 0while OPCODE≠’END’ do

begin if this is not a comment line then

begin if there is a symbol in the LABEL field then

Page 6: Two pass Assembler

Algorithm for Pass 1 of Assembler(3/2)

begin search SYMTAB for LABEL if found then set error flag (duplicate symbol) else insert (LABEL, LOCCTR) into

SYMTABend {if symbol}

search OPTAB for OPCODE if found then

add 3 {instruction length} to LOCCTR else if OPCODE=‘WORD’ then

add 3 to LOCCTR else if OPCODE=‘RESW’ then

add 3 * #[OPERAND] to LOCCTR

Page 7: Two pass Assembler

Algorithm for Pass 1 of Assembler(3/3)

else if OPCODE=‘RESB’ thenadd #[OPERAND] to LOCCTR

else if OPCODE=‘BYTE’ thenbegin find length of constant in

bytes add length to LOCCTRend {if BYTE}

elseset error flag (invalid operation

code)end {if not a comment}

write line to intermediate file read next input lineend {while not END}

Write last line to intermediate fileSave (LOCCTR-starting address) as program length

Page 8: Two pass Assembler

Algorithm for Pass 2 of Assembler(3/1)

read first input line (from intermediate file)If OPCODE=‘START’ then begin

write listing lineread next input line

end {if START}Write Header record to object programInitialize first Text recordWhile OPCODE≠ ‘END’ do begin

if this is not a comment line then begin

search OPTAB for OPCODEif found then begin

Page 9: Two pass Assembler

Algorithm for Pass 2 of Assembler(3/2)

if there is a symbol in OPERAND field then begin

search SYMTAB for OPERANDif found then store symbol value as operand

addresselse begin store 0 as operand address set error flag (undefined

symbol) end

end {if symbol} else store 0 as operand address assemble the object code instruction end {if opcode found}

Page 10: Two pass Assembler

Algorithm for Pass 2 of Assembler(3/3)

else if OPCODE=‘BYTE’ or ‘WORD’ then convert constant to object codeif object code will not fit into the current Text record then begin write Text record to object program initialize new Text record endadd object code to Text record

end {if not comment}write listing lineread next input line

end {while not END}write last Text record to object programWrite End record to object programWrite last listing line

Page 11: Two pass Assembler

#include<stdio.h>#include<string.h>#include<conio.h>void main(){ char *code[9][4]={

{"PRG1","START","",""}, {"","USING","*","15"}, {"","L","",""}, {"","A","",""}, {"","ST","",""}, {"FOUR","DC","F",""}, {"FIVE","DC","F",""}, {"TEMP","DS","1F",""}, {"","END","",""}

}; char av[2],avail[15]={'N','N','N','N','N','N','N','N','N','N','N','N','N','N','N'}; int i,j,k,count[3],lc[9]={0,0,0,0,0,0,0,0,0},loc=0; clrscr();

Program

Page 12: Two pass Assembler

printf("----------------------------------------------------\n"); printf("LABEL\t\tOPCODE\n"); printf("----------------------------------------------------\n\n"); for(i=0;i<=8;i++) { for(j=0;j<=3;j++) { printf("%s\t\t",code[i][j]); } j=0; printf("\n"); } getch(); printf("-----------------------------------------------------"); printf("\nVALUES FOR LC : \n\n"); for(j=0;j<=8;j++) {if((strcmp(code[j][1],"START")!=0)&&(strcmp(code[j][1],"USING")!=0)&&(strcmp(code[j][1],"L")!=0)) lc[j]=lc[j-1]+4; printf("%d\t",lc[j]); }

Page 13: Two pass Assembler

printf("\n\nSYMBOL TABLE:\n----------------------------------------------------\n"); printf("SYMBOL\t\tVALUE\t\tLENGTH\t\tR/A"); printf("\n----------------------------------------------------\n"); for(i=0;i< 9;i++) { if(strcmp(code[i][1],"START")==0) { printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][0],loc,4,'R'); } else if(strcmp(code[i][0],"")!=0) { printf("%s\t\t%d\t\t%d\t\t%c\n",code[i][0],loc,4,'R'); loc=4+loc; } else if(strcmp(code[i][1],"USING")==0) { } else { loc=4+loc; } }

Page 14: Two pass Assembler

printf("----------------------------------------------------");

printf("\n\nBASE TABLE:\n-------------------------------------------------------\n"); printf("REG NO\t\tAVAILIBILITY\tCONTENTS OF BASE TABLE"); printf("\n-------------------------------------------------------\n"); for(j=0;j<=8;j++) { if(strcmp(code[j][1],"USING")!=0) { } else { strcpy(av,code[j][3]); } } count[0]=(int)av[0]-48; count[1]=(int)av[1]-48; count[2]=count[0]*10+count[1]; avail[count[2]-1]='Y';

Page 15: Two pass Assembler

for(k=0;k< 16;k++) { printf(" %d\t\t %c\n",k,avail[k-1]); } printf("-------------------------------------------------------\n"); printf("Continue..??"); getchar(); printf("PASS2 TABLE:\n\n"); printf("LABEL\t\tOP1\t\tLC\t\t"); printf("\n----------------------------------------------------\n"); loc=0; for(i=0;i<=8;i++) { for(j=0;j<=3;j++) { printf("%s\t\t",code[i][j]); } j=0; printf("\n"); } printf("-----------------------------------------------------"); getch(); }

Page 16: Two pass Assembler

----------------------------------------------------LABEL OPCODE----------------------------------------------------PRG1 STARTUSING * 15LASTFOUR DC FFIVE DC FTEMP DS 1FEND-----------------------------------------------------VALUES FOR LC :0 0 0 4 8 12 16 20 24

OUTPUT

Page 17: Two pass Assembler

SYMBOL TABLE:----------------------------------------------------SYMBOL VALUE LENGTH R/A----------------------------------------------------

PRG1 0 4 RFOUR 12 4 RFIVE 16 4 RTEMP 20 4 R

----------------------------------------------------

Page 18: Two pass Assembler

BASE TABLE:-------------------------------------------------------REG NO AVAILIBILITY CONTENTS OF BASE TABLE-------------------------------------------------------0 1 N2 N3 N4 N5 N6 N7 N8 N9 N10 N11 N12 N13 N14 N15 Y

Page 19: Two pass Assembler

Continue..??PASS2 TABLE:

LABEL OP1 LC----------------------------------------------------PRG1 STARTUSING * 15LASTFOUR DC FFIVE DC FTEMP DS 1FEND-----------------------------------------------------

Page 20: Two pass Assembler

Any Queries ???Thank You ……….