轻松的编程学习
首页
题库
学习路径
在线商城
能力地图
下载应用
登录 / 注册
菜单
数一数循环体被执行了几遍
👁️ 2 人浏览
💬 0 人评论
❤️ 添加收藏
同一段汇编,把循环次数分别设成 3 和 7,数出
循环体那四条指令
各被执行了多少遍。
提交你的答案
请登录后提交答案。
去登录
← 把循环反推成一个表达式
⚠️ 猜对了形状,猜错了边界 →
更多题目
让程序说出"你好"
让程序欢迎你
哪个命令能显示内容
哪里是指令,哪里是结果
让程序说出你的名字
这个程序会显示什么
代码编辑器
语言:
python3
c11
cpp17
Ctrl
+
Enter
运行
👩🏫 AI
▶ 运行代码
重置代码
打印代码
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 def body_runs(k): src = ("mov eax, 0\n" + "mov ecx, " + str(k) + "\n" + "L:\nadd eax, 3\nsub ecx, 1\ncmp ecx, 0\njne L\n") R, steps, _ = run(src) # TODO: 总步数减去开头那两条 mov,再除以循环体的四条 return 0 a = body_runs(3) b = body_runs(7) print(str(a) + "/" + str(b) + "/" + str(a * 4) + "/" + str(b * 4))
本次输入:
输出:
👩🏫
AI
请登录后使用 AI 老师
×
登录后可获得解题思路、提示与错误分析。
去登录
关闭
🎉
恭喜你,回答正确!
系统判定:正确
我知道了
💬 题目评论
提交
全部评论
全部评论