First pass of the assembler

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

First pass of the assembler

DJZygote
Hey guys. I've looked around and I can't find anyone else on here with my particular problem so here goes.
I'm using Python to write the assembler and I have a chunk of code that will identify a label command, add it to the symbol table then remove the label command from the list of instructions. I figured I would just remove them immediately after adding them to the symbol table so they are out of the way. This works great until I have two label commands in a row; when that happens the second one is not processed. I am absolutely brand new to Python so I apologize if the code is gross.

text = inFile.readlines()               #read the whole file into a list

line = 0
for item in text:                           #cycle through each item
    if item[0] == '(':                      #if a label
        symbol = item.strip('()')           #grab the symbol
        symbolHash[symbol] = (line)         #add the item to the symbolHash
        text.remove(item)                   #remove from list of instructions
        print symbol                        #print for debug
    line += 1                               #increment the item to be scanned

I would appreciate any help/advice, thanks.
Reply | Threaded
Open this post in threaded view
|

Re: First pass of the assembler

cadet1620
Administrator
You can't remove an item from a list while iterating the list. When the item is removed, the remaining items in the list move one position toward the head of the list. The next item in the list is now at the same position in the list where the deleted item was. The next iteration of the for loop moves to the next position in the list which will contain the item 2 places after the deleted item
a = ['1','2','3','4','5']
for i in a:
    print(i, end=' ')
    if i == '2':
        a.remove(i)
prints 1 2 4 5. Here's why:
a: 1,2,3,4,5
   ^         (iterator pointer)
prints 1
next iteration
a: 1,2,3,4,5
     ^
prints 2
removes 2
a: 1,3,4,5
     ^
next iteration
a: 1,3,4,5
       ^
prints 4
next iteration
a: 1,3,4,5
         ^
prints 5

For the assembler, you want to do identical parsing on identical input data for both passes. Your line variable would be better named something like addr since it represents the instruction address associated with the line being parsed. It should not be incremented for labels since labels do not result in any instructions.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: First pass of the assembler

DJZygote
Thanks, that explains it :)