import json
import sys
class MemoryInspector:
def __init__(self, size: int = 256) -> None:
self.mem = [0] * size
def write_bytes(self, addr: int, values: list[int]) -> None:
for i, v in enumerate(values):
self.mem[addr + i] = v & 0xFF
def read(self, addr: int) -> int:
return self.mem[addr]
def run_cases() -> list[dict]:
cases: list[dict] = []
# Case 1: Unified memory layout storing instructions and data side by side
mi = MemoryInspector(256)
# Code region: 0x00..0x07 (opcode, operand pairs)
# 0x01=LOAD, 0x02=ADD, 0x03=STORE, 0xFF=HALT
mi.write_bytes(0, [0x01, 100, 0x02, 101, 0x03, 102, 0xFF, 0])
# Data region: 100..102
mi.write_bytes(100, [15, 27, 0])
# Execute from unified memory
pc = 0
acc = 0
while True:
op = mi.read(pc)
arg = mi.read(pc + 1)
pc += 2
if op == 0x01:
acc = mi.read(arg)
elif op == 0x02:
acc = (acc + mi.read(arg)) & 0xFF
elif op == 0x03:
mi.write_bytes(arg, [acc])
elif op == 0xFF:
break
actual1 = {
"code_bytes_at_0": mi.mem[0:8],
"data_result_at_102": mi.read(102),
"same_storage_array": True,
}
expected1 = {
"code_bytes_at_0": [1, 100, 2, 101, 3, 102, 255, 0],
"data_result_at_102": 42,
"same_storage_array": True,
}
cases.append(
{
"name": "unified_memory_layout",
"summary": "验证指令字节与数据字节存放于同一线性内存数组并按 PC/地址分别读取执行",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
}
)
# Case 2: Same byte value (0x41 = 65) interpreted by context as instruction, integer, or ASCII
raw_byte = 0x41
opcode_table = {0x41: "INC_ECX_X86_32"}
actual2 = {
"raw_hex": f"0x{raw_byte:02X}",
"raw_bin": format(raw_byte, "08b"),
"as_unsigned_int": raw_byte,
"as_ascii": chr(raw_byte),
"as_opcode": opcode_table[raw_byte],
}
expected2 = {
"raw_hex": "0x41",
"raw_bin": "01000001",
"as_unsigned_int": 65,
"as_ascii": "A",
"as_opcode": "INC_ECX_X86_32",
}
cases.append(
{
"name": "byte_context_ambiguity",
"summary": "验证同一字节 0x41 在不同语境下可分别解释为整数 65、ASCII 字符 A 或指令码",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
}
)
# Case 3: Self-modifying operand (changing STORE target address from 100 to 200 in memory)
mem_sm = MemoryInspector(256)
# Instruction at 0..1: STORE 100 (0x03, 100)
mem_sm.write_bytes(0, [0x03, 100])
target_before = mem_sm.read(1)
# Program writes 200 into address 1 (modifying its own instruction operand)
mem_sm.write_bytes(1, [200])
target_after = mem_sm.read(1)
# Execute modified STORE with acc=99
acc_val = 99
mem_sm.write_bytes(target_after, [acc_val])
actual3 = {
"target_before": target_before,
"target_after": target_after,
"mem_100": mem_sm.read(100),
"mem_200": mem_sm.read(200),
}
expected3 = {
"target_before": 100,
"target_after": 200,
"mem_100": 0,
"mem_200": 99,
}
cases.append(
{
"name": "self_modifying_instruction_demo",
"summary": "验证存储程序架构允许程序将自身指令当作数据改写(STORE 100 动态改为 STORE 200)",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
}
)
# Case 4: Von Neumann vs Harvard vs Modified Harvard split L1 cache comparison
arch_profiles = {
"von_neumann": {"shared_main_mem": True, "split_l1_cache": False, "parallel_if_mem_in_l1": False},
"harvard": {"shared_main_mem": False, "split_l1_cache": False, "parallel_if_mem_in_l1": True},
"modified_harvard": {"shared_main_mem": True, "split_l1_cache": True, "parallel_if_mem_in_l1": True},
}
actual4 = {
"architectures": arch_profiles,
"modern_cpu_model": "modified_harvard",
}
expected4 = {
"architectures": {
"von_neumann": {"shared_main_mem": True, "split_l1_cache": False, "parallel_if_mem_in_l1": False},
"harvard": {"shared_main_mem": False, "split_l1_cache": False, "parallel_if_mem_in_l1": True},
"modified_harvard": {"shared_main_mem": True, "split_l1_cache": True, "parallel_if_mem_in_l1": True},
},
"modern_cpu_model": "modified_harvard",
}
cases.append(
{
"name": "architecture_comparison_matrix",
"summary": "对比冯·诺依曼、纯粹哈佛与现代改进型哈佛(L1 I-Cache/D-Cache 分离)架构特性",
"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-stored-program-concept-lab/explore-stored-program-concept.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-stored-program-concept.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2636.zip
课程关联工程包(登记关联22章) · 含当前章节或实例 · mbskycom202612a2616.zip