haydenw-uk/badgering-about-assembly

GitHub: haydenw-uk/badgering-about-assembly

一个完全使用 x86-64 汇编语言开发的命令行 CRUD 库存管理系统,展示了在纯底层环境下的内存管理、数据结构操作和算法实现。

Stars: 1 | Forks: 0

# 探讨 Assembly [![语言](https://img.shields.io/badge/Language-x86--64%20Assembly-blue.svg)](https://en.wikipedia.org/wiki/X86-64) [![平台](https://img.shields.io/badge/Platform-Linux-orange.svg)](https://www.linux.org/) [![架构](https://img.shields.io/badge/Architecture-64--bit-green.svg)](https://en.wikipedia.org/wiki/64-bit_computing) [![许可证](https://img.shields.io/badge/License-Academic-lightgrey.svg)](#license) ## 概述 **Badgering About Assembly** 是一个命令行库存管理系统,作为牛津布鲁克斯大学(2025年)的课程作业开发。该应用程序通过完全使用 x86-64 汇编语言实现一个完整的 CRUD(创建、读取、更新、删除)系统,展示了底层系统编程。 该系统管理两种实体类型: - **獾记录** - 包含物理属性、出生数据和员工分配的动物园动物 - **员工记录** - 包含部门信息、薪资跟踪和联系方式的人员 ### 核心亮点 - **1,150+ 行** 纯手工编写的 x86-64 汇编代码 - **零高级语言依赖** - 纯汇编实现 - **手动内存管理**,使用固定大小的记录数组 - **动态字段计算**(年龄、薪资增长、自定义指标) - **完整的 CRUD 操作**,并在删除时进行数组压缩 ## 目录 - [技术架构](#technical-architecture) - [功能](#features) - [数据结构](#data-structures) - [前置条件](#prerequisites) - [安装](#installation) - [用法](#usage) - [技术实现细节](#technical-implementation-details) - [项目结构](#project-structure) - [构建说明](#build-instructions) - [展示的技能](#skills-demonstrated) - [许可证](#license) - [作者](#author) ## 技术架构 ### 技术栈 | 组件 | 技术 | |-----------|------------| | **语言** | x86-64 汇编 (NASM 语法) | | **汇编器** | NASM (Netwide Assembler) | | **链接器** | GCC (GNU Compiler Collection) | | **平台** | GNU/Linux (Kernel 3.2.0+) | | **二进制格式** | ELF 64 位 LSB 可执行文件 | | **I/O 库** | 自定义 joey_lib_io 库 | ### 系统要求 - **处理器**:兼容 x86-64(Intel/AMD 64 位) - **操作系统**:Linux(推荐 Ubuntu 20.04+) - **内存**:最低 128KB(用于分配数据结构) ### 内存布局 ``` ┌─────────────────────────────────────────────────────────────┐ │ SECTION .DATA │ │ - Menu strings and UI text │ │ - Field labels and prompts │ │ - Error/success messages │ │ - Record counters (num_current_badger_records, etc.) │ ├─────────────────────────────────────────────────────────────┤ │ SECTION .BSS │ │ - badger_array: 51,500 bytes (500 records × 103 bytes) │ │ - staff_member_array: 22,200 bytes (100 records × 222 bytes│ │ - current_month: 1 byte │ │ - current_year: 2 bytes │ ├─────────────────────────────────────────────────────────────┤ │ SECTION .TEXT │ │ - All executable functions │ │ - Main program loop │ │ - CRUD operation implementations │ └─────────────────────────────────────────────────────────────┘ ``` ## 功能 ### 核心功能 | 功能 | 描述 | |---------|-------------| | **添加记录** | 创建新的獾或员工条目,并进行完整的数据验证 | | **显示所有** | 迭代并格式化输出显示所有记录 | | **按 ID 搜索** | 使用自定义字符串比较算法进行线性搜索 | | **按 ID 删除** | 删除记录并自动进行数组压缩 | ### 计算字段 系统在显示时会自动计算派生值: #### 獾指标 - **年龄计算**:`当前年份 - 出生年份`(根据出生月份进行调整) - **条纹指数**:`质量 × 条纹数量` #### 员工指标 - **服务年限**:`当前年份 - 加入年份` - **当前薪资**:`起薪 + (£300 × 服务年限)` ### 用户界面 ``` === ZOO SYSTEM ADMIN PANEL === 1. Add Staff 2. Add Badger 3. Delete Staff 4. Delete Badger 5. Display All Staff 6. Display All Badgers 7. Search Badger 8. Search Staff 9. Exit Your Choice: _ ``` ## 数据结构 ### 獾记录结构(103 字节) | 字段 | 偏移量 | 大小 | 类型 | 描述 | |-------|--------|------|------|-------------| | `id` | 0 | 8 字节 | 字符串 | 格式:`bXXXXXX`(例如,b123456) | | `name` | 8 | 65 字节 | 字符串 | 獾的名字(最多 64 个字符 + 空字符) | | `home_sett` | 73 | 16 字节 | 字符串 | 居住位置 | | `mass_in_kg` | 89 | 1 字节 | 无符号整数 | 体重,单位 kg (0-255) | | `num_stripes` | 90 | 1 字节 | 无符号整数 | 条纹数量 (0-255) | | `sex` | 91 | 1 字节 | 字符 | 'M' 或 'F' | | `birth_month` | 92 | 1 字节 | 无符号整数 | 月份 (1-12) | | `birth_year` | 93 | 2 字节 | 无符号整数 | 年份(例如,2020) | | `assigned_staff_id` | 95 | 8 字节 | 字符串 | 关联的员工 ID | ### 员工记录结构(222 字节) | 字段 | 偏移量 | 大小 | 类型 | 描述 | |-------|--------|------|------|-------------| | `surname` | 0 | 65 字节 | 字符串 | 姓氏(最多 64 个字符 + 空字符) | | `firstname` | 65 | 65 字节 | 字符串 | 名字(最多 64 个字符 + 空字符) | | `staff_id` | 130 | 9 字节 | 字符串 | 格式:`pXXXXXXX`(例如,p1234567) | | `department` | 139 | 12 字节 | 字符串 | 例如,"Park Keeper" | | `starting_salary` | 151 | 4 字节 | 无符号整数 | 年薪(英镑) | | `year_joining` | 155 | 2 字节 | 无符号整数 | 加入年份 | | `email_address` | 157 | 65 字节 | 字符串 | 联系邮箱 | ### 数组容量 ``` Badger Array: 500 records × 103 bytes = 51,500 bytes (≈50 KB) Staff Array: 100 records × 222 bytes = 22,200 bytes (≈22 KB) ───────────────────────────────────────────────────────────────── Total Data: = 73,700 bytes (≈72 KB) ``` ## 前置条件 在构建此项目之前,请确保您已安装以下软件: ### 必需的软件 ``` # 检查是否已安装 NASM nasm --version # 预期:NASM version 2.14+ # 检查是否已安装 GCC gcc --version # 预期:gcc 9.4.0+ (Ubuntu/Debian) # 检查系统架构 uname -m # 预期:x86_64 ``` ### 安装依赖 #### Ubuntu/Debian ``` sudo apt update sudo apt install nasm gcc build-essential ``` #### Fedora/RHEL ``` sudo dnf install nasm gcc ``` #### Arch Linux ``` sudo pacman -S nasm gcc ``` ## 安装 ### 第一步:克隆仓库 ``` git clone https://github.com/haydenw-uk/badgering-about-assembly.git cd badgering-about-assembly ``` ### 第二步:获取 I/O 库 此项目需要 `joey_lib_io_v9_release.asm` 库文件。 将该库文件放置在已知位置,并更新 `mainprogram.asm` 中的 include 路径: ``` ; Line 4 of mainprogram.asm - update this path %include "/path/to/your/joey_lib_io_v9_release.asm" ``` ### 第三步:构建项目 ``` # 汇编源文件 nasm -f elf64 mainprogram.asm -o mainprogram.o # 使用 GCC 链接以创建 executable gcc -no-pie -o mainprogram mainprogram.o # 使 executable 可执行(如有需要) chmod +x mainprogram ``` ### 第四步:运行应用程序 ``` ./mainprogram ``` ## 用法 ### 初始设置 程序启动时,系统会提示您输入当前日期: ``` [SYSTEM] Enter current month (as 1-12 e.g. jan = 1): 2 [SYSTEM] Enter current year (e.g. 2024): 2025 [SYSTEM] Date set. Welcome ... ``` 此日期用于计算獾的年龄和员工的服务年限。 ### 添加员工 1. 从主菜单中选择选项 `1` 2. 出现提示时,输入各个字段的内容: ``` --- ADD A NEW STAFF MEMBER (ENSURE EACH FIELD IS CORRECTLY FILLED-IN!) --- Surname: Williams Firstname: Hayden Staff ID (pXXXXXXX where X = number): p1234567 Affiliated department: Park Keeper Starting annual salary (GBP): 28000 Year joined: 2023 Email address: h.williams@zoo.org [SUCCESS] Staff Member was added successfully! ``` ### 添加獾 1. 从主菜单中选择选项 `2` 2. 输入獾的详细信息: ``` --- ADD A NEW BADGER (ENSURE EACH FIELD IS CORRECTLY FILLED-IN!) --- Badger ID (bXXXXXX where X = number): b000001 Name: Boris Home Sett: Woodland A Mass (nearest rounded kg): 12 Number of stripes: 7 Sex (M/F): M Birth month (as 1-12 e.g. jan = 1): 5 Birth year (e.g. 2024): 2022 Assigned Staff ID (pXXXXXXX where X = number): p1234567 [SUCCESS] Badger was added successfully! ``` ### 查看记录 - **选项 5**:显示所有员工及其计算的薪资 - **选项 6**:显示所有獾及其计算的年龄和条纹指数 ### 搜索记录 - **选项 7**:通过 ID 搜索特定的獾 - **选项 8**:通过 ID 搜索特定的员工 ### 删除记录 - **选项 3**:通过 ID 删除员工 - **选项 4**:通过 ID 删除獾 ``` [DELETE] Welcome to delete. Enter the ID corresponding to the record you wish to delete: Badger ID (bXXXXXX where X = number): b000001 [SUCCESS] Deletion of record completed successfully. ``` ## 技术实现细节 ### 寄存器约定 该程序遵循 System V AMD64 ABI 调用约定: | 寄存器 | 用途 | |----------|-------| | `rax` | 返回值,算术运算 | | `rdi` | 第一个函数参数 | | `rsi` | 第二个函数参数 | | `rdx` | 第三个函数参数 | | `r12-r15` | 被调用者保存,用于持久化数据 | | `rbp` | 栈帧基指针 | | `rsp` | 栈指针 | ### 内存寻址策略 通过计算的偏移量访问记录: ``` ; Calculate address of nth record movzx rax, byte [num_current_badger_records] ; Get count imul rax, size_badger_record ; Multiply by record size lea r12, [badger_array + rax] ; Load effective address ; Access specific field within record lea rdi, [r12 + offset_badger_name] ; Address of name field ``` ### 字符串比较算法 用于 ID 搜索的自定义字符串比较: ``` compare_strings: ; rdi = string1, rsi = string2 ; Returns 0 if equal, non-zero if different .loop: mov al, [rdi] ; Load char from string1 mov bl, [rsi] ; Load char from string2 cmp al, bl ; Compare characters jne .not_equal ; Jump if different test al, al ; Check for null terminator jz .equal ; If null, strings are equal inc rdi ; Next char in string1 inc rsi ; Next char in string2 jmp .loop ``` ### 删除时的数组压缩 当删除一条记录时,后续记录会进行移位以保持连续性: ``` .shift_loop: lea rsi, [r12 + size_badger_record] ; Source: next record mov rdi, r12 ; Dest: current position mov rcx, size_badger_record ; Bytes to copy rep movsb ; Copy record add r12, size_badger_record ; Move to next position dec rcx ; Decrement counter jmp .shift_loop ``` ### 栈帧管理 每个函数都正确管理其栈帧: ``` function_name: push rbp ; Save old base pointer mov rbp, rsp ; Establish new frame sub rsp, 32 ; Allocate local variables push r12 ; Save callee-saved registers push r13 ; ... function body ... pop r13 ; Restore registers pop r12 add rsp, 32 ; Deallocate locals pop rbp ; Restore old frame ret ``` ## 项目结构 ``` badgering-about-assembly/ │ ├── mainprogram.asm # Main assembly source file (1,150+ lines) │ ├── section .data # Constant strings and initialized data │ ├── section .bss # Uninitialized data (arrays, counters) │ └── section .text # Executable code │ ├── main # Entry point and menu loop │ ├── add_a_new_staff_record │ ├── add_a_new_badger_record │ ├── display_staff_member_record │ ├── display_all_staff_member_records │ ├── display_badger_record │ ├── display_all_badger_records │ ├── search_staff_member_by_id │ ├── search_badger_by_id │ ├── delete_staff_member_by_id │ ├── delete_badger_by_id │ └── compare_strings │ ├── mainprogram.o # Compiled object file ├── mainprogram # Final executable ├── README.md # This documentation └── .git/ # Version control ``` ## 构建说明 ### 快速构建 ``` # One-liner 构建命令 nasm -f elf64 mainprogram.asm -o mainprogram.o && gcc -no-pie -o mainprogram mainprogram.o ``` ### 调试构建 ``` # 构建包含 debug symbols nasm -f elf64 -g -F dwarf mainprogram.asm -o mainprogram.o gcc -no-pie -g -o mainprogram mainprogram.o # 使用 GDB 进行调试 gdb ./mainprogram ``` ### 用于调试的常见 GDB 命令 ``` (gdb) break main # Set breakpoint at main (gdb) run # Start execution (gdb) info registers # View all registers (gdb) x/10x $rsp # Examine stack (gdb) x/s &badger_array # View badger array (gdb) stepi # Single instruction step ``` ### 故障排除 | 问题 | 解决方案 | |-------|----------| | `nasm: command not found` | 安装 NASM:`sudo apt install nasm` | | `undefined reference to 'main'` | 确保声明了 `global main` | | 段错误 | 检查栈对齐(调用时需 16 字节边界对齐) | | 找不到库 | 更新源文件中的 `%include` 路径 | ## 展示的技能 此项目展示了对以下方面的熟练掌握: ### 底层编程 - x86-64 汇编语言和指令集 - 无需垃圾回收的手动内存管理 - 直接的硬件寄存器操作 - 对 CPU 架构和数据流的理解 ### 系统编程 - Linux 系统调用和二进制格式 (ELF) - 符合 ABI 规范 (System V AMD64) - 栈帧管理和调用约定 - 函数调用间的正确寄存器保护 ### 数据结构与算法 - 采用手动索引的固定大小数组实现 - 带有自定义字符串比较的线性搜索 - 用于删除操作的数组压缩算法 - 用于字段访问的记录偏移量计算 ### 软件工程 - 汇编中的模块化函数设计 - 全面的内联文档 - 结合版本控制 (Git) 的增量开发 - 防御性编程(边界检查、数据验证) ### 问题解决 - 将高级 CRUD 概念转换为机器码 - 手动字节级数据结构设计 - 用于动态计算的算术运算 - 无高级构造的控制流实现 ## 限制 - **无持久化**:数据仅存储在内存中,退出后会丢失 - **无更新操作**:记录创建后无法编辑 - **线性搜索**:O(n) 搜索复杂度 - **固定容量**:最多容纳 500 只獾和 100 名员工 - **特定平台**:仅限 Linux x86-64 环境 ## 未来增强功能 扩展开发中潜在的改进方向: - [ ] 用于持久化数据存储的文件 I/O - [ ] 现有记录的更新/编辑功能 - [ ] 基于已排序数组的二分搜索 - [ ] 输入验证和清理 - [ ] 跨平台支持 (Windows, macOS) ## 许可证 ## 此项目是作为我在牛津布鲁克斯大学一个模块的恶意软件学术课程作业的一部分而开发的。 ## 作者 **Hayden Williams** 牛津布鲁克斯大学,2025 年

展示即使是复杂的软件系统也可以完全使用汇编语言从头构建。

标签:x86-64, 信息管理系统, 大学课程作业, 安全报告生成, 快速连接, 汇编语言, 系统编程