Back to COMP1521返回 COMP1521

COMP1521 Week 1 Fundamentals COMP1521 第一周基础知识

Core System Programming Concepts & Memory Management 核心系统编程概念与内存管理

📚 Course Overview 📚 课程概览

What is COMP1521? 什么是COMP1521?

COMP1521 bridges the gap between high-level programming (C) and low-level computer systems (assembly/machine code). You'll learn how programs actually execute on real hardware, understanding the journey from C source code to running processes. COMP1521架起了高级编程(C语言)与底层计算机系统(汇编/机器码)之间的桥梁。你将学习程序如何在真实硬件上执行,理解从C源码到运行进程的完整过程。

Core Learning Objectives 核心学习目标

  • Memory Management: Understand how programs organize data in memory (stack, heap, data, text sections)
  • C-to-Assembly Translation: Master the ability to convert C code into equivalent MIPS assembly
  • System Programming: Learn how programs interact with the operating system through system calls
  • Low-level Debugging: Develop skills to trace program execution at the assembly level
  • Performance Understanding: Comprehend why certain programming patterns are more efficient
  • 内存管理: 理解程序如何在内存中组织数据(栈、堆、数据、代码段)
  • C到汇编的转换: 掌握将C代码转换为等价MIPS汇编的能力
  • 系统编程: 学习程序如何通过系统调用与操作系统交互
  • 底层调试: 培养在汇编级别追踪程序执行的技能
  • 性能理解: 理解为什么某些编程模式更高效
Key Insight: 关键洞察: Every high-level operation in C (variables, functions, loops) has a direct, predictable translation to low-level machine instructions. Understanding this mapping is the foundation of systems programming. C语言中的每一个高级操作(变量、函数、循环)都有直接、可预测的底层机器指令转换。理解这种映射关系是系统编程的基础。

🧠 Program Memory Layout程序内存布局

The Four Memory Sections四个内存区段

When your program runs, the operating system allocates memory and organizes it into four distinct regions, each with specific purposes and characteristics:当程序运行时,操作系统分配内存并将其组织成四个不同的区域,每个区域都有特定的目的和特征:

TEXT SECTION (Code)代码段
📖 Read-only compiled machine instructions📖 只读编译机器指令
DATA SECTION数据段
🌍 Global & static variables (entire program lifetime)🌍 全局和静态变量(整个程序生命周期)
HEAP
💾 Dynamic memory (malloc/free) - grows upward💾 动态内存(malloc/free)- 向上增长
STACK
📚 Function calls & local variables - grows downward📚 函数调用和局部变量 - 向下增长

Memory Section Examples内存区段示例

// Global variables → DATA SECTION int global_counter = 0; char company_name[] = "UNSW"; int main(void) { // Local variables → STACK int local_var = 42; char buffer[100]; // Dynamic allocation → HEAP int *dynamic_array = malloc(10 * sizeof(int)); // Static local → DATA SECTION static int persistent_counter = 0; free(dynamic_array); return 0; }
Critical Understanding:关键理解: Variable lifetime and accessibility are determined by which memory section they reside in. Stack variables are destroyed when functions return, while data section variables persist for the entire program execution.变量的生命周期和可访问性由它们所在的内存区段决定。栈变量在函数返回时被销毁,而数据段变量在整个程序执行期间持续存在。

📞 Function Calls & The Call Stack函数调用和调用栈

The Call Stack Concept调用栈概念

The call stack is a Last-In-First-Out (LIFO) data structure that manages function calls. Each function call creates a "stack frame" containing local variables, parameters, and the return address. 调用栈是一个后进先出(LIFO)数据结构,用于管理函数调用。 每次函数调用都会创建一个“栈帧”,包含局部变量、参数和返回地址。

// Example: Tracing function calls int add(int a, int b) { int result = a + b; // Local variable in add's frame return result; } int calculate(int x) { int doubled = x * 2; // Local in calculate's frame return add(doubled, 10); // Calls add() } int main(void) { int result = calculate(5); // Calls calculate() return 0; }
Stack Execution Trace栈执行跟踪
add() frameadd()栈帧
a=10, b=10, result=20, return_addra=10, b=10, result=20, 返回地址
calculate() framecalculate()栈帧
x=5, doubled=10, return_addrx=5, doubled=10, 返回地址
main() framemain()栈帧
result=?, return_addrresult=?, 返回地址

MIPS Register ConventionsMIPS寄存器约定

To ensure functions can work together, MIPS follows specific conventions about which registers can be used freely and which must be preserved:为确保函数能够协同工作,MIPS遵循特定的约定,规定哪些寄存器可以自由使用,哪些必须保留:

Register Type寄存器类型 Names名称 Responsibility责任 Usage用法
Temporary临时寄存器 $t0-$t9 Caller-saved调用者保存 Free to use, but may be overwritten by function calls可自由使用,但可能被函数调用覆盖
Saved保存寄存器 $s0-$s7 Callee-saved被调用者保存 Must preserve original value if used如使用必须保留原值
Arguments参数寄存器 $a0-$a3 Function args函数参数 Pass parameters to functions向函数传递参数
Return返回寄存器 $v0-$v1 Return values返回值 Function return values函数返回值
Key Insight:关键洞察: Understanding the call stack explains why local variables disappear after function returns, why recursion can cause stack overflow, and why register conventions are essential for function interoperability.理解调用栈可以解释为什么局部变量在函数返回后消失,为什么递归可能导致栈溢出,以及为什么寄存器约定对函数互操作性至关重要。

⚙️ C Compilation PipelineC编译流水线

From C Source to Executable从C源代码到可执行文件

Converting C source code to a running program involves four distinct stages. Understanding each stage helps you debug compilation errors and optimize your programs. 将C源代码转换为运行程序涉及四个不同的阶段。 理解每个阶段可以帮助你调试编译错误并优化程序。

🔍
1. Preprocessor1. 预处理器
clang -E
🔧
2. Compiler2. 编译器
clang -S
🔩
3. Assembler3. 汇编器
clang -c
🔗
4. Linker4. 链接器
clang

Detailed Stage Analysis详细阶段分析

1. Preprocessor (clang -E)1. 预处理器 (clang -E)

Handles all directives starting with #. Includes header files and expands macros.处理所有以#开头的指令。包含头文件并展开宏。

// Before preprocessing: #include <stdio.h> #define MAX_SIZE 100 int main() { char buffer[MAX_SIZE]; printf("Hello\n"); } // After preprocessing (simplified): /* ... hundreds of lines from stdio.h ... */ int main() { char buffer[100]; // MAX_SIZE replaced printf("Hello\n"); }

2. Compiler (clang -S)2. 编译器 (clang -S)

Translates C code into human-readable assembly language (.s file). This is the core of COMP1521!将C代码翻译成人类可读的汇编语言(.s文件)。这是COMP1521的核心!

3. Assembler (clang -c)3. 汇编器 (clang -c)

Converts assembly into binary machine code (.o object file). Not human-readable.将汇编转换为二进制机器代码(.o目标文件)。不可人类读取。

4. Linker (clang)4. 链接器 (clang)

Combines object files and library code into final executable. Resolves function addresses.将目标文件和库代码组合为最终的可执行文件。解析函数地址。

COMP1521 Focus:COMP1521重点: Stage 2 (Compiler) is where C becomes assembly. You'll learn to write the assembly code that the compiler would generate, giving you complete control over program execution.第2阶段(编译器)是C变成汇编的地方。你将学会编写编译器会生成的汇编代码,从而完全控制程序执行。

🖥️ MIPS Assembly FundamentalsMIPS汇编基础

What is MIPS?什么是MIPS?

MIPS (Microprocessor without Interlocked Pipeline Stages) is a simplified assembly language that helps you understand how processors execute instructions. It's the bridge between high-level C code and actual machine operations. MIPS(无互锁流水线阶段的微处理器)是一种简化的汇编语言, 帮助你理解处理器如何执行指令。它是高级C代码和实际机器操作之间的桥梁。

System Calls - Talking to the OS系统调用 - 与操作系统对话

Since user programs can't directly access hardware (for security), they must request services from the operating system kernel through system calls. 由于用户程序不能直接访问硬件(出于安全考虑),它们必须通过系统调用向操作系统内核请求服务。

Call #调用号 Service服务 $a0 Register$a0寄存器 $v0 Returns$v0返回
1 print_int Integer to print要打印的整数 (none)(无)
4 print_string String address字符串地址 (none)(无)
5 read_int (none)(无) User input integer用户输入整数
10 exit (none)(无) (program ends)(程序结束)
11 print_char ASCII character valueASCII字符值 (none)(无)

Your First MIPS Program你的第一个MIPS程序

Here's a simple MIPS program that reads a number and prints its square:这是一个简单的MIPS程序,读取一个数字并打印它的平方:

# Calculate and print the square of a number .data prompt_msg: .asciiz "Enter a number: " .text main: # Print prompt li $v0, 4 # syscall 4: print_string la $a0, prompt_msg # load string address syscall # execute system call # Read integer li $v0, 5 # syscall 5: read_int syscall move $t0, $v0 # store input in $t0 # Calculate square mul $t1, $t0, $t0 # $t1 = $t0 * $t0 # Print result li $v0, 1 # syscall 1: print_int move $a0, $t1 # move result to $a0 syscall # Exit program li $v0, 10 # syscall 10: exit syscall

Labels - Memory Address Aliases标签 - 内存地址别名

Labels are human-readable names for memory addresses. They make code more maintainable and are essential for implementing loops and function calls. 标签是内存地址的人类可读名称。它们使代码更容易维护,并且对实现循环和函数调用至关重要。

Data Labels数据标签

.data message: .asciiz "Hello" numbers: .word 1, 2, 3, 4 # 'message' and 'numbers' are # aliases for memory addresses

Code Labels代码标签

loop_start: # ... loop body ... blt $t0, 10, loop_start # 'loop_start' marks an # instruction address
Essential Skill:基本技能: Learning to translate C constructs (variables, loops, functions) into MIPS assembly is the core competency of COMP1521. Every C program has a direct, predictable MIPS equivalent.学会将C语言结构(变量、循环、函数)翻译成MIPS汇编是COMP1521的核心能力。每个C程序都有直接、可预测的MIPS等价物。

🎯 Comprehensive Quiz综合测验

🧠

COMP1521 Fundamentals Mastery QuizCOMP1521基础精通测验

Test your understanding of memory layout, function calls, compilation pipeline, and MIPS assembly fundamentals with 50 carefully crafted questions.通过50个精心设计的问题,测试你对内存布局、函数调用、编译流水线和MIPS汇编基础的理解。

📊 35 Basic Questions35个基础问题 🎯 10 Intermediate10个中级 🔥 5 Advanced5个高级

Topics Covered涉及主题

  • Memory layout and variable lifetimes内存布局和变量生命周期
  • Function call stack and register conventions函数调用栈和寄存器约定
  • C compilation pipeline stagesC编译流水线阶段
  • MIPS assembly basics and system callsMIPS汇编基础和系统调用
  • Pointer arithmetic and memory management指针算数和内存管理
  • C-to-MIPS translation techniquesC到MIPS的翻译技术