"""Offline x86/NASM semantic verification lab for Chapter 07 (syscall)."""
import json
VERSION = "CPython 3.12.14 offline x86/NASM semantic verifier (nasm/ld/gdb absent)"
def case_linux_32_vs_64_syscall_abi():
i386_table = {
"sys_exit": 1, "sys_fork": 2, "sys_read": 3, "sys_write": 4,
"sys_open": 5, "sys_close": 6, "sys_creat": 8, "sys_lseek": 19, "sys_brk": 45,
}
x86_64_subset = {"sys_read": 0, "sys_write": 1, "sys_open": 2, "sys_close": 3, "sys_exit": 60}
i386_arg_regs = ["EBX", "ECX", "EDX", "ESI", "EDI", "EBP"]
x86_64_arg_regs = ["RDI", "RSI", "RDX", "R10", "R8", "R9"]
expected = {
"i386_count": 9,
"i386_write_nr": 4,
"x86_64_write_nr": 1,
"numbers_differ": True,
"i386_trap": "int 0x80",
"x86_64_trap": "syscall",
"arg_reg_counts": [6, 6],
}
actual = {
"i386_count": len(i386_table),
"i386_write_nr": i386_table["sys_write"],
"x86_64_write_nr": x86_64_subset["sys_write"],
"numbers_differ": i386_table["sys_write"] != x86_64_subset["sys_write"],
"i386_trap": "int 0x80",
"x86_64_trap": "syscall",
"arg_reg_counts": [len(i386_arg_regs), len(x86_64_arg_regs)],
}
return {
"name": "linux_32_vs_64_syscall_abi",
"summary": "核对 Linux 32 位 int 0x80 常用 9 个系统调用号及参数寄存器与 x86-64 syscall 差异",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_sys_write_and_sys_read_echo():
stdin_stream = bytearray(b"BlueSky\n")
stdout_stream = bytearray()
bss_buffer = bytearray(64)
prompt = b"Please enter your name: "
prefix = b"Hello, "
def sys_write(fd, data, count):
if fd != 1:
return -9 # EBADF
chunk = data[:count]
stdout_stream.extend(chunk)
return len(chunk)
def sys_read(fd, buf, max_len):
if fd != 0:
return -9
n = min(len(stdin_stream), max_len)
buf[:n] = stdin_stream[:n]
del stdin_stream[:n]
return n
w1 = sys_write(1, prompt, len(prompt))
n_read = sys_read(0, bss_buffer, 64)
esi = n_read
w2 = sys_write(1, prefix, len(prefix))
w3 = sys_write(1, bytes(bss_buffer), esi)
expected = {
"read_bytes_including_lf": 8,
"written_counts": [24, 7, 8],
"stdout": "Please enter your name: Hello, BlueSky\n",
}
actual = {
"read_bytes_including_lf": n_read,
"written_counts": [w1, w2, w3],
"stdout": stdout_stream.decode("ascii"),
}
return {
"name": "sys_write_and_sys_read_echo",
"summary": "模拟 sys_write 提示输出、sys_read 读取标准输入并按 EAX 实际字节数回显流程",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_sys_exit_and_errno_convention():
def dispatch(eax, ebx=0):
if eax == 1:
return {"terminated": True, "exit_status": ebx & 0xFF}
if eax == 5 and ebx == 0:
return {"terminated": False, "eax": -1} # -EPERM / error
return {"terminated": False, "eax": 0}
ok_exit = dispatch(1, 0)
custom_exit = dispatch(1, 42)
err_call = dispatch(5, 0)
expected = {
"normal_exit_code": 0,
"custom_exit_code": 42,
"error_is_negative": True,
"error_eax": -1,
}
actual = {
"normal_exit_code": ok_exit["exit_status"],
"custom_exit_code": custom_exit["exit_status"],
"error_is_negative": err_call["eax"] < 0,
"error_eax": err_call["eax"],
}
return {
"name": "sys_exit_and_errno_convention",
"summary": "验证 sys_exit 退出码($?)与 Linux 系统调用 EAX 负值错误码判定规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_linux_32_vs_64_syscall_abi(),
case_sys_write_and_sys_read_echo(),
case_sys_exit_and_errno_convention(),
]
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-syscall-lab/explore-syscall.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-syscall.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 x86/NASM 寄存器、标志位与内存语义验证器(本机未安装 nasm/ld/gdb)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2575.zip
课程关联工程包(登记关联23章) · 含当前章节或实例 · mbskycom202612a2558.zip