プリンキピア

第5章アルゴリズムのハードウェアによる実現:コンピューターシステム設計

LINK TO LEARNING学習リンク
It is always good to see the concepts we learn in action. Visit this site to write an HLL program and see the corresponding assembly (https://openstax.org/r/76HLLProgram) at the same time. Any change you make to the HLL code will have an effect on the assembly.
英語のヒント
学んだ概念が実際に働く様子を見るのは、いつでも役に立つ。このサイトでHLLのプログラムを書き、対応するアセンブリを同時に確認してみよう(https://openstax.org/r/76HLLProgram)。HLLのコードを変更すれば、その変更はアセンブリにも影響する。

Assembly Operationsアセンブリの操作

In any ISA, all assembly instructions fall into one of three categories:
英語のヒント
どのISAでも、アセンブリ命令はすべて、次の三つの分類のいずれかに入る。
1. Data movement: from register to register, from memory to register, and from register to memory
1. データ転送:レジスタからレジスタ、メモリからレジスタ、レジスタからメモリへの転送。
2. Arithmetic and logic operations including addition, subtraction, AND, OR, and so on
2. 算術・論理演算:加算、減算、AND、ORなど。
3. Control instructions, which are the instructions that implement the “go to” operations, whether conditional or non-conditional (category also includes procedure calls)
英語のヒント
3. 制御命令:条件付き・無条件の「go to」操作を実装する命令である。この分類には、手続き呼び出しも含まれる。
Data Movement Operationsデータ転送操作
The data movement in assembly takes the form movx source or destination where:
アセンブリのデータ転送は、movx source or destinationという形を取る。各要素の意味は、次のとおりである。
• “x” specifies the number of bytes to be moved from source to destination. It can take one of the following values: “b” means 1 byte, “w” (word) means 2 bytes, “l” (long) means 4 bytes, and “q” (quad word) means 8 bytes. If you think about it, these are the sizes of all the data types we have in any HLL.
英語のヒント
• 「x」は、転送元から転送先へ移すバイト数を指定する。「b」は1バイト、「w」(word)は2バイト、「l」(long)は4バイト、「q」(quad word)は8バイトである。考えてみると、これらはHLLで扱うすべてのデータ型の大きさである。
• the source and destination can be any of the operand types we mentioned earlier. There are only three combinations that are not allowed. The first is to move immediate to immediate as it does not make sense. The second is to move from memory to memory because the CPU must be involved. The last prohibited combination is when the destination is immediate as it also does not make any sense. Some examples:
英語のヒント
• 転送元と転送先には、先に述べたオペランドのどの種類でも指定できる。ただし、許されない組み合わせが三つだけある。第一は即値から即値への転送で、意味をなさないためである。第二はメモリからメモリへの転送で、CPUが関与しなければならないためである。最後は、転送先が即値の場合である。これも意味をなさない。例を見よう。
◦ “movq %rax, %rbx” Move 8 bytes from register rax and put them in register rbx, which is not really a move; it is a copy.
◦ 「movq %rax, %rbx」:raxレジスタから8バイトを取り、rbxレジスタに入れる。実際には移動ではなく、コピーである。
◦ “movq (%rax, %rbx, 4), %rcx” This is a bit complicated. It involves three steps: first, calculate [rax + (4 × rbx)]; second, use this calculated value as an address and go to the memory at that address; and third, get 8 bytes, starting from the address you calculated (do not forget the “q” at the end of the mov instruction) and put them in register rcx. Now do you see why x86 is CISC where C means complex?
英語のヒント
◦ 「movq (%rax, %rbx, 4), %rcx」:こちらは少し複雑である。三つの手順を含む。まず[rax + (4 × rbx)]を計算する。次に、計算した値をアドレスとして、その場所のメモリへアクセスする。最後に、計算したアドレスから始まる8バイトを取得し、rcxレジスタに入れる。mov命令の末尾の「q」を忘れないでほしい。これで、x86がCISCで、そのCがcomplex(複雑)を意味する理由がわかっただろうか。
Arithmetic and Logic Operations算術・論理演算
The arithmetic and logic operations involve very well-known operations such as add, subtract, multiply, and or, xor, shift left, and shift right. As you may have guessed, these are operations that require two operands. Look at the type of operands that we investigated earlier. For example:
英語のヒント
算術・論理演算には、加算、減算、乗算、and、or、xor、左シフト、右シフトなどの、よく知られた操作が含まれる。おそらく想像したとおり、これらは二つのオペランドを必要とする演算である。先ほど調べたオペランドの種類を見てみよう。たとえば、次のようになる。
addq %rax, %rbx
means rax = rax + rbx.
Additionally, there are some one operand operations such as increment and decrement. There are some complexities involved in multiplication and division where there are different instructions for signed and unsigned integers. And, for the division, yet another complexity is where to store the remainder.
英語のヒント
さらに、インクリメントやデクリメントなど、一つのオペランドを使う演算もある。乗算と除算には、符号付き整数用と符号なし整数用で異なる命令があるという複雑さがある。また、除算には、余りをどこへ保存するかという別の複雑さもある。
Comparison and Test Operations比較とテストの操作
To be able to implement the complex data flow in HLLs (e.g., switch case or if-else), the assembly language must support conditional and unconditional go to. In x86 parlance, it is called a jump instruction. The generic form of jump is the jump instruction followed by a label. The label is a variable name that we give to an assembly instruction. This is needed because if you want to say (go to this instruction), how can you define “this” instruction? The label takes the following form: label: instruction. For example:
英語のヒント
HLLの複雑なデータの流れ(たとえばswitch caseやif-else)を実装するには、アセンブリ言語が条件付き・無条件のgo toに対応している必要がある。x86の用語では、これをジャンプ命令という。一般形は、ジャンプ命令の後ろにラベルを置いたものだ。ラベルは、アセンブリ命令に付ける変数名である。「この命令へ移動せよ」と指示するなら、「この」命令が何かを指定する必要があるためである。ラベルは、label: instructionという形を取る。たとえば、次のようになる。
part: movq %rax, %rbx
. . . .
jmp part
In this example, “part” is the label for the move instruction. The “jmp” is a nonconditional jump; that is, it is always executed. There are some for conditional branches too. Let’s look at one of them.
英語のヒント
この例では、「part」が転送命令のラベルである。「jmp」は無条件ジャンプで、つまり常に実行される。条件付き分岐の命令もある。その一つを見てみよう。
For the jump if equal, or je, label, this instruction means jump to label if the zero flag is set to 1. In the programmer’s view of the assembly program, there are some flags that give information on the previous instruction (refer to Figure 5.18). One of the flags is called the zero flag, and it is set to 1 if the previous instruction has generated 0 as a result. For example, subtracting two registers and getting the result of 0.
英語のヒント
等しい場合にジャンプするje labelという命令は、ゼロフラグが1ならlabelへジャンプする、という意味である。プログラマーから見たアセンブリプログラムには、直前の命令について情報を示すフラグがある(図5.18を参照)。その一つがゼロフラグで、直前の命令の結果が0の場合、1に設定される。たとえば、二つのレジスタの減算結果が0になった場合である。