0

I am trying to program an assembler and the way I do it is the following: First I get get the program, split it in lines. After that, I treat each line individually, split each line into characters including everything and I am stuck now because I do not know how to check for matches, for example: how would I check for the opcode "LOAD" the register "R1" and the data value "18" in a list such as ["L", "O", "A", "D", " ", "R", "1", ",", "1", "8"]? Please, help is appreciated.

  • 1
    [maybe related: how-do-you-make-an-assembler](https://stackoverflow.com/questions/2478142/how-do-you-make-an-assembler) – Nahuel Fouilleul Feb 01 '18 at 13:43
  • 1
    Why split `LOAD R1,18` into `["L", "O", "A", "D", " ", "R", "1", ",", "1", "8"]` instead of `["LOAD", "R1", "18"]`? – mouviciel Feb 01 '18 at 13:44
  • 2
    It might be helpful to show your code (or at least some example code that demonstrates what you're trying to achieve). – glibdud Feb 01 '18 at 13:44
  • 1
    You need to define the grammar of your assembly language. If all instructions are always of the form `opcode operand[,operands...]`, then you can just split on whitespace to separate `opcode` from `operands`, and then split `operands` on `,` to break it into pieces. – 0x5453 Feb 01 '18 at 13:47

2 Answers2

1

Before you split the line into characters you could split it into words like below list. The if statement then can check for items in the list.

x=["This", "apple","is","red"]
if "This" in x:
    print "yes"

Updated answer per comments below.

line="MD=D+1"
if "MD" in line:
    print "Do something"

Updated to get command out of line.

line="MD=D+1"
if "MD" in line:
    print line.split("MD")
    command=line.split("MD")[1] #get second element in list
    print command
    #now you can parse command to do something with it. 
Zack Tarr
  • 851
  • 1
  • 8
  • 28
0

First, you have to concatenate the Strings in an new str. Then, you can use split method on your string to create a charList.

str1 = 'LOAD", str2 = 'R1', str3= '8

strN = str1 + " " + str2 + " " + str3
result = strN.split()
print(result)