0

I have an assignment to build a two pass assembler using any language I want. I chose python. It served me well, except for one short problem.

I built a symbol table using a .asm input file. Then I use the symbol table to create object code. My code is given below:

import re,os
import ast
fr=open("2input.asm","r")
fw=open("symbol.txt","r+")
objFile=open("symbol1.txt","w")

str2=fr.readline() #reads lines in file
temp=""
str1="" #used for splitting original string
var=""
var1=""
printstr=""
location=map(int, re.findall('\d+',str2)) #extracts location in the form of string and    then converts it to integer
loc=location[0] #converts location from list form to integer form


for line in fr:         #creates a symbol table
    str2=line
    loc=loc+1
    if str2.find(":")!=-1:
        str1=str2.split(":")
        str1.append(str(loc))
        print>>fw, str1
    elif str2.find("DC")!=-1:
        str1=str2.split("DC")
        str1.append(str(loc))
        print>>fw, str1
    elif str2.find("DS")!=-1:
        str1=str2.split("DS")
        str1.append(str(loc))
        print>>fw, str1
 #symbol table created
fw.seek(0)
fr.seek(0)

for line in fr:         #creates object file
    str2=line
    if str2.find("READ")!=-1:
        str1=str2.split()
        var=str1[1]
        for line in fw:
            var1=ast.literal_eval(line)
            var2=var1[0]
            if var==var2: #it never goes in this if even if the condition is true
                printstr="rohit"
                print>>objFile, printstr
fw.close()

In the last if condition I have used the ast library to convert a string which is in list format to a list datatype.
The control never goes in the last if, why does this happen? How can the control go in the if? Even if both the strings are the same, the control does not go in the if. Why does this happen?

The "symbol.txt" file contains some lines, all of which are stored in list format. Those lines are being converted to list datatype by the ast statement.

EDIT:
I got the problem. Apparently, the ast statement, while converting string to list dataype added some extra whitespaces which led to falsifying the if condition. I changed the if condition thus:

if var1 in var2:
  #do my job
13
  • Most likely the condition is not true. Print out both the string representation and the types of both var1 and var2 and see if there's a difference. Commented Aug 17, 2013 at 17:23
  • You know you don't need to declare your variables, right? What's up with that part at the beginning where you initialize a bunch of variables to ""? Commented Aug 17, 2013 at 17:23
  • 1
    Your variable names are uninformative. var and var1 are particularly bad; pick names that describe the meaning of the contents. Commented Aug 17, 2013 at 17:27
  • 2
    @RohitShinde as viraptor said, print out the representation as well, to see what you're actually dealing with. print repr(var), repr(var2). Commented Aug 17, 2013 at 17:29
  • 1
    If you want to check whether a string is contained in another string, just use the in operator: substring in biggerstring. find is unnecessary. Commented Aug 17, 2013 at 17:33

1 Answer 1

2

If the condition is true, the code in the if statement absolutely will execute. You can be certain about that. Therefore, the condition simply must not be true. Without knowing exactly what your data looks like it's impossible to know for certain. Most likely the values look identical, but they simply cannot be.

So, start with the premise that the values are not equal, and answer the question "if they aren't equal, why aren't they equal?". For example, you can print out the type of each variable, the length of each variable, or you could write a little function that compares each byte one at a time to see which one is different. There's likely either an invisible character you aren't seeing visually (perhaps a leading or trailing space, or perhaps a control character such as a carriage return), or there are visible characters that are extremely similar (such as the digit one and the lowercase L, or a capital O (oh) and number 0 (zero)).

Sign up to request clarification or add additional context in comments.

3 Comments

Even I thought the same thing, I have even cross checked whether the types are same or not. But it simply doesn't help. Is the ast statement likely to cause problems?
You were correct. There were special characters which I could not see. Thanks for helping me out.
@RohitShinde If you don't already know about the logging and unittest modules you might want to look into them. More versatile and elegant than throwing 'print' statements into your code to debug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.