Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

this is a really old book so assistance from an experience coder would be prefer

ID: 3597737 • Letter: T

Question

this is a really old book so assistance from an experience coder would be preferable. I need assistance in writing this pass 1 assembler in C. I'm currently working on it in Linux and I can't quite get the hashing to display correctly. the output should look like somewhat like the image above the book. Will Rate. thanks in advance.

address Symbol Value LENGTH 100C ROREC 1000 4547 4600 00030000 00xxXXXX KEXXEXR 20130 ZERO 1006 0100 1810063048- 12 WRREC 201F 1000 0ice BUFFER CLOOP 2012 are d Of semb Pass 1: read first input line if OROODE STARTthen tand save #10PERAND] as starting address initialize LOOCTR write line to intermediate tile read next input line n F to starting address 18-21 and fif START) initialize LOCCTR to begin while oFcODE END' do if this is not a comment line then begin if there is a symbol in the LABEL field then begin search sYMIAB for LABEL if found then ng tools g and riai set error flag (duplicate symbol) else insert (LABEL. LOCCTR) into SYMPAB and (if symbol) search oPTAB for OPCODE if found then else if OPCODE= 'WORD' then else if OPCODE'RESW' then else if OPCODE RESB, then else if OPCODE 'BYTE' then add 3 finstruction length) to LOCCTR add 3 to LOOCTR add 3 * # [OPERAND) to LOCCTR add N[OPERAND] to LOCCTR begin RES n asse the ede bler thos find length of constant in bytes add length to LOCCTR and (if BYTE) else ght b the s write last line to intermediate file set error flag (invalid operation code) end fif not a comment) write line to intermediate file read next input line end (while not END) save (LOCCTR starting address) as program length end (Pass 1) Figure 2.4(a) Algorithm for Pass 1 of assembler

Explanation / Answer

//MAIN C program file

#include<stdio.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;

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 %s %s ",label,opcode,operand);

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

}

else

locctr=0;

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

{

fprintf(fp3,"%d ",locctr);

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

fprintf(fp2,"%s %d ",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 %s %s ",label,opcode,operand);

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

}

fprintf(fp3,"%d %s %s %s ",locctr,label,opcode,operand);

length=locctr-start;

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

fclose(fp1);

fclose(fp2);

fclose(fp3);

fclose(fp4);

}

Content of symtab.dat:

BEGIN 2000

NUM1 2012

NUM2 2015

CHAR1 2018

CHAR2 2019

Content of input.dat

MAIN START 2000
BEGIN LDA NUM1
** ADD NUM2
** LDCH CHAR1
** STCH CHAR2
NUM1 WORD 5
NUM2 RESW 1
CHAR1 BYTE C'A'
CHAR2 RESB 1
** END BEGIN

Content of 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 *

After compiling and running the program out.dat file will be created whose content will look like this:

MAIN START 2000

2000 BEGIN LDA NUM1

2003 ** ADD NUM2

2006 ** LDCH CHAR1

2009 ** STCH CHAR2

2012 NUM1 WORD 5

2015 NUM2 RESW 1

2018 CHAR1 BYTE C'A'

2019 CHAR2 RESB 1

2020 ** END BEGIN