第5章アルゴリズムのハードウェアによる実現:コンピューターシステム設計
Registersレジスタ
A register is a memory unit that functions at very high speed. Figure 5.17 shows registers as one black box. If we open this box and see what is inside, we see 16 registers. Any instruction in x86 assembly uses only those 16 registers. The name of each register is shown in Figure 5.18 and starts with an “r.” The naming convention of registers is a bit odd, but it is due to some historical naming (for the registers on the left). To keep backward compatibility, old register names cannot be changed.
英語のヒント

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 5.18 Registers in x86 are a bit complicated due to the need to keep backward compatibility and the fact that x86 is CISC. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図5.18 x86のレジスタがやや複雑なのは、後方互換性を保つ必要があることと、x86がCISCであることによる。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
Each one of these registers can hold 64 bits. When we had 32-bit machines, only eight registers existed, as shown on the left of Figure 5.18. In the 32-bit era, each register could hold 32-bits only and its name started with “e” (for extended). This is why we see that the lower 32 bits of the current registers hold the old names to keep backward compatibility. Not only that, in the 16-bit era, each register held 16 bits only. Those on the left had names: ax, bx, cx, and so on, which are the names of the lowest 16 bits of the current registers. Registers on the right side of the figure did not exist before the 64-bit era. If we go to the 8-bit era, we can even access the lowest 8-bits of the register. The register rax is shown as an example where parts of the register can be accessed using the naming convention: rax (16 bits), eax (32 bits), ax (16 bits), and ah and al (8 bits each). In the 8-bit era there were only four registers, which are the top four in the left column of the figure.
英語のヒント
これらのレジスタは、それぞれ64ビットを保持できる。32ビットの機械の時代には、図5.18の左側に示す8個のレジスタしかなかった。当時は各レジスタが32ビットだけを保持し、名前はextended(拡張)を意味する「e」で始まった。そのため、現在のレジスタの下位32ビットには、後方互換性を保つための古い名前が残っている。それだけでなく、16ビットの時代には、各レジスタが保持するのは16ビットだけだった。左側のレジスタにはax、bx、cxなどの名前があり、現在のレジスタの下位16ビットの名前になっている。図の右側のレジスタは、64ビットの時代より前には存在しなかった。8ビットの時代までさかのぼれば、レジスタの下位8ビットにもアクセスできる。例として示すraxレジスタでは、rax(16ビット)、eax(32ビット)、ax(16ビット)、ahとal(各8ビット)という命名規則を使い、それぞれの部分にアクセスできる。8ビットの時代には、図の左列の上側4個にあたる、4個のレジスタしかなかった。
CONCEPTS IN PRACTICE概念を実践する
HLLs and AssemblyHLLとアセンブリ
Most programmers use HLLs to write their programs. Why do you think world class programmers are very well versed in assembly? Professional programmers write in HLL but they often like to look at the assembly version of their code as well. This allows them to discover mistakes in their HLL code and also to find out ways to enhance their HLL code.
英語のヒント
ほとんどのプログラマーは、HLLでプログラムを書く。では、世界一流のプログラマーがアセンブリにも非常に詳しいのは、なぜだと思うか。プロのプログラマーはHLLで書くが、自分のコードのアセンブリ版もよく確認する。そうすることで、HLLのコードの誤りを見つけたり、改善する方法を見つけたりできるからである。
However, there are some programs, or parts of programs, that need to be written in assembly and not in HLL for the sake of performance (assembly code written by a human in these cases is much faster than HLL translated to assembly by a compiler) and more control over the hardware. Assembly language common uses today include device drivers, low-level embedded systems, and real-time systems.
英語のヒント
しかし、性能とハードウェアに対する制御の自由度を確保するために、HLLではなくアセンブリで書く必要のあるプログラムや、その一部分もある。こうした場合、人間が書いたアセンブリコードは、コンパイラがHLLから翻訳したものよりはるかに高速である。現在、アセンブリ言語は、デバイスドライバー、低レベルの組み込みシステム、リアルタイムシステムなどによく使われている。
Operandsオペランド
An assembly language program consists of a group of instructions. Each instruction does an operation and for this operation to be executed, it needs operands. An operand is a value used as input for an operator. Perhaps we want to add two numbers in an operation. To be executed it needs two numbers, which are the operands. For example: add %RAX, %RBX means add the content of the register RAX to the content of the register RBX and put the result in register RAX. Operands in assembly can be one of three things:
英語のヒント
アセンブリ言語のプログラムは、命令の集まりである。各命令は一つの操作を行い、それを実行するためにオペランドを必要とする。オペランドとは、演算子への入力として使う値である。たとえば二つの数を足す操作なら、実行に必要な二つの数がオペランドである。例として、add %RAX, %RBXは、RAXレジスタの内容をRBXレジスタの内容に足し、結果をRAXレジスタに入れることを意味する。アセンブリのオペランドには、次の三つの種類がある。
• A register
• レジスタ。
• An immediate operand (e.g., add 7 to rax; the operand here is mentioned explicitly in the instruction)
英語のヒント
• 即値オペランド(たとえば、raxに7を足す場合。ここでは、オペランドが命令中に明示されている)。
• Data from memory
• メモリ内のデータ。
Memory Addressing Modes
メモリアドレッシングモード
To get data from the memory, we need to specify the location, called the addressing mode, in the memory that contains the required data. Why are there several “modes” instead of just specifying an address directly? Well, the answer is related to the HLL.
英語のヒント
メモリからデータを取得するには、必要なデータを含むメモリ内の場所を、アドレッシングモードと呼ばれる方法で指定する必要がある。アドレスを直接指定するだけでなく、なぜ複数の「モード」があるのだろうか。答えはHLLに関係している。
In HLL programs, we use complicated data structures, such as arrays with one or more dimensions, structures, or linked lists. The compiler needs to translate this data structure to a much simpler assembly language. In assembly there are no complex data structures; there are only data items of 1, 2, 4, and 8 bytes. Then how can we map these complex data structures to the simple single dimension data items? One crucial way is to have rich addressing modes. In its most general form, an address in x86 is specified as D (Rb, Ri, S) where:
英語のヒント
HLLのプログラムでは、一次元以上の配列、構造体、連結リストなどの複雑なデータ構造を使う。コンパイラは、これらのデータ構造を、はるかに単純なアセンブリ言語へ翻訳する必要がある。アセンブリには複雑なデータ構造はなく、1、2、4、8バイトのデータ項目しかない。では、複雑なデータ構造を、単純な一次元のデータ項目にどう対応づければよいのだろうか。重要な方法の一つは、豊富なアドレッシングモードを用意することである。最も一般的な形では、x86のアドレスをD (Rb, Ri, S)と指定する。各要素の意味は次のとおりである。
• D is a non-negative (but can be 0) integer whose range is from 0 to 232 – 1.
英語のヒント
• Dは、0から232 − 1までの範囲の、非負の整数である(0も取れる)。
• Rb and Ri are registers, and they can be any one of the 16 registers.
• RbとRiはレジスタで、16個のどのレジスタでもかまわない。
• S is a scale that takes one of the following values: 1, 2, 4, or 8.
英語のヒント
• Sは倍率で、1、2、4、8のいずれかの値を取る。
The address is calculated as: D + Rb + (S × Ri). While this is a general form, it can have more reduced forms such as (Rb, Ri). Here D’s default is 0 and S’s default is 1; D (Rb, Ri); or (Rb, Ri, S).
英語のヒント
アドレスは、D + Rb + (S × Ri)として計算される。これは一般形であるが、(Rb, Ri)のように省略した形にもできる。この場合、Dの既定値は0、Sの既定値は1である。D (Rb, Ri)や(Rb, Ri, S)という形もある。
Now that we know about operands, let’s look at the operations themselves that are implemented by the different assembly instructions.
英語のヒント
オペランドについてわかったところで、さまざまなアセンブリ命令が実装する、操作そのものを見てみよう。