计算机实验室之树莓派：课程 0 简介
树莓派
![](/data/attachment/album/201901/09/225414hclxe1ymhcme5cmi.jpg)


这个课程简介不包含实践内容，但它解释了一个操作系统的基本概念、汇编代码、和其它很重要的一些基本原理。如果你想直接进入实践环节，跳过本课程并不会有什么问题。


### 1、操作系统


操作系统就是一个非常复杂的程序。它的任务就是组织安排计算机上的其它程序，包括共享计算机的时间、内存、硬件和其它资源。你可能听说过的一些比较大的桌面操作系统家族有 GNU/Linux、Mac OS X 和 Microsoft Windows。其它的设备比如电话，也需要操作系统，它可能使用的操作系统是 Android、iOS 和 Windows Phone。 <sup id="fnref1"> <a href="#fn1" rel="footnote">  1 </a></sup>


由于操作系统是用来与计算机系统上的硬件进行交互的，所以它必须了解系统上硬件专有的信息。为了能让操作系统适用于各种类型的计算机，发明了 **驱动程序** 的概念。驱动程序是为了能够让操作系统与特定的硬件进行交互而添加（并可删除）到操作系统上的一小部分代码。在本课程中，我们并不涉及如何创建可删除的驱动程序，而是专注于特定的一个硬件：树莓派。


操作系统有各种不同的设计方式，在本课程中，我们只触及操作系统设计的皮毛。本课程中，我们主要专注于操作系统与各种硬件的交互部分，因为这经常是比较棘手的部分，并且也是在网络上文档和帮助最少的部分。


### 2、汇编代码



> 
> 处理器每秒可以执行上百万的指令，但是这些指令必须要简单。
> 
> 
> 


本课程几乎要完全靠汇编代码来写。汇编代码非常接近计算机的底层。计算机其实是靠一个叫处理器的设备来工作的，处理器能够执行像加法这样的简单任务，还有一组叫做 RAM 的芯片，它能够用来保存数字。当计算机通电后，处理器执行程序员给定的一系列指令，这将导致内存中的数字发生变化，以及与连接的硬件进行交互。汇编代码只是将这些机器命令转换为人类可读的文本。


常规的编程就是，程序员使用编程语言，比如 C++、Java、C#、Basic 等等来写代码，然后一个叫编译器的程序将程序员写的代码转换成汇编代码，然后进一步转换为二进制代码。<sup id="fnref2"> <a href="#fn2" rel="footnote">  2 </a></sup> 二进制代码才是计算机真正能够理解的东西，但它是人类无法读取的东西。汇编代码比二进制代码好一点，至少它的命令是人类可读的，但它仍然让人很沮丧。请记住，你用汇编代码写的每个命令都是处理器可以直接认识的，因此这些命令设计的很简单，因为物理电路必须能够处理每个命令。


![Compiler process](/data/attachment/album/201901/16/011504ukcpyy997wz568q9.png)


和普通编程一样，也有很多不同的汇编代码编程语言，但与普通编程不一样的是，每个汇编编程语言是面对不同的处理器的，每种处理器设计为去理解不同的语言。因此，用一个针对某种机器设计的汇编语言所写的汇编代码，是不能在其它种类的机器上运行的。很多情况下，这都是一个大灾难，因此每个程序都必须在使用它的不同种类的机器上重写一遍，但对于操作系统，这不是个问题，因为在不同的硬件上它必须得重写。尽管如此，大多数操作系统都是用 C++ 或 C 来写的，这样它们就可以很容易地在不同种类的硬件上使用，只需要重写那些必须用汇编代码来实现的部分即可。


现在，你已经准备好进入第一节课了，它是 [课程 1 OK01](https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/ok01.html)。




---


1. 要查看更完整的操作系统列表，请参照：[操作系统列表 - Wikipedia](http://en.wikipedia.org/wiki/List_of_operating_systems) [↩](#fnref1)
2. 当然，我简化了普通编程的这种解释，实际上它在很大程度上取决于语言和机器。感兴趣的话，参见 [编译器 - Wikipedia](http://en.wikipedia.org/wiki/Compiler) [↩](#fnref2)




---


via: <https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/introduction.html>


作者：[Robert Mullins](http://www.cl.cam.ac.uk/%7Erdm34) 选题：[lujun9972](https://github.com/lujun9972) 译者：[qhwdw](https://github.com/qhwdw) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Raspberry Pi

# Lesson 0 Introduction

This introductory lesson does not contain a practical element, but exists to explain the basic concepts of what is an operating system, what is assembly code and other important basics. If you just want to get straight into practicals, it should be safe to skip this lesson.

Contents |


## 1 Operating Systems

Throughout these tutorials I will put interesting information in boxes like this one.

Throughout these tutorials I will put information about each command we learn in boxes like this one.

An operating system is just a very complicated program. It has the job of organising
other programs on a computer, including sharing the computer's time, memory, hardware
and other resources. Some big families of desktop operating systems that you may
have heard of include GNU/Linux, Mac OS X and Microsoft Windows. Other devices also
need operating systems such as phones, which may use operating systems such as Android,
iOS and Windows Phone.[1]

Since the operating system has to interact with the hardware on a computer system,
it also has to have specific knowledge of the hardware on a system. To allow operating
systems to be used on a variety of computers, the concept of **drivers**
was invented. Drivers are small bits of code that can be added and removed from
the operating system in order to allow the operating system to talk to a particular
piece of hardware. In this course, we do not cover how to create such removable
drivers, and instead focus on making specific ones for the Raspberry Pi.

There are all kinds of different designs of operating systems, and this course can only just scratch the surface. In this course we will mainly focus on getting the operating system to interact with a variety of bits of hardware, as this is often the trickiest bit, and the part for which the least documentation and help exists online.

## 2 Assembly Code

A processor can often perform millions of instructions per second, but they must be simple.

This course will be written almost exclusively in assembly code. Assembly code is
code that is *very* close to what the computer understands. How a computer
really works is that there is a small device called a processor which is capable
of performing simple jobs like adding numbers, and there is a set of one or more
microchips called the **RAM** which are
capable of storing numbers. When a computer has power, the processor works through
a sequence of instructions given to it by the programmer, which cause it to change
numbers in the RAM, and interact with connected hardware. Assembly code is a translation
into human readable text of those commands.

When programming normally, the programmer writes code in a programming language
such as C++, Java, C#, Basic, etc, and then a program called the compiler translates
what the programmer wrote into assembly code, which is the further reduced into
binary code[ [2]](#note2).
Binary code is what the computer actually understands, but it is almost impossible
for humans to read. Assembly code is much better, but it can be frustrating how
few commands are possible. Remember that every command you write in assembly code
is something that the processor understands directly, and so the commands are simple
by design, as a physical circuit must process each one.

![Compiler process](images/compiling.png)

Just like with ordinary programming, there are many different assembly code languages, however unlike ordinary programming, the reason these exist is due to the fact that there exists many different processors, each designed to understand a different language. Thus a program written in assembly code for one machine, will not work on a different one. For most things, this would be a disaster as each program would have to be rewritten for every system it was used on, but for operating systems this isn't so much of a problem, as it would have to be rewritten anyway due to differing hardware. Nevertheless, most operating systems are written in C++ or C, so that they can be converted more easily, and only the sections that absolutely have to be written in assembly are.

You're now ready to move on to the first lesson, [Lesson 1: OK01](ok01.html)