蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 ELF32 汇编链接命令链、$ - msg 长度计算与 int 0x80 Hello 模拟

PYTHONWindows x64、CPython 3.12.14 离线 x86/NASM 寄存器、标志位与内存语义验证器(本机未安装 nasm/ld/gdb)。explore-env-setup.py
explore-env-setup.py129 行
"""Offline x86/NASM semantic verification lab for Chapter 03 (env-setup)."""
import json
import re

VERSION = "CPython 3.12.14 offline x86/NASM semantic verifier (nasm/ld/gdb absent)"


def case_hello_asm_section_and_len_equ():
    msg_bytes = b"Hello, Assembly!\n"
    msg_addr = 0x08049000
    current_addr = msg_addr + len(msg_bytes)
    len_equ = current_addr - msg_addr
    expected = {
        "msg_len": 17,
        "trailing_lf": 0x0A,
        "len_equ_matches": True,
        "entry_symbol": "_start",
    }
    actual = {
        "msg_len": len_equ,
        "trailing_lf": msg_bytes[-1],
        "len_equ_matches": len_equ == len(msg_bytes),
        "entry_symbol": "_start",
    }
    return {
        "name": "hello_asm_section_and_len_equ",
        "summary": "校验 section .data 字符串字节序列、0xA 换行符与 len equ $ - msg 编译期长度计算",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_int80_hello_simulation():
    stdout = bytearray()
    exit_code = None
    mem = {0x08049000: b"Hello, Assembly!\n"}

    def int80(eax, ebx=0, ecx=0, edx=0):
        nonlocal exit_code
        if eax == 4 and ebx == 1:
            data = mem[ecx][:edx]
            stdout.extend(data)
            return len(data)
        if eax == 1:
            exit_code = ebx
            return 0
        return -38

    written = int80(eax=4, ebx=1, ecx=0x08049000, edx=17)
    int80(eax=1, ebx=0)
    expected = {"written_bytes": 17, "stdout": "Hello, Assembly!\n", "exit_code": 0}
    actual = {"written_bytes": written, "stdout": stdout.decode("ascii"), "exit_code": exit_code}
    return {
        "name": "int80_hello_simulation",
        "summary": "模拟 Linux 32 位 int 0x80 执行 sys_write(4) 与 sys_exit(1) 寄存器传参与返回值",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_build_and_link_command_pipeline():
    ld_pipeline = [
        "nasm -f elf32 hello.asm -o hello.o",
        "ld -m elf_i386 hello.o -o hello",
        "./hello",
    ]
    gcc_cmd = "gcc -m32 hello.o -o hello -nostartfiles"
    expected = {
        "nasm_format": "elf32",
        "ld_emulation": "elf_i386",
        "gcc_has_m32": True,
        "gcc_has_nostartfiles": True,
        "needs_multilib_pkg": "gcc-multilib",
    }
    actual = {
        "nasm_format": re.search(r"-f\s+(\S+)", ld_pipeline[0]).group(1),
        "ld_emulation": re.search(r"-m\s+(\S+)", ld_pipeline[1]).group(1),
        "gcc_has_m32": "-m32" in gcc_cmd,
        "gcc_has_nostartfiles": "-nostartfiles" in gcc_cmd,
        "needs_multilib_pkg": "gcc-multilib",
    }
    return {
        "name": "build_and_link_command_pipeline",
        "summary": "校验 nasm -f elf32、ld -m elf_i386 与 gcc -m32 -nostartfiles 汇编链接参数链",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_gdb_debug_commands_and_entry_check():
    gdb_cmds = ["break _start", "run", "stepi", "info registers", "x/s msg"]
    src_with_global = "section .text\n    global _start\n_start:\n    mov eax, 1\n"
    has_global = bool(re.search(r"\bglobal\s+_start\b", src_with_global))
    has_label = bool(re.search(r"^_start:", src_with_global, re.M))
    expected = {
        "gdb_command_count": 5,
        "debug_flag": "-g",
        "entry_exported_and_defined": True,
    }
    actual = {
        "gdb_command_count": len(gdb_cmds),
        "debug_flag": "-g",
        "entry_exported_and_defined": has_global and has_label,
    }
    return {
        "name": "gdb_debug_commands_and_entry_check",
        "summary": "核对 GDB 常用调试命令集、-g 调试符号标志与 global _start 入口符号匹配检查",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_hello_asm_section_and_len_equ(),
        case_int80_hello_simulation(),
        case_build_and_link_command_pipeline(),
        case_gdb_debug_commands_and_entry_check(),
    ]
    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-env-setup-lab/explore-env-setup.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-env-setup.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 x86/NASM 寄存器、标志位与内存语义验证器(本机未安装 nasm/ld/gdb)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:汇编语言

课程关联工程包(登记关联23章) · 含当前章节或实例 · mbskycom202612a2558.zip

下载课程关联工程包:汇编语言

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2562.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习