计算机实验室之树莓派：课程 3 OK03
树莓派
![](/data/attachment/album/201902/08/225754zmxkmdok4dx83ybz.jpg)


OK03 课程基于 OK02 课程来构建，它教你在汇编中如何使用函数让代码可复用和可读性更好。假设你已经有了 [课程 2：OK02](/article-10478-1.html) 的操作系统，我们将以它为基础。


### 1、可复用的代码


到目前为止，我们所写的代码都是以我们希望发生的事为顺序来输入的。对于非常小的程序来说，这种做法很好，但是如果我们以这种方式去写一个完整的系统，所写的代码可读性将非常差。我们应该去使用函数。



> 
> 一个函数是一段可复用的代码片断，可以用于去计算某些答案，或执行某些动作。你也可以称它们为<ruby> 过程 <rt>  procedure </rt></ruby>、<ruby> 例程 <rt>  routine </rt></ruby>或<ruby> 子例程 <rt>  subroutine </rt></ruby>。虽然它们都是不同的，但人们几乎都没有正确地使用这个术语。
> 
> 
> 你应该在数学上遇到了函数的概念。例如，余弦函数应用于一个给定的数时，会得到介于 -1 到 1 之间的另一个数，这个数就是角的余弦。一般我们写成 `cos(x)` 来表示应用到一个值 `x` 上的余弦函数。
> 
> 
> 在代码中，函数可以有多个输入（也可以没有输入），然后函数给出多个输出（也可以没有输出），并可能导致副作用。例如一个函数可以在一个文件系统上创建一个文件，第一个输入是它的名字，第二个输入是文件的长度。
> 
> 
> ![Function as black boxes](/data/attachment/album/201902/08/225958r9kfp7hot9ofc08c.png)
> 
> 
> 函数可以认为是一个“黑匣子”。我们给它输入，然后它给我们输出，而我们不需要知道它是如何工作的。
> 
> 
> 


在像 C 或 C++ 这样的高级代码中，函数是语言的组成部分。在汇编代码中，函数只是我们的创意。


理想情况下，我们希望能够在我们的寄存器中设置一些输入值，然后分支切换到某个地址，然后预期在某个时刻分支返回到我们代码，并通过代码来设置输出值到寄存器。这就是我们所设想的汇编代码中的函数。困难之处在于我们用什么样的方式去设置寄存器。如果我们只是使用平时所接触到的某种方法去设置寄存器，每个程序员可能使用不同的方法，这样你将会发现你很难理解其他程序员所写的代码。另外，编译器也不能像使用汇编代码那样轻松地工作，因为它们压根不知道如何去使用函数。为避免这种困惑，为每个汇编语言设计了一个称为<ruby> 应用程序二进制接口 <rt>  Application Binary Interface </rt></ruby>（ABI）的标准，由它来规范函数如何去运行。如果每个人都使用相同的方法去写函数，这样每个人都可以去使用其他人写的函数。在这里，我将教你们这个标准，而从现在开始，我所写的函数将全部遵循这个标准。


该标准规定，寄存器 `r0`、`r1`、`r2` 和 `r3` 将被依次用于函数的输入。如果函数没有输入，那么它不会在意值是什么。如果只需要一个输入，那么它应该总是在寄存器 `r0` 中，如果它需要两个输入，那么第一个输入在寄存器 `r0` 中，而第二个输入在寄存器 `r1` 中，依此类推。输出值也总是在寄存器 `r0` 中。如果函数没有输出，那么 `r0` 中是什么值就不重要了。


另外，该标准要求当一个函数运行之后，寄存器 `r4` 到 `r12` 的值必须与函数启动时的值相同。这意味着当你调用一个函数时，你可以确保寄存器 `r4` 到 `r12` 中的值没有发生变化，但是不能确保寄存器 `r0` 到 `r3` 中的值也没有发生变化。


当一个函数运行完成后，它将返回到启动它的代码分支处。这意味着它必须知道启动它的代码的地址。为此，需要一个称为 `lr`（链接寄存器）的专用寄存器，它总是在保存调用这个函数的指令后面指令的地址。


表 1.1 ARM ABI 寄存器用法




| 寄存器 | 简介 | 保留 | 规则 |
| --- | --- | --- | --- |
| `r0` | 参数和结果 | 否 | `r0` 和 `r1` 用于给函数传递前两个参数，以及函数返回的结果。如果函数返回值不使用它，那么在函数运行之后，它们可以携带任何值。 |
| `r1` | 参数和结果 | 否 |  |
| `r2` | 参数 | 否 | `r2` 和 `r3` 用去给函数传递后两个参数。在函数运行之后，它们可以携带任何值。 |
| `r3` | 参数 | 否 |  |
| `r4` | 通用寄存器 | 是 | `r4` 到 `r12` 用于保存函数运行过程中的值，它们的值在函数调用之后必须与调用之前相同。 |
| `r5` | 通用寄存器 | 是 |  |
| `r6` | 通用寄存器 | 是 |  |
| `r7` | 通用寄存器 | 是 |  |
| `r8` | 通用寄存器 | 是 |  |
| `r9` | 通用寄存器 | 是 |  |
| `r10` | 通用寄存器 | 是 |  |
| `r11` | 通用寄存器 | 是 |  |
| `r12` | 通用寄存器 | 是 |  |
| `lr` | 返回地址 | 否 | 当函数运行完成后，`lr` 中保存了分支的返回地址，但在函数运行完成之后，它将保存相同的地址。 |
| `sp` | 栈指针 | 是 | `sp` 是栈指针，在下面有详细描述。它的值在函数运行完成后，必须是相同的。 |


通常，函数需要使用很多的寄存器，而不仅是 `r0` 到 `r3`。但是，由于 `r4` 到 `r12` 必须在函数完成之后值必须保持相同，因此它们需要被保存到某个地方。我们将它们保存到称为栈的地方。



> 
> ![Stack diagram](/data/attachment/album/201902/08/230000q7m5u356p66z0lkl.png)
> 
> 
> 一个<ruby> 栈 <rt>  stack </rt></ruby>就是我们在计算中用来保存值的一个很形象的方法。就像是摞起来的一堆盘子，你可以从上到下来移除它们，而添加它们时，你只能从下到上来添加。
> 
> 
> 在函数运行时，使用栈来保存寄存器值是个非常好的创意。例如，如果我有一个函数需要去使用寄存器 `r4` 和 `r5`，它将在一个栈上存放这些寄存器的值。最后用这种方式，它可以再次将它拿回来。更高明的是，如果为了运行完我的函数，需要去运行另一个函数，并且那个函数需要保存一些寄存器，在那个函数运行时，它将把寄存器保存在栈顶上，然后在结束后再将它们拿走。而这并不会影响我保存在寄存器 `r4` 和 `r5` 中的值，因为它们是在栈顶上添加的，拿走时也是从栈顶上取出的。
> 
> 
> 用来表示使用特定的方法将值放到栈上的专用术语，我们称之为那个方法的“<ruby> 栈帧 <rt>  stack frame </rt></ruby>”。不是每种方法都使用一个栈帧，有些是不需要存储值的。
> 
> 
> 


因为栈非常有用，它被直接实现在 ARMv6 的指令集中。一个名为 `sp`（栈指针）的专用寄存器用来保存栈的地址。当需要有值添加到栈上时，`sp` 寄存器被更新，这样就总是保证它保存的是栈上第一个值的地址。`push {r4,r5}` 将推送 `r4` 和 `r5` 中的值到栈顶上，而 `pop {r4,r5}` 将（以正确的次序）取回它们。


### 2、我们的第一个函数


现在，关于函数的原理我们已经有了一些概念，我们尝试来写一个函数。由于是我们的第一个很基础的例子，我们写一个没有输入的函数，它将输出 GPIO 的地址。在上一节课程中，我们就是写到这个值上，但将它写成函数更好，因为我们在真实的操作系统中经常需要用到它，而我们不可能总是能够记住这个地址。


复制下列代码到一个名为 `gpio.s` 的新文件中。就像在 `source` 目录中使用的 `main.s` 一样。我们将把与 GPIO 控制器相关的所有函数放到一个文件中，这样更好查找。



```
.globl GetGpioAddress
GetGpioAddress:
ldr r0,=0x20200000
mov pc,lr
```


> 
> `.globl lbl` 使标签 `lbl` 从其它文件中可访问。
> 
> 
> `mov reg1,reg2` 复制 `reg2` 中的值到 `reg1` 中。
> 
> 
> 


这就是一个很简单的完整的函数。`.globl GetGpioAddress` 命令是通知汇编器，让标签 `GetGpioAddress` 在所有文件中全局可访问。这意味着在我们的 `main.s` 文件中，我们可以使用分支指令到标签 `GetGpioAddress` 上，即便这个标签在那个文件中没有定义也没有问题。


你应该认得 `ldr r0,=0x20200000` 命令，它将 GPIO 控制器地址保存到 `r0` 中。由于这是一个函数，我们必须要让它输出到寄存器 `r0` 中，我们不能再像以前那样随意使用任意一个寄存器了。


`mov pc,lr` 将寄存器 `lr` 中的值复制到 `pc` 中。正如前面所提到的，寄存器 `lr` 总是保存着方法完成后我们要返回的代码的地址。`pc` 是一个专用寄存器，它总是包含下一个要运行的指令的地址。一个普通的分支命令只需要改变这个寄存器的值即可。通过将 `lr` 中的值复制到 `pc` 中，我们就可以将要运行的下一行命令改变成我们将要返回的那一行。


理所当然这里有一个问题，那就是我们如何去运行这个代码？我们将需要一个特殊的分支类型 `bl` 指令。它像一个普通的分支一样切换到一个标签，但它在切换之前先更新 `lr` 的值去包含一个在该分支之后的行的地址。这意味着当函数执行完成后，将返回到 `bl` 指令之后的那一行上。这就确保了函数能够像任何其它命令那样运行，它简单地运行，做任何需要做的事情，然后推进到下一行。这是理解函数最有用的方法。当我们使用它时，就将它们按“黑匣子”处理即可，不需要了解它是如何运行的，我们只了解它需要什么输入，以及它给我们什么输出即可。


到现在为止，我们已经明白了函数如何使用，下一节我们将使用它。


### 3、一个大的函数


现在，我们继续去实现一个更大的函数。我们的第一项任务是启用 GPIO 第 16 号针脚的输出。如果它是一个函数那就太好了。我们能够简单地指定一个针脚号和一个函数作为输入，然后函数将设置那个针脚的值。那样，我们就可以使用这个代码去控制任意的 GPIO 针脚，而不只是 LED 了。


将下列的命令复制到 `gpio.s` 文件中的 `GetGpioAddress` 函数中。



```
.globl SetGpioFunction
SetGpioFunction:
cmp r0,#53
cmpls r1,#7
movhi pc,lr
```


> 
> 带后缀 `ls` 的命令只有在上一个比较命令的结果是第一个数字小于或与第二个数字相同的情况下才会被运行。它是无符号的。
> 
> 
> 带后缀 `hi` 的命令只有上一个比较命令的结果是第一个数字大于第二个数字的情况下才会被运行。它是无符号的。
> 
> 
> 


在写一个函数时，我们首先要考虑的事情就是输入，如果输入错了我们怎么办？在这个函数中，我们有一个输入是 GPIO 针脚号，而它必须是介于 0 到 53 之间的数字，因为只有 54 个针脚。每个针脚有 8 个函数，被编号为 0 到 7，因此函数编号也必须是 0 到 7 之间的数字。我们可以假设输入应该是正确的，但是当在硬件上使用时，这种做法是非常危险的，因为不正确的值将导致非常糟糕的副作用。所以，在这个案例中，我们希望确保输入值在正确的范围。


为了确保输入值在正确的范围，我们需要做一个检查，即 `r0` <= 53 并且 `r1` <= 7。首先我们使用前面看到的比较命令去将 `r0` 的值与 53 做比较。下一个指令 `cmpls` 仅在前一个比较指令结果是小于或与 53 相同时才会去运行。如果是这种情况，它将寄存器 `r1` 的值与 7 进行比较，其它的部分都和前面的是一样的。如果最后的比较结果是寄存器值大于那个数字，最后我们将返回到运行函数的代码处。


这正是我们所希望的效果。如果 `r0` 中的值大于 53，那么 `cmpls` 命令将不会去运行，但是 `movhi` 会运行。如果 `r0` 中的值 <= 53，那么 `cmpls` 命令会运行，它会将 `r1` 中的值与 7 进行比较，如果 `r1` > 7，`movhi` 会运行，函数结束，否则 `movhi` 不会运行，这样我们就确定 `r0` <= 53 并且 `r1` <= 7。


`ls`（低于或相同）与 `le`（小于或等于）有一些细微的差别，以及后缀 `hi`（高于）和 `gt`（大于）也一样有一些细微差别，我们在后面将会讲到。


将这些命令复制到上面的代码的下面位置。



```
push {lr}
mov r2,r0
bl GetGpioAddress
```


> 
> `push {reg1,reg2,...}` 复制列出的寄存器 `reg1`、`reg2`、… 到栈顶。该命令仅能用于通用寄存器和 `lr` 寄存器。
> 
> 
> `bl lbl` 设置 `lr` 为下一个指令的地址并切换到标签 `lbl`。
> 
> 
> 


这三个命令用于调用我们第一个方法。`push {lr}` 命令复制 `lr` 中的值到栈顶，这样我们在后面可以获取到它。当我们调用 `GetGpioAddress` 时必须要这样做，我们将需要使用 `lr` 去保存我们函数要返回的地址。


如果我们对 `GetGpioAddress` 函数一无所知，我们必须假设它改变了 `r0`、`r1`、`r2` 和 `r3` 的值 ，并移动我们的值到 `r4` 和 `r5` 中，以在函数完成之后保持它们的值一样。幸运的是，我们知道 `GetGpioAddress` 做了什么，并且我们也知道它仅改变了 `r0` 为 GPIO 地址，它并没有影响 `r1`、`r2` 或 `r3` 的值。因此，我们仅去将 GPIO 针脚号从 `r0` 中移出，这样它就不会被覆盖掉，但我们知道，可以将它安全地移到 `r2` 中，因为 `GetGpioAddress` 并不去改变 `r2`。


最后我们使用 `bl` 指令去运行 `GetGpioAddress`。通常，运行一个函数，我们使用一个术语叫“调用”，从现在开始我们将一直使用这个术语。正如我们前面讨论过的，`bl` 调用一个函数是通过更新 `lr` 为下一个指令的地址并切换到该函数完成的。


当一个函数结束时，我们称为“返回”。当一个 `GetGpioAddress` 调用返回时，我们已经知道了 `r0` 中包含了 GPIO 的地址，`r1` 中包含了函数编号，而 `r2` 中包含了 GPIO 针脚号。


我前面说过，GPIO 函数每 10 个保存在一个块中，因此首先我们需要去判断我们的针脚在哪个块中。这似乎听起来像是要使用一个除法，但是除法做起来非常慢，因此对于这些比较小的数来说，不停地做减法要比除法更好。


将下面的代码复制到上面的代码中最下面的位置。



```
functionLoop$:

cmp r2,#9
subhi r2,#10
addhi r0,#4
bhi functionLoop$
```


> 
> `add reg,#val` 将数字 `val` 加到寄存器 `reg` 的内容上。
> 
> 
> 


这个简单的循环代码将针脚号（`r2`）与 9 进行比较。如果它大于 9，它将从针脚号上减去 10，并且将 GPIO 控制器地址加上 4，然后再次运行检查。


这样做的效果就是，现在，`r2` 中将包含一个 0 到 9 之间的数字，它是针脚号除以 10 的余数。`r0` 将包含这个针脚的函数所设置的 GPIO 控制器的地址。它就如同是 “GPIO 控制器地址 + 4 × (GPIO 针脚号 ÷ 10)”。


最后，将下面的代码复制到上面的代码中最下面的位置。



```
add r2, r2,lsl #1
lsl r1,r2
str r1,[r0]
pop {pc}
```


> 
> 移位参数 `reg,lsl #val` 表示将寄存器 `reg` 中二进制表示的数逻辑左移 `val` 位之后的结果作为与前面运算的操作数。
> 
> 
> `lsl reg,amt` 将寄存器 `reg` 中的二进制数逻辑左移 `amt` 中的位数。
> 
> 
> `str reg,[dst]` 与 `str reg,[dst,#0]` 相同。
> 
> 
> `pop {reg1,reg2,...}` 从栈顶复制值到寄存器列表 `reg1`、`reg2`、… 仅有通用寄存器与 `pc` 可以这样弹出值。
> 
> 
> 


这个代码完成了这个方法。第一行其实是乘以 3 的变体。乘法在汇编中是一个大而慢的指令，因为电路需要很长时间才能给出答案。有时使用一些能够很快给出答案的指令会让它变得更快。在本案例中，我们知道 `r2` × 3 与 `r2` × 2 + `r2` 是相同的。一个寄存器乘以 2 是非常容易的，因为它可以通过将二进制表示的数左移一位来很方便地实现。


ARMv6 汇编语言其中一个非常有用的特性就是，在使用它之前可以先移动参数所表示的位数。在本案例中，我将 `r2` 加上 `r2` 中二进制表示的数左移一位的结果。在汇编代码中，你可以经常使用这个技巧去更快更容易地计算出答案，但如果你觉得这个技巧使用起来不方便，你也可以写成类似 `mov r3,r2`； `add r2,r3`； `add r2,r3` 这样的代码。


现在，我们可以将一个函数的值左移 `r2` 中所表示的位数。大多数对数量的指令（比如 `add` 和 `sub`）都有一个可以使用寄存器而不是数字的变体。我们执行这个移位是因为我们想去设置表示针脚号的位，并且每个针脚有三个位。


然后，我们将函数计算后的值保存到 GPIO 控制器的地址上。我们在循环中已经算出了那个地址，因此我们不需要像 OK01 和 OK02 中那样在一个偏移量上保存它。


最后，我们从这个方法调用中返回。由于我们将 `lr` 推送到了栈上，因此我们 `pop pc`，它将复制 `lr` 中的值并将它推送到 `pc` 中。这个操作类似于 `mov pc,lr`，因此函数调用将返回到运行它的那一行上。


敏锐的人可能会注意到，这个函数其实并不能正确工作。虽然它将 GPIO 针脚函数设置为所要求的值，但它会导致在同一个块中的所有的 10 个针脚的函数都归 0！在一个大量使用 GPIO 针脚的系统中，这将是一个很恼人的问题。我将这个问题留给有兴趣去修复这个函数的人，以确保只设置相关的 3 个位而不去覆写其它位，其它的所有位都保持不变。关于这个问题的解决方案可以在本课程的下载页面上找到。你可能会发现非常有用的几个函数是 `and`，它是计算两个寄存器的布尔与函数，`mvns` 是计算布尔非函数，而 `orr` 是计算布尔或函数。


### 4、另一个函数


现在，我们已经有了能够管理 GPIO 针脚函数的函数。我们还需要写一个能够打开或关闭 GPIO 针脚的函数。我们不需要写一个打开的函数和一个关闭的函数，只需要一个函数就可以做这两件事情。


我们将写一个名为 `SetGpio` 的函数，它将 GPIO 针脚号作为第一个输入放入 `r0` 中，而将值作为第二个输入放入 `r1` 中。如果该值为 `0`，我们将关闭针脚，而如果为非零则打开针脚。


将下列的代码复制粘贴到 `gpio.s` 文件的结尾部分。



```
.globl SetGpio
SetGpio:
pinNum .req r0
pinVal .req r1
```


> 
> `alias .req reg` 设置寄存器 `reg` 的别名为 `alias`。
> 
> 
> 


我们再次需要 `.globl` 命令，标记它为其它文件可访问的全局函数。这次我们将使用寄存器别名。寄存器别名允许我们为寄存器使用名字而不仅是 `r0` 或 `r1`。到目前为止，寄存器别名还不是很重要，但随着我们后面写的方法越来越大，它将被证明非常有用，现在开始我们将尝试使用别名。当在指令中使用到 `pinNum .req r0` 时，它的意思是 `pinNum` 表示 `r0`。


将下面的代码复制粘贴到上述的代码下面位置。



```
cmp pinNum,#53
movhi pc,lr
push {lr}
mov r2,pinNum
.unreq pinNum
pinNum .req r2
bl GetGpioAddress
gpioAddr .req r0
```


> 
> `.unreq alias` 删除别名 `alias`。
> 
> 
> 


就像在函数 `SetGpio` 中所做的第一件事情是检查给定的针脚号是否有效一样。我们需要同样的方式去将 `pinNum`（`r0`）与 53 进行比较，如果它大于 53 将立即返回。一旦我们想要再次调用 `GetGpioAddress`，我们就需要将 `lr` 推送到栈上来保护它，将 `pinNum` 移动到 `r2` 中。然后我们使用 `.unreq` 语句来删除我们给 `r0` 定义的别名。因为针脚号现在保存在寄存器 `r2` 中，我们希望别名能够反映这个变化，因此我们从 `r0` 移走别名，重新定义到 `r2`。你应该每次在别名使用结束后，立即删除它，这样当它不再存在时，你就不会在后面的代码中因它而产生错误。


然后，我们调用了 `GetGpioAddress`，并且我们创建了一个指向 `r0`的别名以反映此变化。


将下面的代码复制粘贴到上述代码的后面位置。



```
pinBank .req r3
lsr pinBank,pinNum,#5a
lsl pinBank,#2
add gpioAddr,pinBank
.unreq pinBank
```


> 
> `lsr dst,src,#val` 将 `src` 中二进制表示的数右移 `val` 位，并将结果保存到 `dst`。
> 
> 
> 


对于打开和关闭 GPIO 针脚，每个针脚在 GPIO 控制器上有两个 4 字节组。第一个 4 字节组每个位控制前 32 个针脚，而第二个 4 字节组控制剩下的 22 个针脚。为了判断我们要设置的针脚在哪个 4 字节组中，我们需要将针脚号除以 32。幸运的是，这很容易，因为它等价于将二进制表示的针脚号右移 5 位。因此，在本案例中，我们将 `r3` 命名为 `pinBank`，然后计算 `pinNum` ÷ 32。因为它是一个 4 字节组，我们需要将它与 4 相乘的结果。它与二进制表示的数左移 2 位相同，这就是下一行的命令。你可能想知道我们能否只将它右移 3 位呢，这样我们就不用先右移再左移。但是这样做是不行的，因为当我们做 ÷ 32 时答案有些位可能被舍弃，而如果我们做 ÷ 8 时却不会这样。


现在，`gpioAddr` 的结果有可能是 20200000<sub> 16</sub>（如果针脚号介于 0 到 31 之间），也有可能是 20200004<sub> 16</sub>（如果针脚号介于 32 到 53 之间）。这意味着如果加上 28<sub> 10</sub>，我们将得到打开针脚的地址，而如果加上 40<sub> 10</sub> ，我们将得到关闭针脚的地址。由于我们用完了 `pinBank` ，所以在它之后立即使用 `.unreq` 去删除它。


将下面的代码复制粘贴到上述代码的下面位置。



```
and pinNum,#31
setBit .req r3
mov setBit,#1
lsl setBit,pinNum
.unreq pinNum
```


> 
> `and reg,#val` 计算寄存器 `reg` 中的数与 `val` 的布尔与。
> 
> 
> 


该函数的下一个部分是产生一个正确的位集合的数。至于 GPIO 控制器去打开或关闭针脚，我们在针脚号除以 32 的余数里设置了位的数。例如，设置 16 号针脚，我们需要第 16 位设置数字为 1 。设置 45 号针脚，我们需要设置第 13 位数字为 1，因为 45 ÷ 32 = 1 余数 13。


这个 `and` 命令计算我们需要的余数。它是这样计算的，在两个输入中所有的二进制位都是 1 时，这个 `and` 运算的结果就是 1，否则就是 0。这是一个很基础的二进制操作，`and` 操作非常快。我们给定的输入是 “pinNum and 31<sub> 10</sub> = 11111<sub> 2</sub>”。这意味着答案的后 5 位中只有 1，因此它肯定是在 0 到 31 之间。尤其是在 `pinNum` 的后 5 位的位置是 1 的地方它只有 1。这就如同被 32 整除的余数部分。就像 31 = 32 - 1 并不是巧合。


![binary division example](/data/attachment/album/201902/08/230000ps9989cmnb8lrs0y.png)


代码的其余部分使用这个值去左移 1 位。这就有了创建我们所需要的二进制数的效果。


将下面的代码复制粘贴到上述代码的下面位置。



```
teq pinVal,#0
.unreq pinVal
streq setBit,[gpioAddr,#40]
strne setBit,[gpioAddr,#28]
.unreq setBit
.unreq gpioAddr
pop {pc}
```


> 
> `teq reg,#val` 检查寄存器 `reg` 中的数字与 `val` 是否相等。
> 
> 
> 


这个代码结束了该方法。如前面所说，当 `pinVal` 为 0 时，我们关闭它，否则就打开它。`teq`（等于测试）是另一个比较操作，它仅能够测试是否相等。它类似于 `cmp` ，但它并不能算出哪个数大。如果你只是希望测试数字是否相同，你可以使用 `teq`。


如果 `pinVal` 是 0，我们将 `setBit` 保存在 GPIO 地址偏移 40 的位置，我们已经知道，这样会关闭那个针脚。否则将它保存在 GPIO 地址偏移 28 的位置，它将打开那个针脚。最后，我们通过弹出 `pc` 返回，这将设置它为我们推送链接寄存器时保存的值。


### 5、一个新的开始


在完成上述工作后，我们终于有了我们的 GPIO 函数。现在，我们需要去修改 `main.s` 去使用它们。因为 `main.s` 现在已经有点大了，也更复杂了。将它分成两节将是一个很好的设计。到目前为止，我们一直使用的 `.init` 应该尽可能的让它保持小。我们可以更改代码来很容易地反映出这一点。


将下列的代码插入到 `main.s` 文件中 `_start:` 的后面：



```
b main

.section .text
main:
mov sp,#0x8000
```

在这里重要的改变是引入了 `.text` 节。我设计了 `makefile` 和链接器脚本，它将 `.text` 节（它是默认节）中的代码放在地址为 8000<sub> 16</sub> 的 `.init` 节之后。这是默认加载地址，并且它给我们提供了一些空间去保存栈。由于栈存在于内存中，它也有一个地址。栈向下增长内存，因此每个新值都低于前一个地址，所以，这使得栈顶是最低的一个地址。


![Layout diagram of operating system](/data/attachment/album/201902/08/230000r540p85w8jz5hmmj.png)



> 
> 图中的 “ATAGs” 节的位置保存了有关树莓派的信息，比如它有多少内存，默认屏幕分辨率是多少。
> 
> 
> 


用下面的代码替换掉所有设置 GPIO 函数针脚的代码：



```
pinNum .req r0
pinFunc .req r1
mov pinNum,#16
mov pinFunc,#1
bl SetGpioFunction
.unreq pinNum
.unreq pinFunc
```

这个代码将使用针脚号 16 和函数编号 1 去调用 `SetGpioFunction`。它的效果就是启用了 OK LED 灯的输出。


用下面的代码去替换打开 OK LED 灯的代码：



```
pinNum .req r0
pinVal .req r1
mov pinNum,#16
mov pinVal,#0
bl SetGpio
.unreq pinNum
.unreq pinVal
```

这个代码使用 `SetGpio` 去关闭 GPIO 第 16 号针脚，因此将打开 OK LED。如果我们（将第 4 行）替换成 `mov pinVal,#1` 它将关闭 LED 灯。用以上的代码去替换掉你关闭 LED 灯的旧代码。


### 6、继续向目标前进


但愿你能够顺利地在你的树莓派上测试我们所做的这一切。到目前为止，我们已经写了一大段代码，因此不可避免会出现错误。如果有错误，可以去查看我们的排错页面。


如果你的代码已经正常工作，恭喜你。虽然我们的操作系统除了做 [课程 2：OK02](/article-10478-1.html) 中的事情，还做不了别的任何事情，但我们已经学会了函数和格式有关的知识，并且我们现在可以更好更快地编写新特性了。现在，我们在操作系统上修改 GPIO 寄存器将变得非常简单，而它就是用于控制硬件的！


在 [课程 4：OK04](https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/ok04.html) 中，我们将处理我们的 `wait` 函数，目前，它的时间控制还不精确，这样我们就可以更好地控制我们的 LED 灯了，进而最终控制所有的 GPIO 针脚。




---


via: <https://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/os/ok03.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 3 OK03

The OK03 lesson builds on OK02 by teaching how to use functions in assembly to make
more reusable and rereadable code. It is assumed you have the code for the [
Lesson 2: OK02](ok02.html) operating system as a basis.

Contents |


## 1 Reusable Code

So far we've made code for our operating system by typing the things we want to happen in order. This is fine for such tiny programs, but if we wrote the whole system like this, the code would be completely unreadable. Instead we use functions.

![Function as black boxes](images/functions.png)

Functions are said to be 'black boxes'. We put inputs in, and outputs come out, but we don't need to know how they work.

In higher level code such as C or C++, functions are part of the language itself. In assembly code, functions are just ideas we have.

Ideally we want to be able to set our registers to some input values, branch to an address, and expect that at some point the code will branch back to our code having set the registers to output values. This is what a function is in assembly code. The difficulty comes in what system we use for setting the registers. If we just used any system we felt like, each programmer may use a different system, and would find other programmers' work hard to understand. Further, compilers would not be able to work with assembly code as easily, as they would not know how to use the functions. To prevent confusion, a standard called the Application Binary Interface (ABI) was devised for each assembly language which is an agreement on how functions should be run. If everyone makes functions in the same way, then everyone will be able to use each others' functions. I will teach that standard here, and from now on I will code all of my functions to meet the standard.

The standard says that r0,r1,r2 and r3 will be used as inputs to a function in order. If a function needs no inputs, then it doesn't matter what value it takes. If it needs only one it always goes in r0, if it needs two, the first goes in r0, and the second goes on r1, and so on. The output will always be in r0. If a function has no output, it doesn't matter what value r0 takes.

Further, it also requires that after a function is run, r4 to r12 must have the same values as they had when the function started. This means that when you call a function, you can be sure the r4 to r12 will not change value, but you cannot be so sure about r0 to r3.

When a function completes it has to branch back to the code that started it. This means it must know the address of the code that started it. To facilitate this, there is a special register called lr (link register) which always holds the address of the instruction after the one that called this function.

Register | Brief | Preserved | Rules |
---|---|---|---|

r0 | Argument and result | No | r0 and r1 are used for passing the first two arguments to functions, and returning the results of functions. If a function does not use them for a return value, they can take any value after a function. |

r1 | Argument and result | No | |

r2 | Argument | No | r2 and r3 are used for passing the second two arguments to functions. There values after a function is called can be anything. |

r3 | Argument | No | |

r4 | General purpose | Yes | r4 to r12 are used for working values, and their value after a function is called must be the same as before. |

r5 | General purpose | Yes | |

r6 | General purpose | Yes | |

r7 | General purpose | Yes | |

r8 | General purpose | Yes | |

r9 | General purpose | Yes | |

r10 | General purpose | Yes | |

r11 | General purpose | Yes | |

r12 | General purpose | Yes | |

lr | Return address | No | lr is the address to branch back to when a function is finished, but this does have to contain the same address after the function has finished. |

sp | Stack pointer | Yes | sp is the stack pointer, described below. Its value must be the same after the function has finished. |


Often functions need to use more registers than just r0 to r3. But, since r4 to r12 must stay the same after the method has run, they must be saved somewhere. We save them on something called the stack.

Because the stack is so useful, it has been implemented in the ARMv6 instruction set directly. A special register called sp (stack pointer) holds the address of the stack. When items are added to the stack, the sp register updates so that it always holds the address of the first item on the stack. push {r4,r5} would put the values in r4 and r5 onto the top of the stack and pop {r4,r5} would take them back off again (in the correct order).

## 2 Our First Function

Now that we have some idea about how functions work, let's try to make one. For a basic first example, we are going to make a function that takes no input, and gives an output of the GPIO address. In the last lesson, we just wrote in this value, but it would be better as a function, since it is something we might need to do often in a real operating system, and we might not always remember the address.

Copy the following code into a new file called 'gpio.s'. Just make the new file in the 'source' directory with 'main.s'. We're going to put all functions related to the GPIO controller in one file to make them easier to find.

.globl GetGpioAddress

GetGpioAddress:

ldr r0,=0x20200000

mov pc,lr


.globl lbl makes the label lbl accessible from other files.

mov reg1,reg2 copies the value in reg2 into reg1.

This is a very simple complete function. The .globl GetGpioAddress command is a message to the assembler to make the label GetGpioAddress accessible to all files. This means that in our main.s file we can branch to the label GetGpioAddress even though it is not defined in that file.

You should recognise the ldr r0,=0x20200000 command, which stores the GPIO controller address in r0. Since this is a function, we have to give the output in r0, so we are not as free to use any register as we once were.

mov pc,lr copies the value in lr to pc. As mentioned earlier lr always contains the address of the code that we have to go back to when a method finishes. pc is a special register which always contains the address of the next instruction to be run. A normal branch command just changes the value of this register. By copying the value in lr to pc we just change the next line to be run to be the one we were told to go back to.

A reasonable question would now be, how would we actually run this code? A special type of branch bl does what we need. It branches to a label like a normal branch, but before it does it updates lr to contain the address of the line after the branch. That means that when the function finishes, the line it will go back to will be the one after the bl command. This makes a function running just look like any other command, it simply runs, does whatever it needs to do, and then carries on to the next line. This is a really useful way of thinking about functions. We treat them as 'black boxes' in that when we use them, we don't need to think about how they work, we just need to know what inputs they need, and what outputs they give.

For now, don't worry about using the function, we will use it in the next section.

## 3 A Big Function

Now we're going to implement a bigger function. Our first job was to enable output on GPIO pin 16. It would be nice if this was a function. We could simply specify a pin and a function as the input, and the function would set the function of that pin to that value. That way, we could use the code to control any GPIO pin, not just the LED.

Copy the following commands below the GetGpioAddress function in gpio.s.

.globl SetGpioFunction

SetGpioFunction:

cmp r0,#53

cmpls r1,#7

movhi pc,lr


Suffix ls causes the command to be executed only if the last comparison determined that the first number was less than or the same as the second. Unsigned.

Suffix hi causes the command to be executed only if the last comparison determined that the first number was higher than the second. Unsigned.

One of the first things we should always think about when writing functions is our inputs. What do we do if they are wrong? In this function, we have one input which is a GPIO pin number, and so must be a number between 0 and 53, since there are 54 pins. Each pin has 8 functions, numbered 0 to 7 and so the function code must be too. We could just assume that the inputs will be correct, but this is very dangerous when working with hardware, as incorrect values could cause very bad side effects. Therefore, in this case, we wish to make sure the inputs are in the right ranges.

To do this we need to check that r0 <= 53 and r1 <= 7. First of all, we can use the comparison we've seen before to compare the value of r0 with 53. The next instruction, cmpls is a normal comparison instruction that will only be run if r0 was lower than or the same as 53. If that was the case, it compares r1 with 7, otherwise the result of the comparison is the same as before. Finally we go back to the code that ran the function if the result of the last comparison was that the register was higher than the number.

The effect of this is exactly what we want. If r0 was bigger than 53, then the cmpls command doesn't run, but the movhi does. If r0 is <= 53, then the cmpls command does run, and so r1 is compared with 7, and then if it is higher than 7, movhi is run, and the function ends, otherwise movhi does not run, and we know for sure that r0 <= 53 and r1 <= 7.

There is a subtle difference between the ls (lower or same) and le (less or equal) as well as between hi (higher) and gt (greater) suffixes, but I will cover this later.

Copy these commands below the above.

push {lr}

mov r2,r0

bl GetGpioAddress


push {reg1,reg2,...} copies the registers in the list reg1,reg2,... onto the top of the stack. Only general purpose registers and lr can be pushed.

bl lbl sets lr to the address of the next instruction and then branches to the label lbl.

These next three commands are focused on calling our first method. The push {lr} command copies the value in lr onto the top of the stack, so that we can retrieve it later. We must do this because when we call GetGpioAddress, we will need to use lr to store the address to come back to in our function.

If we did not know anything about the GetGpioAddress function, we would have to assume it changes r0,r1,r2 and r3, and would have to move our values to r4 and r5 to keep them the same after it finishes. Fortunately, we do know about GetGpioAddress, and we know it only changes r0 to the address, it doesn't affect r1,r2 or r3. Thus, we only have to move the GPIO pin number out of r0 so it doesn't get overwritten, but we know we can safely move it to r2, as GetGpioAddress doesn't change r2.

Finally we use the bl instruction to run GetGpioAddress. Normally we use the term 'call' for running a function, and I will from now. As discussed earlier bl calls a function by updating the lr to the next instruction's address, and then branching to the function.

When a function ends we say it has 'returned'. When the call to GetGpioAddress returns, we now know that r0 contains the GPIO address, r1 contains the function code and r2 contains the GPIO pin number. I mentioned earlier that the GPIO functions are stored in blocks of 10, so first we need to determine which block of ten our pin number is in. This sounds like a job we would use a division for, but divisions are very slow indeed, so it is better for such small numbers to do repeated subtraction.

Copy the following code below the above.

functionLoop$:


cmp r2,#9

subhi r2,#10

addhi r0,#4

bhi functionLoop$


add reg,#val adds the number val to the contents of the register reg.

This simple loop code compares the pin number to 9. If it is higher than 9, it subtracts 10 from the pin number, and adds 4 to the GPIO Controller address then runs the check again.

The effect of this is that r2 will now contain a number from 0 to 9 which represents the remainder of dividing the pin number by 10. r0 will now contain the address in the GPIO controller of this pin's function settings. This would be the same as GPIO Controller Address + 4 × (GPIO Pin Number ÷ 10).

Finally, copy the following code below the above.

add r2, r2,lsl #1

lsl r1,r2

str r1,[r0]

pop {pc}


Argument shift reg,lsl #val shifts the binary representation of the number in reg left by val before using it in the operation before.

lsl reg,amt shifts the binary representation of the number in reg left by the number in amt.

str reg,[dst] is the same as str reg,[dst,#0].

pop {reg1,reg2,...} copies the values from the top of the stack into the register list reg1,reg2,.... Only general purpose registers and pc can be popped.

This code finishes off the method. The first line is actually a multiplication by 3 in disguise. Multiplication is a big and slow instruction in assembly code, as the circuit can take a long time to come up with the answer. It is much faster sometimes to use some instructions which can get the answer quicker. In this case, I know that r2 × 3 is the same as r2 × 2 + r2. It is very easy to multiply a register by 2 as this is conveniently the same as shifting the binary representation of the number left by one place.

One of the very useful features of the ARMv6 assembly code language is the ability to shift an argument before using it. In this case, I add r2 to the result of shifting the binary representation of r2 to the left by one place. In assembly code, you often use tricks such as this to compute answers more easily, but if you're uncomfortable with this, you could also write something like mov r3,r2; add r2,r3; add r2,r3.

Now we shift the function value left by a number of places equal to r2. Most instructions such as add and sub have a variant which uses a register rather than a number for the amount. We perform this shift because we want to set the bits that correspond to our pin number, and there are three bits per pin.

We then store the the computed function value at the address in the GPIO controller. We already worked out the address in the loop, so we don't need to store it at an offset like we did in OK01 and OK02.

Finally, we can return from this method call. Since we pushed lr onto the stack, if we pop pc, it will copy the value that was in lr at the time we pushed it into pc. This would be the same as having used mov pc,lr and so the function call will return when this line is run.

The very keen may notice that this function doesn't actually work correctly. Although it sets the function of the GPIO pin to the requested value, it causes all the pins in the same block of 10's functions to go back to 0! This would likely be quite annoying in a system which made heavy use of the GPIO pins. I leave it as a challenge to the interested to fix this function so that it does not overwrite other pins values by ensuring that all bits other than the 3 that must be set remain the same. A solution to this can be found on the downloads page for this lesson. Functions that you may find useful are and which computes the Boolean and function of two registers, mvns which computes the Boolean not and orr which computes the Boolean or.

## 4 Another Function

So, we now have a function which takes care of the GPIO pin function setting. We now need to make a function to turn a GPIO pin on or off. Rather than having one function for off and one function for on, it would be handy to have a single function which does either.

We will make a function called SetGpio which takes a GPIO pin number as its first input in r0, and a value as its second in r1. If the value is 0 we will turn the pin off, and if it is not zero we will turn it on.

Copy and paste the following code at the end of 'gpio.s'.

.globl SetGpio

SetGpio:

pinNum .req r0

pinVal .req r1


alias .req reg sets alias to mean the register reg.

Once again we need the .globl command and the label to make the function accessible from other files. This time we're going to use register aliases. Register aliases allow us to use a name other than just r0 or r1 for registers. This may not be so important now, but it will prove invaluable when writing big methods later, and you should try to use aliases from now on. pinNum .req r0 means that pinNum now means r0 when used in instructions.

Copy and paste the following code after the above.

cmp pinNum,#53

movhi pc,lr

push {lr}

mov r2,pinNum

.unreq pinNum

pinNum .req r2

bl GetGpioAddress

gpioAddr .req r0


.unreq alias removes the alias alias.

Like in SetGpioFunction the first thing we must do is check that we were actually given a valid pin number. We do this in exactly the same way by comparing pinNum (r0) with 53, and returning immediately if it is higher. Once again we wish to call GetGpioAddress, so we have to preserve lr by pushing it onto the stack, and to move pinNum to r2. We then use the .unreq statement to remove our alias from r0. Since the pin number is now stored in r2 we want our alias to reflect this, so we remove the alias from r0 and remake it on r2. You should always .unreq every alias as soon as it is done with, so that you cannot make the mistake of using it further down the code when it no longer exists.

We then call GetGpioAddress, and we create an alias for r0 to reflect this.

Copy and paste the following code after the above.

pinBank .req r3

lsr pinBank,pinNum,#5

lsl pinBank,#2

add gpioAddr,pinBank

.unreq pinBank


lsr dst,src,#val shifts the binary representation of the number in src right by val, but stores the result in dst.

The GPIO controller has two sets of 4 bytes each for turning pins on and off. The first set in each case controls the first 32 pins, and the second set controls the remaining 22. In order to determine which set it is in, we need to divide the pin number by 32. Fortunately this is very easy, at is the same as shifting the binary representation of the pin number right by 5 places. Hence, in this case I've named r3 as pinBank and then computed pinNum ÷ 32. Since it is a set of 4 bytes, we then need to multiply the result of this by 4. This is the same as shifting the binary representation left by 2 places, which is the command that follows. You may wonder if we could just shift it right by 3 places, as we went right then left. This won't work however, as some of the answer may have been rounded away when we did ÷ 32 which may not be if we just ÷ 8.

The result of this is that gpioAddr now contains
either 2020000016 if the pin number is 0-31, and 2020000416
if the pin number is 32-53. This means if we add 2810 we get the address
for turning the pin on, and if we add 4010 we get the address for turning
the pin off. Since we are done with pinBank,
I use .unreq immediately afterwards.

Copy and paste the following code after the above.

and pinNum,#31

setBit .req r3

mov setBit,#1

lsl setBit,pinNum

.unreq pinNum


and reg,#val computes the Boolean and function of the number in reg with val.

This next part of the function is for generating a number with the correct bit set. For the GPIO controller to turn a pin on or off, we give it a number with a bit set in the place of the remainder of that pin's number divided by 32. For example, to set pin 16, we need a number with the 16th bit a 1. To set pin 45 we would need a number with the 13th bit 1 as 45 ÷ 32 = 1 remainder 13.

The and command computes the remainder we need.
How it does this is that the result of an and operation is a number with 1s in all
binary digits which had 1s in both of the inputs, and 0s elsewhere. This is a fundamental
binary operation, and is very quick. We have given it inputs of
pinNum and 3110 = 111112. This means that the answer
can only have 1 bits in the last 5 places, and so is definitely between 0 and 31.
Specifically it only has 1s where there were 1s in pinNum's
last 5 places. This is the same as the remainder of a division by 32. It is no coincidence
that 31 = 32 - 1.

![binary division example](images/binary3.png)

The rest of this code simply uses this value to shift the number 1 left. This has the effect of creating the binary number we need.

Copy and paste the following code after the above.

teq pinVal,#0

.unreq pinVal

streq setBit,[gpioAddr,#40]

strne setBit,[gpioAddr,#28]

.unreq setBit

.unreq gpioAddr

pop {pc}


teq reg,#val checks if the number in reg is equal to val.

This code ends the method. As stated before, we turn the pin off if pinVal is zero, and on otherwise. teq (test equal) is another comparison operation that can only be used to test for equality. It is similar to cmp but it does not work out which number is bigger. If all you wish to do is test if to numbers are the same, you can use teq.

If pinVal is zero, we store the setBit at 40 away from the GPIO address, which we already know turns the pin off. Otherwise we store it at 28, which turns the pin on. Finally, we return by popping the pc, which sets it to the value that we stored when we pushed the link register.

## 5 A New Beginning

Finally, after all that work we have our GPIO functions. We now need to alter 'main.s' to use them. Since 'main.s' is now getting a lot bigger and more complicated, it is better design to split it into two sections. The '.init' we've been using so far is best kept as small as possible. We can change the code to reflect this easily.

Insert the following just after _start: in main.s:

b main


.section .text

main:

mov sp,#0x8000


The key change we have made here is to introduce the .text
section. I have designed the makefile and linker scripts such that code in the .text
section (which is the default section) is placed after the .init section which is placed at address 800016.
This is the default load address and gives us some
space to store the stack. As the stack exists in memory, it has to have an address.
The stack grows down memory, so that each new value is at a lower address, thus
making the 'top' of the stack, the lowest address.

The 'ATAGs' section in the diagram is a place where information about the Raspberry Pi is stored such as how much memory it has, and what its default screen resolution is.

![Layout diagram of operating system](images/osLayout.png)

Replace all the code that set the function of the GPIO pin with the following:

pinNum .req r0

pinFunc .req r1

mov pinNum,#16

mov pinFunc,#1

bl SetGpioFunction

.unreq pinNum

.unreq pinFunc


This code calls SetGpioFunction with the pin number 16 and the pin function code 1. This has the effect of enabling output to the OK LED.

Replace any code which turns the OK LED on with the following:

pinNum .req r0

pinVal .req r1

mov pinNum,#16

mov pinVal,#0

bl SetGpio

.unreq pinNum

.unreq pinVal


This code uses SetGpio to turn off GPIO pin 16, thus turning on the OK LED. If we instead used mov pinVal,#1, it would turn the LED off. Replace your old code to turn the LED off with that.

## 6 Onwards

Hopefully now, you should be able to test what you have made on the Raspberry Pi. We've done a large amount of code this time, so there is a lot that can go wrong. If it does, head to the troubleshooting page.

When you get it working, congratulations. Although our operating system does nothing
more than it did in [Lesson 2: OK02](ok02.html), we've learned a lot about
functions and formatting, and we can now code new features much more quickly. It
would be very simple now to make an Operating System that alters any GPIO register,
which could be used to control hardware!

In [Lesson 4: OK04](ok04.html), we will address our wait function, which
is currently imprecise, so that we can gain better control over our LED, and ultimately
over all of the GPIO pins.