一段陌生汇编跑出来是多少

👁️ 1 人浏览 💬 0 人评论 ❤️ 添加收藏

运行下面这段程序,填写它打印出来的结果。

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, 9
mov ebx, 7
cmp eax, ebx
jle SMALL
mov ecx, eax
jmp END
SMALL:
mov ecx, ebx
END:
"""
R, steps, _ = run(SRC)
print(str(R["ecx"]) + "/" + str(steps))
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论