"""Offline x86/NASM semantic verification lab for Chapter 14 (loop)."""
import json
VERSION = "CPython 3.12.14 offline x86/NASM semantic verifier (nasm/ld/gdb absent)"
def case_loop_and_loope_loopne_variants():
# Basic LOOP 5 times with push/pop ECX around syscall
ecx = 5
outputs = []
stack = []
while True:
stack.append(ecx)
outputs.append("mbsky")
ecx = stack.pop()
ecx -= 1
if ecx == 0:
break
# LOOPE: scan [0, 0, 0, 5, 0, 0] for first non-zero
arr = [0, 0, 0, 5, 0, 0]
ecx_e = len(arr)
esi = -1
while True:
esi += 1
zf = 1 if arr[esi] == 0 else 0
ecx_e -= 1
if not (ecx_e != 0 and zf == 1):
break
# LOOPNE: scan [1, 2, 0, 4] until finding 0
arr2 = [1, 2, 0, 4]
ecx_ne = len(arr2)
edi = -1
while True:
edi += 1
zf2 = 1 if arr2[edi] == 0 else 0
ecx_ne -= 1
if not (ecx_ne != 0 and zf2 == 0):
break
expected = {
"loop_output_count": 5,
"loope_found_index": 3,
"loope_found_val": 5,
"loope_remaining_ecx": 2,
"loopne_zero_index": 2,
}
actual = {
"loop_output_count": len(outputs),
"loope_found_index": esi,
"loope_found_val": arr[esi],
"loope_remaining_ecx": ecx_e,
"loopne_zero_index": edi,
}
return {
"name": "loop_and_loope_loopne_variants",
"summary": "模拟标准 LOOP(带栈保护 ECX)、LOOPE 查找首个非零项与 LOOPNE 查找首个零值",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_while_and_do_while_control_flow():
# while (x < 100) x = x * 2
x = 1
while_trace = [x]
while x < 100:
x <<= 1
while_trace.append(x)
# do-while: executes at least once even if condition is already false
counter = 0
while True:
counter += 1
if not (counter < 5):
break
counter_already_high = 10
do_once_runs = 0
while True:
counter_already_high += 1
do_once_runs += 1
if not (counter_already_high < 5):
break
expected = {
"while_final_x": 128,
"while_steps": 7,
"do_while_counter": 5,
"do_while_executes_at_least_once": 1,
}
actual = {
"while_final_x": x,
"while_steps": len(while_trace) - 1,
"do_while_counter": counter,
"do_while_executes_at_least_once": do_once_runs,
}
return {
"name": "while_and_do_while_control_flow",
"summary": "验证先判后执 WHILE 倍增至 128 与先执后判 DO-WHILE 至少执行一次的控制流语义",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_nested_loop_and_unrolling_sum():
# Nested 3x3 loop saving outer ECX on stack
stack = []
outer_ecx = 3
grid = []
while True:
stack.append(outer_ecx)
row_idx = 4 - outer_ecx
inner_ecx = 3
row = []
while True:
col_idx = 4 - inner_ecx
row.append(row_idx * col_idx)
inner_ecx -= 1
if inner_ecx == 0:
break
grid.append(row)
outer_ecx = stack.pop()
outer_ecx -= 1
if outer_ecx == 0:
break
# Loop unrolling (factor 4) vs standard sum over 1..100
nums = list(range(1, 101))
sum_loop = sum(nums)
sum_unrolled = 0
for i in range(0, len(nums), 4):
sum_unrolled += nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3]
sum_reverse = 0
for idx in range(len(nums) - 1, -1, -1):
sum_reverse += nums[idx]
expected = {
"grid_3x3": [[1, 2, 3], [2, 4, 6], [3, 6, 9]],
"stack_balanced": True,
"sum_1_to_100": 5050,
"unrolled_matches": True,
"reverse_matches": True,
}
actual = {
"grid_3x3": grid,
"stack_balanced": len(stack) == 0,
"sum_1_to_100": sum_loop,
"unrolled_matches": sum_unrolled == 5050,
"reverse_matches": sum_reverse == 5050,
}
return {
"name": "nested_loop_and_unrolling_sum",
"summary": "验证嵌套循环压栈保护外层 ECX、4 路循环展开与倒序遍历累加 1..100 得 5050 的等价性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_loop_and_loope_loopne_variants(),
case_while_and_do_while_control_flow(),
case_nested_loop_and_unrolling_sum(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 x86/NASM 寄存器、标志位与内存语义验证器(本机未安装 nasm/ld/gdb)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 x86/NASM 寄存器、标志位与内存语义验证器(本机未安装 nasm/ld/gdb)。
入口文件:projects/assembly-loop-lab/explore-loop.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-loop.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 x86/NASM 寄存器、标志位与内存语义验证器(本机未安装 nasm/ld/gdb)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2566.zip
课程关联工程包(登记关联23章) · 含当前章节或实例 · mbskycom202612a2558.zip