Back to COMP1521 返回 COMP1521

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学习目标

🧩 Exam Alignment与考试练习的对应关系

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易错点提醒

🛠️ 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 周

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资源与清单