1
votes

I want to design a simple assembler for IBM360 assembly language.so i'm implementing symbol table first. i'm storing my symbols/labels in a separate file so as to compare it while generating the symbol table.the problem im facing is ,the incorrect location counter(LC) value due to unwanted comparisions.I'm able to detect the symbols but with the wrong LC value. can anyone guide me in modifying my code?

Here is my program :

import java.io.*; 
import java.lang.*; 

class SymbolTable
 { 
    public static void main(String args[]) throws Exception 
    { 
    FileReader fr = new FileReader("program.asm"); 
    BufferedReader br = new BufferedReader(fr); 
    String s,l; 
    String code[]=new String[100];
    String label[]=new String[100];

    int N=0,i,LOC=0,n=0,j;
    System.out.println("Assembly lang program :\n--------------------------");
    while((s = br.readLine()) != null)
    {
        code[N++]=s;
        System.out.println(s); 
    } 
    fr.close();
    FileReader labels = new FileReader("label.txt"); 
    BufferedReader buff = new BufferedReader(labels); 
    while((s = buff.readLine()) != null)
    {
        label[n++]=s;
    } 
    labels.close();
    System.out.println("\n\n SYMBOL TABLE :\n-------------------------------------------\nLABEL\tLC\tLENGTH\tRELATIVE/ABSOLUTE\n-------------------------------------------");
    for(i=0;i<N;i++)
    {
        for(j=0;j<n;j++)
        {           
                char codearr[]=new char[15];
                codearr=code[i].toCharArray();
                if(code[i].startsWith("USING"))
                {
                 break;
                }
                else if(code[i].startsWith(label[j]))
                {
                    System.out.println(label[j]+"\t"+LOC+"\t4\tR");
                    if(i==0)
                    {}
                    else
                    LOC=LOC+4;
                    break;                  
                }
                else if(codearr[1]=='R')   // for register addressing mode
                    LOC=LOC+2;
                else
                    LOC=LOC+4;
        }   
    }
    } 
 }

program.asm:

JOHN START 
USING *,15
L 1,FIVE
A 1,FOUR
ST 1,TEMP
FOUR DC F '4'
FIVE DC F '5'
TEMP DS 1F
END

label.txt

JOHN
FOUR 
FIVE
TEMP

output :

G:\programs>javac SymbolTable.java
G:\programs>java SymbolTable
Assembly lang program :
--------------------------
JOHN START
USING *,15
LR 1,FIVE
A 1,FOUR
ST 1,TEMP
FOUR DC F '4'
FIVE DC F '5'
TEMP DS 1F
END

 SYMBOL TABLE :
-------------------------------------------
LABEL   LC      LENGTH  RELATIVE/ABSOLUTE
-------------------------------------------
JOHN    0       4       R
FOUR    44      4       R
FIVE    56      4       R
TEMP    72      4       R
1
I suggest that you move out of the internal loop everything that is not dependent on j.PM 77-1
@PM77-1 if i will move out of the internal loop ,then how i can do the rest of the comparisons ???chinu
If I understand correctly, only one condition requires the use of inner loop. So put the loop inside that particular else if. Does it make sense or have I missed something here?PM 77-1
thank you for the help. yes, you are right. i corrected my code as you said. @PM77-1chinu

1 Answers

1
votes

Here's an example to help you see your way through. I'm not aware of the language ins and outs, but based on the input you provided, this example should help. It builds the symbol table from labels matched at the beginning of the opcode, which I assume is the correct syntax for demarcating code sections associated with a label. The code makes assumptions and doesn't check against possible conflicts, but you can figure all that out. Feel free to build off it.

Input

The exact input you provided.

Output

JOHN    0   true
FOUR    50  true
TEMP    78  true
FIVE    64  true

Code

import java.io.File;
import java.io.IOException;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

class SymbolTableBuilder
{
    public static SymbolTable build(String asm, String lbls)
    {
        return build(asm.split("\n"), lbls.split("\n"));
    }

    public static SymbolTable build(String[] asm, String[] lbls)
    {
        return build(Arrays.asList(asm), Arrays.asList(lbls));
    }

    public static SymbolTable build(File asm, File lbls)
    {
        //TODO
        return null;
    }

    public static SymbolTable build(List<String> asm, List<String> lbls)
    {
        SymbolTable tbl = new SymbolTable();
        int pos = 0;
        for (String opCode : asm) {
            String tok = opCode.split("\\s+")[0];
            if (lbls.contains(tok))
                tbl.addSymbol(tok, new Symbol(tok, pos, true));
            pos += opCode.length() + 1; //account for newline
        }
        return tbl;
    }

    public static void main(String[] args) throws IOException
    {
        final String asm
            = "JOHN START\nUSING *,15\nL 1,FIVE\nA 1,FOUR\n"
            + "ST 1,TEMP\nFOUR DC F '4'\nFIVE DC F '5'\nTEMP DS 1F\nEND";

        final String lbls = "JOHN\nFOUR\nFIVE\nTEMP";

        for (Entry<String, Symbol> e : SymbolTableBuilder.build(asm, lbls))
            System.out.println(e.getValue());
    }

}

class SymbolTable implements Iterable<Entry<String, Symbol>>
{
    private Map<String, Symbol> syms;

    public SymbolTable()
    {
        syms = new HashMap<>();
    }

    public void addSymbol(String key, Symbol sym)
    {
        syms.put(key, sym);
    }

    public Symbol getSymbol(String key)
    {
        return syms.get(key);
    }

    @Override
    public Iterator<Entry<String, Symbol>> iterator()
    {
        return syms.entrySet().iterator();
    }
}

class Symbol
{
    String label;
    int loc;
    boolean relative;

    public Symbol(String label, int loc, boolean relative)
    {
        this.label = label;
        this.loc = loc;
        this.relative = relative;
    }

    @Override
    public int hashCode()
    {
        int hash = 3;
        hash = 29 * hash + Objects.hashCode(this.label);
        return hash;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final Symbol other = (Symbol) obj;
        if (!Objects.equals(this.label, other.label)) return false;
        return true;
    }

    @Override
    public String toString()
    {
        return label + "\t" + loc + "\t" + relative;
    }
}