import json
import struct
import sys
def run_cases() -> list[dict]:
cases: list[dict] = []
# Case 1: Address bus width to addressable byte space (8-bit to 64-bit)
widths = [8, 16, 20, 24, 32, 36, 48, 64]
spaces = {f"{w}bit": 1 << w for w in widths}
actual1 = {
"8bit_bytes": spaces["8bit"],
"16bit_KiB": spaces["16bit"] // 1024,
"20bit_MiB": spaces["20bit"] // (1024 ** 2),
"32bit_GiB": spaces["32bit"] // (1024 ** 3),
"36bit_PAE_GiB": spaces["36bit"] // (1024 ** 3),
"48bit_TiB": spaces["48bit"] // (1024 ** 4),
"64bit_EiB": spaces["64bit"] // (1024 ** 6),
}
expected1 = {
"8bit_bytes": 256,
"16bit_KiB": 64,
"20bit_MiB": 1,
"32bit_GiB": 4,
"36bit_PAE_GiB": 64,
"48bit_TiB": 256,
"64bit_EiB": 16,
}
cases.append(
{
"name": "address_width_capacity_table",
"summary": "验证 8 位至 64 位地址宽度对应的可寻址字节容量(32 位=4 GiB,48 位=256 TiB,64 位=16 EiB)",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
}
)
# Case 2: Byte-addressable memory layout — Little-Endian vs Big-Endian for 0x12345678 & ASCII "Hello\0"
val = 0x12345678
le_bytes = [f"0x{b:02X}" for b in struct.pack("<I", val)]
be_bytes = [f"0x{b:02X}" for b in struct.pack(">I", val)]
hello_bytes = [f"0x{b:02X}" for b in (b"Hello\x00")]
actual2 = {
"little_endian_bytes": le_bytes,
"big_endian_bytes": be_bytes,
"hello_c_string_bytes": hello_bytes,
"le_roundtrip": struct.unpack("<I", struct.pack("<I", val))[0] == val,
}
expected2 = {
"little_endian_bytes": ["0x78", "0x56", "0x34", "0x12"],
"big_endian_bytes": ["0x12", "0x34", "0x56", "0x78"],
"hello_c_string_bytes": ["0x48", "0x65", "0x6C", "0x6C", "0x6F", "0x00"],
"le_roundtrip": True,
}
cases.append(
{
"name": "endianness_and_byte_addressing",
"summary": "验证 0x12345678 在小端序与大端序中的字节排布及字符串 Hello\\0 按字节顺序存储",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
}
)
# Case 3: Virtual-to-Physical Page Table Translation (4 KiB pages = 12-bit offset)
# Virtual address 0x08048123 -> VPN = 0x08048, Offset = 0x123
# Page table maps VPN 0x08048 -> PFN 0x1A3F0 -> Physical = 0x1A3F0123
page_table = {0x08048: 0x1A3F0, 0x08049: 0x2B4C1}
vaddr = 0x08048123
vpn = vaddr >> 12
offset = vaddr & 0xFFF
pfn = page_table[vpn]
paddr = (pfn << 12) | offset
# Second process with same virtual address 0x08048123 maps to different PFN 0x3C5D2
page_table_proc2 = {0x08048: 0x3C5D2}
paddr_proc2 = (page_table_proc2[vpn] << 12) | offset
actual3 = {
"page_size_bytes": 1 << 12,
"vpn_hex": f"0x{vpn:05X}",
"offset_hex": f"0x{offset:03X}",
"proc1_paddr_hex": f"0x{paddr:08X}",
"proc2_paddr_hex": f"0x{paddr_proc2:08X}",
"isolated_physical_frames": paddr != paddr_proc2,
}
expected3 = {
"page_size_bytes": 4096,
"vpn_hex": "0x08048",
"offset_hex": "0x123",
"proc1_paddr_hex": "0x1A3F0123",
"proc2_paddr_hex": "0x3C5D2123",
"isolated_physical_frames": True,
}
cases.append(
{
"name": "mmu_virtual_page_translation",
"summary": "模拟 4 KiB 分页机制下 MMU 将虚拟地址 0x08048123 经页表隔离映射至不同物理页框",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
}
)
# Case 4: Array stride & pointer arithmetic (4-byte int32 array base=0x1000) + CPython small-int interning
base = 0x1000
elem_size = 4
addrs = [f"0x{base + i * elem_size:04X}" for i in range(5)]
a, b = 42, 42
same_id_small_int = (id(a) == id(b))
is_64bit_runtime = sys.maxsize > (1 << 32)
actual4 = {
"int32_array_addresses": addrs,
"small_int_42_shared_id": same_id_small_int,
"python_64bit_address_model": is_64bit_runtime,
}
expected4 = {
"int32_array_addresses": ["0x1000", "0x1004", "0x1008", "0x100C", "0x1010"],
"small_int_42_shared_id": True,
"python_64bit_address_model": True,
}
cases.append(
{
"name": "array_stride_and_python_object_id",
"summary": "验证 4 字节整型数组的连续步长寻址规律以及 64 位 Python 小整数对象地址复用特性",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
}
)
return cases
def main() -> None:
payload = {
"version": f"CPython {sys.version.split()[0]}",
"cases": run_cases(),
}
print(json.dumps(payload, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。
入口文件:projects/computer-organization-memory-addressing-lab/explore-memory-addressing.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-memory-addressing.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联22章) · 含当前章节或实例 · mbskycom202612a2616.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2631.zip