"""Offline x86/NASM semantic verification lab for Chapter 06 (registers)."""
import json
VERSION = "CPython 3.12.14 offline x86/NASM semantic verifier (nasm/ld/gdb absent)"
class X86Reg32:
def __init__(self, val=0):
self.v = val & 0xFFFFFFFF
@property
def eax(self):
return self.v
@eax.setter
def eax(self, x):
self.v = x & 0xFFFFFFFF
@property
def ax(self):
return self.v & 0xFFFF
@ax.setter
def ax(self, x):
self.v = (self.v & 0xFFFF0000) | (x & 0xFFFF)
@property
def ah(self):
return (self.v >> 8) & 0xFF
@ah.setter
def ah(self, x):
self.v = (self.v & 0xFFFF00FF) | ((x & 0xFF) << 8)
@property
def al(self):
return self.v & 0xFF
@al.setter
def al(self, x):
self.v = (self.v & 0xFFFFFF00) | (x & 0xFF)
def compute_add_sub_flags(a: int, b: int, is_sub: bool):
a32, b32 = a & 0xFFFFFFFF, b & 0xFFFFFFFF
if is_sub:
raw = a32 - b32
res = raw & 0xFFFFFFFF
cf = 1 if a32 < b32 else 0
of = 1 if (((a32 ^ b32) & (a32 ^ res)) >> 31) & 1 else 0
af = 1 if (a32 & 0xF) < (b32 & 0xF) else 0
else:
raw = a32 + b32
res = raw & 0xFFFFFFFF
cf = 1 if raw > 0xFFFFFFFF else 0
of = 1 if ((~(a32 ^ b32) & (a32 ^ res)) >> 31) & 1 else 0
af = 1 if ((a32 & 0xF) + (b32 & 0xF)) > 0xF else 0
zf = 1 if res == 0 else 0
sf = (res >> 31) & 1
pf = 1 if bin(res & 0xFF).count("1") % 2 == 0 else 0
return {"res": res, "CF": cf, "PF": pf, "AF": af, "ZF": zf, "SF": sf, "OF": of}
def case_partial_register_aliasing():
reg = X86Reg32(0x12345678)
s1 = (hex(reg.eax), hex(reg.ax), hex(reg.ah), hex(reg.al))
reg.ax = 0xAABB
s2 = (hex(reg.eax), hex(reg.ax), hex(reg.al))
reg.al = 0xCC
s3 = (hex(reg.eax), hex(reg.ax), hex(reg.al))
expected = {
"initial": ["0x12345678", "0x5678", "0x56", "0x78"],
"after_mov_ax_aabb": ["0x1234aabb", "0xaabb", "0xbb"],
"after_mov_al_cc": ["0x1234aacc", "0xaacc", "0xcc"],
}
actual = {"initial": list(s1), "after_mov_ax_aabb": list(s2), "after_mov_al_cc": list(s3)}
return {
"name": "partial_register_aliasing",
"summary": "验证 32 位寄存器 EAX 与子寄存器 AX/AH/AL 的重叠映射及局部写入保留高位语义",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_eflags_arithmetic_updates():
f_sub = compute_add_sub_flags(10, 10, is_sub=True)
f_add = compute_add_sub_flags(0xFFFFFFFF, 1, is_sub=False)
f_signed_ovf = compute_add_sub_flags(0x7FFFFFFF, 1, is_sub=False)
expected = {
"sub_10_10": {"res": 0, "ZF": 1, "SF": 0, "CF": 0, "OF": 0, "PF": 1},
"add_maxu32_1": {"res": 0, "ZF": 1, "CF": 1, "OF": 0, "SF": 0},
"add_maxs32_1": {"res": 0x80000000, "ZF": 0, "CF": 0, "OF": 1, "SF": 1},
}
actual = {
"sub_10_10": {k: f_sub[k] for k in ("res", "ZF", "SF", "CF", "OF", "PF")},
"add_maxu32_1": {k: f_add[k] for k in ("res", "ZF", "CF", "OF", "SF")},
"add_maxs32_1": {k: f_signed_ovf[k] for k in ("res", "ZF", "CF", "OF", "SF")},
}
return {
"name": "eflags_arithmetic_updates",
"summary": "精确校验 EFLAGS 中 CF/PF/AF/ZF/SF/OF 在归零减法、无符号回绕与有符号溢出时的状态",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_register_classes_and_cdecl_roles():
gprs = ["EAX", "EBX", "ECX", "EDX", "ESI", "EDI", "EBP", "ESP"]
segs = ["CS", "DS", "SS", "ES", "FS", "GS"]
ptrs = ["EIP", "ESP", "EBP", "ESI", "EDI"]
callee_saved = ["EBX", "ESI", "EDI", "EBP"]
caller_saved = ["EAX", "ECX", "EDX"]
expected = {
"gpr_count": 8,
"seg_count": 6,
"ptr_index_count": 5,
"disjoint_cdecl_sets": True,
"return_reg": "EAX",
}
actual = {
"gpr_count": len(gprs),
"seg_count": len(segs),
"ptr_index_count": len(ptrs),
"disjoint_cdecl_sets": set(callee_saved).isdisjoint(set(caller_saved)),
"return_reg": "EAX",
}
return {
"name": "register_classes_and_cdecl_roles",
"summary": "核对 x86 八大通用寄存器、六大段寄存器、指针变址寄存器及 cdecl 调用/被调用者保存划分",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_x86_64_zero_extension_note():
rax = 0x1122334455667788
# In x86-64: writing 32-bit EAX zero-extends upper 32 bits of RAX; writing AX keeps upper 48 bits
rax_after_mov_eax = 0xAABBCCDD & 0xFFFFFFFF
rax_after_mov_ax = (rax & ~0xFFFF) | 0x9999
expected = {
"after_32bit_write_hex": "0xaabbccdd",
"after_16bit_write_hex": "0x1122334455669999",
}
actual = {
"after_32bit_write_hex": hex(rax_after_mov_eax),
"after_16bit_write_hex": hex(rax_after_mov_ax),
}
return {
"name": "x86_64_zero_extension_note",
"summary": "对比验证 x86-64 下写 32 位寄存器清零高 32 位、写 16 位寄存器保留高 48 位的演进差异",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_partial_register_aliasing(),
case_eflags_arithmetic_updates(),
case_register_classes_and_cdecl_roles(),
case_x86_64_zero_extension_note(),
]
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-registers-lab/explore-registers.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-registers.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 x86/NASM 寄存器、标志位与内存语义验证器(本机未安装 nasm/ld/gdb)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联23章) · 含当前章节或实例 · mbskycom202612a2558.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2573.zip