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 pad(int n, int w)
{
String zeros ="";
// determine length of 'n'
len = String.valueof(n).length()
use len to calculate #zeros
// calculate number of leading 0's to pad with
for(numzeros, i++)
{
zero=zero+"0";
}
return (zero+n)
// create and return string of leading 0's, and 'n'
}
static void readSrc(String fname) throws IOException
{
// open BufferedReader FileIO.java
BufferedReader br = new BufferedReader(new FileReader(fname));
String buffer;
//for each line of assembly code:
{
// skip any lines of length zero or starting with '#'
while ((buffer = br.readLine())!= null){
System.out.println("["+ buffer +"]");
// Call ST tokenize string class. 1st token is operator, 2nd is operand
???= new ST(buffer)
// If operand is invalid, display error and abort.
// If operator is invalid, display error and abort.
VAR1= stok.next();
VAR2= stok.next();
!VAR2= parseInt...
print VAR1 and VAR2
if(operator == "HALT")opcode =0
else if(operator == "ADD")opcode=1
else if(operator == "SUB")opcode=3
...
else print an error message and break/return/exit...
operator = "MULT";
String operator[]={"HALT", "ADD", "SUB", "MLT", "DIV","ILOAD", "LOAD", "STOR", "READ"...
//Indexs: 012345678
looping variable: i
to conver operator to numeric opcode:
opcode =-1
loop though all of the operator
{
if we find it, we assign opcode variable
}
//....println(pad(opcode,2)+ pad(ioperand,2))
pad opcode and operand
}
}
// convert string operator to numeric form.
// output opcode and operand, each padded to two characters.
}
}
public static void main(String argv[]) throws IOException
{
if (argv.length !=1)
{
System.out.println("usage: java Assemble INPUTFILE");
System.exit(0);
}
readSrc(argv[0]);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!