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.
Asked
Active
Viewed 77 times
0
-
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
-
1Why 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
-
2It 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
-
1You 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 Answers
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
-
Zack, that would work but, each instruction treats parameters differently. – Benny Sweetz Feb 01 '18 at 14:30
-
-
The actual code I am trying to assemble looks something like "MD=D+1" and it does not contain spaces. – Benny Sweetz Feb 01 '18 at 14:34
-
-
I am trying to find "MD" and then make it realize which parameters to expect and how to treat them. – Benny Sweetz Feb 01 '18 at 14:52
-
What parameters and treat them in what way? Please give me an example in some sudo python code. – Zack Tarr Feb 01 '18 at 14:53
-
-
We cannot help really without the code. Hopefully my code gets you started but really we need a nice MCVE https://stackoverflow.com/help/mcve – Zack Tarr Feb 01 '18 at 15:21
-
`characterLine = ["M", "D", "=", "D", "+", "1"] characterCounter = "" for char in characterLine: characterCounter = characterCounter + x if characterCounter == "MD": print("What do I do now")` – Benny Sweetz Feb 01 '18 at 15:31
-
The challenge is you are splitting it into characters so my original will not work. But you want something that says if "MD" is in line do something? Correct? – Zack Tarr Feb 01 '18 at 15:38
-
-
Please see updated answer. If this is what you need please accept the answer. :D – Zack Tarr Feb 01 '18 at 15:43
-
But wait, what I want to do after finding "MD" is manipulate the other parameters ("=D+1") but those are split into individual characters too D:! – Benny Sweetz Feb 01 '18 at 15:46
-
Okay so do not split them into characters. Just keep it as a line. One sec and let me test. – Zack Tarr Feb 01 '18 at 15:47
-
I have updated it so you can get a "command" which in this case will be a string with "=D+1". Then you will need to do something with that "=D+1" but I need you to write code from here and test. Then if you have issues we can help later on. – Zack Tarr Feb 01 '18 at 15:51
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)

maxime Lecoustre
- 9
- 2