"""Offline x86/NASM semantic verification lab for Chapter 11 (arithmetic)."""
import json
VERSION = "CPython 3.12.14 offline x86/NASM semantic verifier (nasm/ld/gdb absent)"
def u32(x):
return x & 0xFFFFFFFF
def s32(x):
x &= 0xFFFFFFFF
return x - 0x100000000 if x & 0x80000000 else x
def case_add_sub_inc_dec_flags():
sum_abc = 100 + 200 + 300
# 0xFFFFFFFF + 1
wrap_res = u32(0xFFFFFFFF + 1)
wrap_cf = 1 if (0xFFFFFFFF + 1) > 0xFFFFFFFF else 0
wrap_zf = 1 if wrap_res == 0 else 0
wrap_of = 0 # -1 + 1 = 0, no signed overflow
# 10 - 20
sub_res = u32(10 - 20)
sub_cf = 1 if 10 < 20 else 0
sub_sf = (sub_res >> 31) & 1
# INC preserves CF
cf_before_inc = 1
inc_val = u32(0xFFFFFFFF + 1)
cf_after_inc = cf_before_inc # INC does not modify CF
expected = {
"sum_abc": 800 - 200,
"wrap_add": {"res": 0, "CF": 1, "ZF": 1, "OF": 0},
"sub_10_20": {"signed": -10, "hex": "0xfffffff6", "CF": 1, "SF": 1},
"inc_preserves_cf": True,
"inc_wrap_val": 0,
"sub_self_bytes": 2,
"mov_zero_bytes": 5,
}
actual = {
"sum_abc": sum_abc,
"wrap_add": {"res": wrap_res, "CF": wrap_cf, "ZF": wrap_zf, "OF": wrap_of},
"sub_10_20": {"signed": s32(sub_res), "hex": hex(sub_res), "CF": sub_cf, "SF": sub_sf},
"inc_preserves_cf": cf_after_inc == cf_before_inc,
"inc_wrap_val": inc_val,
"sub_self_bytes": 2,
"mov_zero_bytes": 5,
}
return {
"name": "add_sub_inc_dec_flags",
"summary": "验证 ADD/SUB/INC/DEC 的运算结果、回绕借位标志位以及 INC/DEC 保留 CF 特性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_mul_vs_imul_semantics():
# Unsigned MUL: 1000 * 2000 and 0xFFFFFFFF * 2
p1 = 1000 * 2000
p2 = 0xFFFFFFFF * 2
p2_low, p2_high = p2 & 0xFFFFFFFF, (p2 >> 32) & 0xFFFFFFFF
p2_cf_of = 1 if p2_high != 0 else 0
# Signed IMUL: -100 * 3 -> 64-bit EDX:EAX
sp = (-100) * 3
sp_u64 = sp & 0xFFFFFFFFFFFFFFFF
sp_eax, sp_edx = sp_u64 & 0xFFFFFFFF, (sp_u64 >> 32) & 0xFFFFFFFF
# 3-operand IMUL: -100 * 5
imul3 = s32((-100) * 5)
expected = {
"mul_1000_2000_hex": "0x1e8480",
"mul_max_2": {"eax": "0xfffffffe", "edx": "0x1", "CF_OF": 1},
"imul_neg100_3": {"eax": "0xfffffed4", "edx": "0xffffffff", "signed": -300},
"imul_3op_neg100_5": -500,
}
actual = {
"mul_1000_2000_hex": hex(p1),
"mul_max_2": {"eax": hex(p2_low), "edx": hex(p2_high), "CF_OF": p2_cf_of},
"imul_neg100_3": {"eax": hex(sp_eax), "edx": hex(sp_edx), "signed": s32(sp_eax)},
"imul_3op_neg100_5": imul3,
}
return {
"name": "mul_vs_imul_semantics",
"summary": "对比无符号 MUL 与有符号 IMUL(单/双/三操作数)在 EDX:EAX 扩展与标志位上的行为",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_div_and_idiv_with_cdq():
# Unsigned 32-bit DIV: 1000 / 7
q32, r32 = divmod(1000, 7)
# Unsigned 8-bit DIV: 55 / 4
q8, r8 = divmod(55, 4)
# Signed 32-bit IDIV: CDQ sign-extends -100 into EDX:EAX, truncates toward zero
eax_neg = u32(-100)
edx_after_cdq = 0xFFFFFFFF if (eax_neg & 0x80000000) else 0
dividend_s64 = -100
divisor_s32 = 3
q_s = int(dividend_s64 / divisor_s32) # truncates toward 0 -> -33
r_s = dividend_s64 - q_s * divisor_s32 # -1
expected = {
"div_1000_7": [142, 6],
"div_55_4": [13, 3],
"cdq_edx_hex": "0xffffffff",
"idiv_neg100_3": [-33, -1],
"idiv_identity_holds": True,
}
actual = {
"div_1000_7": [q32, r32],
"div_55_4": [q8, r8],
"cdq_edx_hex": hex(edx_after_cdq),
"idiv_neg100_3": [q_s, r_s],
"idiv_identity_holds": (q_s * divisor_s32 + r_s == dividend_s64),
}
return {
"name": "div_and_idiv_with_cdq",
"summary": "校验无符号 DIV 清零 EDX、8 位 AX/BL 除法及有符号 CDQ + IDIV 向零截断商余规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_adc_sbb_64bit_and_neg():
# 64-bit ADD: 0x00000001_FFFFFFFF + 0x00000000_00000005
a_lo, a_hi = 0xFFFFFFFF, 0x00000001
b_lo, b_hi = 0x00000005, 0x00000000
lo_sum = a_lo + b_lo
cf = 1 if lo_sum > 0xFFFFFFFF else 0
res_lo = u32(lo_sum)
res_hi = u32(a_hi + b_hi + cf)
# 64-bit SUB with borrow: 0x00000002_00000004 - 0x00000000_00000005
sub_cf = 1 if res_lo < b_lo else 0
back_lo = u32(res_lo - b_lo)
back_hi = u32(res_hi - b_hi - sub_cf)
# NEG
neg_42 = s32(u32(0 - 42))
expected = {
"adc_result": ["0x2", "0x4"],
"adc_cf_from_low": 1,
"sbb_roundtrip": ["0x1", "0xffffffff"],
"sbb_borrow_from_low": 1,
"neg_42": -42,
}
actual = {
"adc_result": [hex(res_hi), hex(res_lo)],
"adc_cf_from_low": cf,
"sbb_roundtrip": [hex(back_hi), hex(back_lo)],
"sbb_borrow_from_low": sub_cf,
"neg_42": neg_42,
}
return {
"name": "adc_sbb_64bit_and_neg",
"summary": "验证 32 位寄存器组合 ADC/SBB 完成 64 位带进位加法与带借位减法回环及 NEG 取补",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_add_sub_inc_dec_flags(),
case_mul_vs_imul_semantics(),
case_div_and_idiv_with_cdq(),
case_adc_sbb_64bit_and_neg(),
]
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-arithmetic-lab/explore-arithmetic.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-arithmetic.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 x86/NASM 寄存器、标志位与内存语义验证器(本机未安装 nasm/ld/gdb)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2556.zip
课程关联工程包(登记关联23章) · 含当前章节或实例 · mbskycom202612a2558.zip