Experiment 11 Implementation of Pass 1 of 2 Pass Assembler: Description of The Problem

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Experiment 11

Implementation of Pass 1 of 2 Pass Assembler

Aim: To implement pass 1 of pass 2 assembler

Description of the problem:

The input file INPUT.DAT contains the assembly language statements. The OPTAB.DAT file
contains the opcodes. The memory addresses of statements as well as the statements should be
written into the file OUT.DAT. The symbols and addresses should be written to SYMTAB.DAT

Algorithm

1. Read first input line


2. If opcode = 'START'
2.1 save operand as starting address
2.2 initialize locctr to starting address
2.3 write line to intermediate file
2.4 read next line from input file
else
2.5 locctr = 0
3. while opcode != 'END'
3.1 if there is a symbol in the label field
3.1.1 insert (label, locctr) into SYMTAB.DAT
3.2 Search OPTAB.DAT for opcode
3.3 If found
add 3 to locctr
else if opcode == WORD
add 3 to locctr
else if opcode == RESW
add 3 * [operand] to locctr
else if opcode == RESB
add [operand] to locctr
else if opcode == BYTE
find length of constant in bytes
add length to locctr
3.4 write line to intermediate file
3.5 read next input line
4. write last line to intermediate file
5. save locctr – starting address as program length

Program

#include<stdio.h>
#include<string.h>

void main()
{
char opcode[10],operand[10],label[10], regstr[20];
int locctr,start,len,i=0,j=0;
char otp[20];
int o;
char * ptr;

FILE *fp1,*fp2,*fp3, *fp4 ;

fp1=fopen("INPUT.DAT","r");
fp2=fopen("SYMTAB.DAT","w");
fp3=fopen("OUT.DAT","w");

fscanf(fp1,"%s%s%s",label,opcode,operand);

if(strcmp(opcode,"START")==0)
{
start = (int)strtol(operand, NULL, 16);
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,"%0x",locctr);

if(strcmp(label,"-")!=0)
{

fprintf(fp2,"%s\t%0x\n",label,locctr);
}

fp4=fopen("OPTAB.DAT","r");
fscanf(fp4, "%s%d", otp,&o);

while(!feof(fp4))
{
// missing code
}

fclose(fp4);

// missing code

fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s%s%s",label,opcode,operand);
}
fprintf(fp3,"%x\t%s\t%s\t%s\n",locctr, "-", "END", "-");

printf("length of the program = %x\n", locctr - start);


}
Input ( Taken from System Software by Leland L. Beck- pg 47)

INPUT.DAT

- START 1000
FIRST STL RETADR
CLOOP JSUB RDREC
- LDA LENGTH
- COMP ZERO
- JEQ ENDFIL
- JSUB WRREC
- J CLOOP
ENDFIL LDA EOF
- STA BUFFER
- LDA THREE
- STA LENGTH
- JSUB WRREC
- LDL RETADR
- RSUB -
EOF BYTE C'EOF'
THREE WORD 3
ZERO WORD 0
RETADR RESW 1
LENGTH RESW 1
BUFFER RESB 4096
----------------------------------------------------- (Subroutine to read record from buffer)----------
RDREC LDX ZERO
- LDA ZERO
RLOOP TD INPUT
- JEQ RLOOP
- RD INPUT
- COMP ZERO
- JEQ EXIT
- STCH BUFFER,X
- TIX MAXLEN
- JLT RLOOP
EXIT STX LENGTH
- RSUB -
INPUT BYTE X'F1'
MAXLEN WORD 4096
--------------------------------------------------- (Subroutine to write record from buffer)-------------
WRREC LDX ZERO
WLOOP TD OUTPUT
- JEQ WLOOP
- LDCH BUFFER,X
- WD OUTPUT
- TIX LENGTH
- JLT WLOOP
- RSUB -
OUTPUT BYTE X'05'
- END -

OPTAB.DAT

STL 14
JSUB 48
LDA 00
COMP 28
JEQ 30
J 3C
STA 0C
LDL 08
RSUB 4C
LDX 04
TD E0
COMP 28
RD D8
STCH 54
TIX 2C
JLT 38
STX 10
LDCH 50
WD DC

To Do

Complete the missing code by looking into the algorithm given. Store the input assembly program
in INPUT.DAT file. Using the above program, input file INPUT.DAT and OPTAB.DAT which
contains the operation codes and machine language equivalents, obtain the intermediate file which
is stored in OUT.DAT and the file SYMTAB.DAT which contains the symbols and its addresses.
All addresses are in hexadecimal.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy