Reputation: 1603
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
Upvotes: 0
Views: 146
Reputation: 385830
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)).
Upvotes: 2