这段汇编在算什么
运行下面这段程序,填写它打印出来的结果。
def run(src, regs=None, guard=200):
R = {"eax": 0, "ebx": 0, "ecx": 0, "edx": 0}
if regs:
R.update(regs)
labels = {}
prog = []
for line in src.strip().splitlines():
line = line.strip()
if not line:
continue
if line.endswith(":"):
labels[line[:-1]] = len(prog)
else:
prog.append(line)
pc = 0
flag = 0
stack = []
steps = 0
while pc < len(prog):
steps = steps + 1
if steps > guard:
break
op, _, rest = prog[pc].partition(" ")
args = [a.strip() for a in rest.split(",")] if rest else []
if op == "mov":
R[args[0]] = R[args[1]] if args[1] in R else int(args[1])
elif op == "add":
R[args[0]] = R[args[0]] + (R[args[1]] if args[1] in R else int(args[1]))
elif op == "sub":
R[args[0]] = R[args[0]] - (R[args[1]] if args[1] in R else int(args[1]))
elif op == "cmp":
flag = R[args[0]] - (R[args[1]] if args[1] in R else int(args[1]))
elif op == "push":
stack.append(R[args[0]])
elif op == "pop":
R[args[0]] = stack.pop()
elif op == "jmp":
pc = labels[args[0]]
continue
elif op == "jne":
if flag != 0:
pc = labels[args[0]]
continue
elif op == "jle":
if flag <= 0:
pc = labels[args[0]]
continue
pc = pc + 1
return R, steps, stack
SRC = """
mov eax, 0
mov ecx, 5
L:
add eax, 3
sub ecx, 1
cmp ecx, 0
jne L
"""
R, steps, _ = run(SRC)
print(str(R["eax"]) + "/" + str(steps))
全部评论