Week 5 — Systems I/O & Data Representation
第 5 周 — 系统 I/O 与数据表示
Packed digits, endian conversions, buffered input, and BCD arithmetic for lab and exam mastery
打包数字、端序转换、缓冲输入与 BCD 算术,直连实验与考试掌握
🎯 Learning Objectives学习目标
-
Implement buffered character I/O in C using `fgets`, `scanf`, and manual parsing as practised in Lab05 `sixteen_in`/`sixteen_out`.
掌握 `fgets`、`scanf` 及手动解析,实现 Lab05 `sixteen_in`/`sixteen_out` 中的缓冲字符 I/O。
-
Convert between endian formats with bitwise masks and shifts, applying the `nuxi.s` template.
利用位掩码与移位完成端序转换,应用 `nuxi.s` 模板。
-
Encode/decode packed decimal (BCD) numbers in C, including carry handling and digit iteration (`bcd.c`, `bcd_add.c`, `bcd_arithmetic.c`).
在 C 语言中实现打包十进制(BCD)的编码/解码,处理进位与数字迭代,覆盖 `bcd.c`、`bcd_add.c`、`bcd_arithmetic.c`。
-
Trace static storage, `malloc` reuse, and string manipulation in Tutorial05 tasks, mapping to exam-style file-processing questions.
跟踪 Tutorial05 中的静态存储、`malloc` 复用与字符串处理,对接考试的文件处理题型。
-
Design testing harnesses for conversion routines, logging mismatches for iterative debugging.
为转换例程设计测试脚本,记录不匹配并进行迭代调试。
🧩 Exam Alignment与考试练习的对应关系
-
BCD arithmetic — 22T2 Q8, 20T2 Q10 emphasise digit loops and carry propagation; Lab05 `bcd_add.c` mirrors scoring rubric.
BCD 算术 — 22T2 Q8、20T2 Q10 聚焦数字循环与进位传播;Lab05 `bcd_add.c` 与评分标准一致。
-
Endian conversions — 22T3 Q5, Lab05 `nuxi.s` demand byte extraction templates.
端序转换 — 22T3 Q5、Lab05 `nuxi.s` 需要字节提取模板。
-
String/IO sanitation — Tutorial05 Q1–Q8 map to exam rapid-fire safety checks.
字符串/I/O 安全 — Tutorial05 第 1–8 题映射到考试快问快答的安全检查。
-
Static vs dynamic storage — Q10/Q11 explore `static` semantics, supporting exam memory model questions.
静态与动态存储 — 第 10/11 题讨论 `static` 语义,对应考试的内存模型题。
-
File packing — Lab05 complements exam tasks requiring binary file output (22T2 Q9 style). Optional notes highlight extension but limit to 10% time.
文件打包 — Lab05 补充二进制输出题(类似 22T2 Q9),扩展练习标记为 Optional,时间不超过 10%。
Coverage Targets
覆盖目标
Complete ≥4 endian test cases, ≥6 BCD addition/subtraction drills, and capture buffer sanitation checklist for each I/O task. Link each checklist item to specific lab/exam references.
完成 ≥4 个端序测试用例、≥6 个 BCD 加减训练,并为每个 I/O 任务记录缓冲区安全清单;将每项清单与对应实验/试题编号关联。
📚 Core Concepts核心概念
Buffered Input & Validation
缓冲输入与合法性检查
Use `fgets` into a fixed buffer, strip newline, then validate with `strspn` or manual loops. Guard length before conversion to avoid overflow.
先用 `fgets` 读入固定缓冲区,去掉换行,再通过 `strspn` 或手写循环验证字符;转换前检查长度,防止溢出。
Endian Conversion Template
端序转换模板
Extract bytes using masks (`0xFF << shift`), shift to new positions, reassemble with OR. Extend to 64-bit by iterating over nibble pairs.
通过掩码(`0xFF << shift`)提取字节,移位到新位置,再用 OR 合并。对于 64 位可遍历两位十六进制一组。
Packed BCD Internals
打包 BCD 内部结构
Each byte stores two digits (high nibble, low nibble). Iteration: read nibble → convert to decimal → accumulate → handle carry using mod/div by 10.
每个字节存两个数字(高四位、低四位)。迭代流程:取出半字节转换为十进制 → 累加 → 通过除/模 10 处理进位。
Carry Propagation Strategy
进位传播策略
Store intermediate sums in integers, update carry before writing nibble. When adding arrays, process from least significant nibble to most.
将中间结果保存在整数中,在写回半字节前更新进位。数组加法从最低有效半字节向高位遍历。
Testing Harness Design
测试框架设计
Compare expected vs actual strings with `strcmp`, log mismatches to stderr, regenerate summary metrics. Integrate into `Makefile` or simple shell script.
用 `strcmp` 比较期望与实际字符串,将不匹配输出到 stderr,并重新计算汇总指标。可写入 `Makefile` 或脚本自动化。
🧪 Worked Examples例题与讲解
Example 1 — `sixteen_in.c` Buffer Parsing
例 1 — `sixteen_in.c` 缓冲解析
Read a hex string with `fgets`, validate with `strspn(buffer, "0123456789abcdefABCDEF\n")`, strip newline, then `strtol` to convert. On error, print bilingual message and exit.
用 `fgets` 读取十六进制字符串,利用 `strspn(buffer, "0123456789abcdefABCDEF\n")` 验证,去掉换行,再用 `strtol` 转换;若出错,打印中英错误提示后退出。
Exam tie-in: 22T2 Q7 expects identical validation steps.
考试关联:22T2 Q7 对应相同的输入验证步骤。
Example 2 — `nuxi.s` Endian Swap
例 2 — `nuxi.s` 端序转换
Given 32-bit word in `$a0`, extract bytes via `andi` + shifts, reorder using `sll`/`srl`, accumulate with `or`. Template reuses loop for 64-bit by processing two bytes at a time.
对 `$a0` 中的 32 位数据,先用 `andi`+移位提取字节,再通过 `sll`/`srl` 重排,用 `or` 合并。扩展到 64 位时可每次处理两个字节。
Example 3 — `bcd_add.c` Carry Loop
例 3 — `bcd_add.c` 进位循环
Iterate from least significant nibble: extract digits with helper `get_digit(packed, index)`, sum with carry, store `sum % 10`, update `carry = sum / 10`. After loop, append carry if non-zero.
从最低有效半字节开始:用 `get_digit(packed, index)` 取数字,与进位相加,写回 `sum % 10`,更新 `carry = sum / 10`,循环结束后若 carry 非零则追加。
Exam 22T2 Q8 uses identical algorithm; annotate reasons for modulo/div to earn reasoning marks.
22T2 Q8 与算法一致;写明使用除/模的原因可获得推理分。
⚠️ Common Pitfalls易错点提醒
-
Forgetting to null-terminate buffers after manual parsing.
手动解析后忘记补充字符串终止符。
-
Reversing byte order without masking first, causing sign extension artefacts.
端序转换时未先取掩码直接移位,导致符号扩展污染结果。
-
Iterating BCD digits from most significant end, which fails carry propagation.
BCD 遍历从最高位开始,会导致进位处理失败。
-
Not resetting `carry`/`borrow` before new operations, leading to stale state.
未在新运算前将 `carry`/`borrow` 复位,旧状态残留。
🛠️ Practice Task实践任务
Create `ledger_tool.c`: read two newline-separated decimal strings, convert to packed BCD arrays, compute sum and difference, output bilingual summaries (`printf` English/Chinese). Include functions `pack_digits`, `bcd_add`, `bcd_sub`, and `format_bcd`.
实现 `ledger_tool.c`:读入两个换行分隔的十进制字符串,转换成打包 BCD 数组,计算和与差,并以中英双语打印汇总。实现 `pack_digits`、`bcd_add`、`bcd_sub`、`format_bcd` 四个函数。
OptionalOptional: add file mode that reads binary ledger files and validates checksum (≤10% extra time).
可选Optional:增加读取二进制账本并校验校验和的文件模式(额外时间 ≤10%)。
🧪 Tutorial & Lab Mapping教程与实验映射
Tutorial 05 Highlights
Tutorial 05 重点
-
Q1–Q4: pointer arithmetic on arrays of structs, prepping for BCD packing.
第 1–4 题:结构体数组指针运算,为 BCD 打包做准备。
-
Q5–Q8: string manipulation and buffer safety (matching `sixteen_in/out`).
第 5–8 题:字符串处理与缓冲安全,对应 `sixteen_in/out`。
-
Q9–Q12: `static` semantics, `realloc` behaviour — reuse for exam memory model questions.
第 9–12 题:`static` 语义与 `realloc` 行为,可复用于考试内存模型题。
Lab 05 Programming Tasks
Lab 05 编程任务
-
`sixteen_in.c` / `sixteen_out.c`: buffered hex input/output.
`sixteen_in.c` / `sixteen_out.c`:缓冲十六进制输入/输出。
-
`bcd.c` / `packed_bcd.c`: basic packing/unpacking helpers.
`bcd.c` / `packed_bcd.c`:打包/解包基础函数。
-
`bcd_add.c`: multi-digit addition with carry tracking.
`bcd_add.c`:含进位的多位加法。
-
`nuxi.s`: assembly endian conversion aligning with exam templates.
`nuxi.s`:端序转换汇编,与考试模板同步。
-
`bcd_arithmetic.c`: combines add/sub routines into CLI tool (core take-home pattern).
`bcd_arithmetic.c`:整合加减例程生成命令行工具,是核心作业模式。
📝 Study Log — Week 5学习记录 — 第 5 周
-
Inputs shared with AI: Tutorial 05 page, Lab 05 page (`sixteen_in`, `sixteen_out`, `bcd`, `packed_bcd`, `bcd_add`, `bcd_arithmetic`, `nuxi`), exams 22T2 Q8/Q9, 22T3 Q5, PPT `integers.pdf`, `bitwise_operations.pdf`.
向 AI 提供的资料:Tutorial 05、Lab 05(`sixteen_in`、`sixteen_out`、`bcd`、`packed_bcd`、`bcd_add`、`bcd_arithmetic`、`nuxi`)、往年试题 22T2 Q8/Q9、22T3 Q5、PPT《integers.pdf》《bitwise_operations.pdf》。
-
Key prompt: “Generate BCD carry templates tied to Lab05 tasks and exam Q8.”
核心提示词:“生成与 Lab05 及往年 Q8 对齐的 BCD 进位模板。”
-
What clicked: Logging mismatch cases accelerates debugging; aligning buffer sanitation checklist with tutorial questions prevents oversight.
收获:记录不匹配输入有助于快速调试;将缓冲安全清单与教程题对齐能避免遗漏。
-
Misconceptions fixed: Previously attempted to store decimal digits directly in bytes; clarified need for packed nibble representation.
修正误区:原以为可直接把十进制数字放进字节,现已明确必须使用打包半字节格式。
-
Still shaky: Binary file checksum extension (marked Optional). To revisit post-week tasks.
仍待巩固:二进制文件校验扩展(标记 Optional),待本周任务完成后复习。
-
Next actions: Finish `ledger_tool` CLI, craft 6 regression tests, rerun `1521 autotest lab05_bcd_arithmetic`.
后续行动:完成 `ledger_tool` 命令行工具,编写 6 个回归测试,重新运行 `1521 autotest lab05_bcd_arithmetic`。
Premium Quiz — I/O & Representation
Premium 测验 — I/O 与数据表示
28 basic (buffers, masks), 8 intermediate (endian loops, BCD helpers), 4 advanced (file pipelines, arithmetic edge cases). Unlock with membership.
基础 28 题(缓冲、掩码),中级 8 题(端序循环、BCD 工具),高级 4 题(文件流水线、算术边界)。会员解锁后可使用。
Go to Week 5 Quiz
进入第 5 周测验
🔭 Next Week Preview下周预告
Week 6 transitions to memory management and virtual memory; gather notes on `malloc`, `free`, and paging ahead of schedule.
第 6 周将转向内存管理与虚拟内存,提前整理 `malloc`、`free` 与分页的资料。
📎 Resources & Checklist资源与清单
-
PPT: `bitwise_operations.pdf` pages 25–44, `integers.pdf` pages 30–42.
PPT:`bitwise_operations.pdf` 第 25–44 页,`integers.pdf` 第 30–42 页。
-
Autotest targets: `1521 autotest lab05_sixteen_in`, `lab05_sixteen_out`, `lab05_bcd`, `lab05_bcd_add`, `lab05_bcd_arithmetic`.
自动测评:`1521 autotest lab05_sixteen_in`、`lab05_sixteen_out`、`lab05_bcd`、`lab05_bcd_add`、`lab05_bcd_arithmetic`。
-
Reflection prompts: “Did buffer sanitation happen before numeric conversion? Are carry/borrow variables reset each iteration?”
反思提示:“在数字转换前是否完成缓冲区验证?每次循环是否重置进位/借位变量?”