"""Offline x86/NASM semantic verification lab for Chapter 15 (numbers)."""
import json
VERSION = "CPython 3.12.14 offline x86/NASM semantic verifier (nasm/ld/gdb absent)"
def case_radix_and_ascii_digit_mapping():
bases = {
"bin": int("10101010", 2),
"oct": int("52", 8),
"dec": 42,
"hex": int("2A", 16),
}
ascii_map = {
"'0'": (ord("0"), ord("0") - 0x30),
"'1'": (ord("1"), ord("1") - 0x30),
"'9'": (ord("9"), ord("9") - 0x30),
"'A'": (ord("A"), ord("A") - ord("A") + 10),
"'F'": (ord("F"), ord("F") - ord("A") + 10),
"'a'": (ord("a"), ord("a") - ord("a") + 10),
}
expected = {
"oct_dec_hex_all_42": True,
"bin_10101010": 170,
"ascii_0": [0x30, 0],
"ascii_9": [0x39, 9],
"ascii_A": [0x41, 10],
"ascii_F": [0x46, 15],
"ascii_a": [0x61, 10],
}
actual = {
"oct_dec_hex_all_42": bases["oct"] == bases["dec"] == bases["hex"] == 42,
"bin_10101010": bases["bin"],
"ascii_0": list(ascii_map["'0'"]),
"ascii_9": list(ascii_map["'9'"]),
"ascii_A": list(ascii_map["'A'"]),
"ascii_F": list(ascii_map["'F'"]),
"ascii_a": list(ascii_map["'a'"]),
}
return {
"name": "radix_and_ascii_digit_mapping",
"summary": "校验二/八/十/十六进制 NASM 字面量与 ASCII 字符 '0'-'9'/'A'-'F'/'a' 的数值映射",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_string_to_int_and_int_to_string():
# string_to_int ("12345")
s = "12345"
eax = 0
steps = []
for ch in s:
eax = eax * 10 + (ord(ch) - ord("0"))
steps.append(eax)
# int_to_string (12345 and 0) filling buffer backward
def int_to_str(val):
buf = bytearray(12)
edi = 11
buf[edi] = 0
edi -= 1
n = val
while True:
n, rem = divmod(n, 10)
buf[edi] = rem + ord("0")
edi -= 1
if n == 0:
break
start = edi + 1
return buf[start:11].decode("ascii")
expected = {
"parse_steps": [1, 12, 123, 1234, 12345],
"formatted_12345": "12345",
"formatted_0": "0",
}
actual = {
"parse_steps": steps,
"formatted_12345": int_to_str(eax),
"formatted_0": int_to_str(0),
}
return {
"name": "string_to_int_and_int_to_string",
"summary": "模拟乘 10 累加字符串转整数与除 10 取余倒序写入缓冲区的整数转十进制字符串算法",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_hex_format_and_simple_calc():
# 32-bit hex output for 0x1A2B3C4D
val = 0x1A2B3C4D
buf = bytearray(8)
edi = 8
n = val
for _ in range(8):
n, rem = divmod(n, 16)
ch = (rem + ord("0")) if rem < 10 else (rem + ord("A") - 10)
edi -= 1
buf[edi] = ch
hex_out = "0x" + buf.decode("ascii")
# Single-digit addition calculator with 1-digit and 2-digit output trimming
def add_single_digits(c1, c2):
total = (ord(c1) - ord("0")) + (ord(c2) - ord("0"))
tens, ones = divmod(total, 10)
s_tens, s_ones = chr(tens + ord("0")), chr(ones + ord("0"))
return s_ones if s_tens == "0" else (s_tens + s_ones)
expected = {
"hex_output": "0x1A2B3C4D",
"calc_7_plus_8": "15",
"calc_3_plus_4": "7",
"calc_9_plus_9": "18",
}
actual = {
"hex_output": hex_out,
"calc_7_plus_8": add_single_digits("7", "8"),
"calc_3_plus_4": add_single_digits("3", "4"),
"calc_9_plus_9": add_single_digits("9", "9"),
}
return {
"name": "hex_format_and_simple_calc",
"summary": "验证 32 位定宽 8 字符十六进制输出(0x1A2B3C4D)与个位数加法器前导零裁剪逻辑",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_radix_and_ascii_digit_mapping(),
case_string_to_int_and_int_to_string(),
case_hex_format_and_simple_calc(),
]
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-numbers-lab/explore-numbers.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-numbers.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 x86/NASM 寄存器、标志位与内存语义验证器(本机未安装 nasm/ld/gdb)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联23章) · 含当前章节或实例 · mbskycom202612a2558.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2570.zip