Core System Programming Concepts & Memory Management 核心系统编程概念与内存管理
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源码到运行进程的完整过程。
When your program runs, the operating system allocates memory and organizes it into four distinct regions, each with specific purposes and characteristics:当程序运行时,操作系统分配内存并将其组织成四个不同的区域,每个区域都有特定的目的和特征:
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)数据结构,用于管理函数调用。 每次函数调用都会创建一个“栈帧”,包含局部变量、参数和返回地址。
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函数返回值 |
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源代码转换为运行程序涉及四个不同的阶段。 理解每个阶段可以帮助你调试编译错误并优化程序。
Handles all directives starting with #. Includes header files and expands macros.处理所有以#开头的指令。包含头文件并展开宏。
Translates C code into human-readable assembly language (.s file). This is the core of COMP1521!将C代码翻译成人类可读的汇编语言(.s文件)。这是COMP1521的核心!
Converts assembly into binary machine code (.o object file). Not human-readable.将汇编转换为二进制机器代码(.o目标文件)。不可人类读取。
Combines object files and library code into final executable. Resolves function addresses.将目标文件和库代码组合为最终的可执行文件。解析函数地址。
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代码和实际机器操作之间的桥梁。
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)(无) |
Here's a simple MIPS program that reads a number and prints its square:这是一个简单的MIPS程序,读取一个数字并打印它的平方:
Labels are human-readable names for memory addresses. They make code more maintainable and are essential for implementing loops and function calls. 标签是内存地址的人类可读名称。它们使代码更容易维护,并且对实现循环和函数调用至关重要。
Test your understanding of memory layout, function calls, compilation pipeline, and MIPS assembly fundamentals with 50 carefully crafted questions.通过50个精心设计的问题,测试你对内存布局、函数调用、编译流水线和MIPS汇编基础的理解。