我正在用Python编写汇编程序,我只需要帮助编写if else语句。我有以下机器语言指令,我想转换为二进制。
@i
D=M // D=i
@100
D=D-A // D=i-100
如何编写语句,以便删除/忽略注释和空格。我看到它的方式,它将删除/忽略/后面的任何内容,并跳过该行直到它到达一个字母,但我不知道如何写这个。 谢谢!
答案 0 :(得分:1)
with open('in.txt') as f:
lines=f.readlines()
for line in lines:
line=line.strip()
if line:
if '//' in line:
ind=line.find('//')
print(line[:ind])
else:
print(line)
<强>输出强>:
@i
D=M
@100
D=D-A
答案 1 :(得分:0)
请按照以下示例。
if-else codeblock:
#!/usr/bin/python
var1 = 100
if var1:
print "1 - Got a true expression value"
print var1
else:
print "1 - Got a false expression value"
print var1
var2 = 0
if var2:
print "2 - Got a true expression value"
print var2
else:
print "2 - Got a false expression value"
print var2
print "Good bye!"
输出:
1 - Got a true expression value
100
2 - Got a false expression value
0
Good bye!
if-elif-else codeblock:
#!/usr/bin/python
var = 100
if var == 200:
print "1 - Got a true expression value"
print var
elif var == 150:
print "2 - Got a true expression value"
print var2
elif var == 100:
print "3 - Got a true expression value"
print var
else:
print "4 - Got a false expression value"
print var
print "Good bye!"
输出:
3 - Got a true expression value
100
Good bye!