Question: import java.io . * ; import java.util. * ; public class Assemble { public static String pad ( int n , int w ) {
import java.io;
import java.util.;
public class Assemble
public static String padint n int w
String zeros ;
determine length of n
len String.valueofnlength
use len to calculate #zeros
calculate number of leading s to pad with
fornumzeros i
zerozero;
return zeron
create and return string of leading s and n
static void readSrcString fname throws IOException
open BufferedReader FileIO.java
BufferedReader br new BufferedReadernew FileReaderfname;
String buffer;
for each line of assembly code:
skip any lines of length zero or starting with #
while buffer brreadLine null
System.out.println buffer ;
Call ST tokenize string class. st token is operator, nd is operand
new STbuffer
If operand is invalid, display error and abort.
If operator is invalid, display error and abort.
VAR stok.next;
VAR stok.next;
VAR parseInt...
print VAR and VAR
ifoperator "HALT"opcode
else ifoperator "ADD"opcode
else ifoperator "SUB"opcode
else print an error message and breakreturnexit
operator "MULT";
String operatorHALT "ADD", "SUB", MLT "DIV","ILOAD", "LOAD", "STOR", "READ"...
Indexs:
looping variable: i
to conver operator to numeric opcode:
opcode
loop though all of the operator
if we find it we assign opcode variable
printlnpadopcode padioperand
pad opcode and operand
convert string operator to numeric form.
output opcode and operand, each padded to two characters.
public static void mainString argv throws IOException
if argvlength
System.out.printlnusage: java Assemble INPUTFILE";
System.exit;
readSrcargv;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
