プリンキピア

第4章アルゴリズムの言語による実現:低水準プログラミング言語

Low-Level Programming低水準プログラミング

A programming language’s level of abstraction is the degree to which a computational model, programming language, or piece of software relates to computer hardware. A low-level language has a low level of abstraction, while a high-level language has a high level of abstraction. In a low-level language, the programmer must describe an algorithm in terms that the computer hardware can understand directly. Source code must describe details such as the location of data in memory and the particulars of how the computer calculates arithmetic.
英語のヒント
プログラミング言語の抽象化の水準は、計算モデル、言語、ソフトウェアが、コンピューターのハードウェアにどの程度関わるかを表す。低水準言語の抽象化の水準は低く、高水準言語の抽象化の水準は高い。低水準言語では、プログラマーは、ハードウェアが直接理解できる形でアルゴリズムを記述しなければならない。ソースコードは、データのメモリ内の位置や、算術計算の具体的な方法など、細部まで記述する必要がある。
Generally, low-level programs execute faster but are more labor-intensive to create and maintain. In a low-level language, the programmer is forced to think deliberately about how the computer hardware executes, so the finished program usually executes efficiently. However, that deliberate thought takes time and effort. In a high-level language, the programmer is not burdened with thinking about so many details and can finish their work faster while preventing certain types of programming errors from occurring. A compiler automates converting high-level code to low-level code, but that automated process can introduce some inefficiency. In some settings code performance is more important, and in other settings programmer productivity is more important, which is why we have both kinds of languages.
英語のヒント
一般に、低水準のプログラムは実行が速いが、作成と保守に手間がかかる。プログラマーはハードウェアの実行方法について意識的に考えざるを得ないため、完成したプログラムは通常、効率よく動く。しかし、意識的に考えるには時間と労力が必要である。高水準言語では、多くの細部を考える負担がなく、ある種のプログラミングの誤りを防ぎながら、作業を早く終えられる。コンパイラーは高水準コードから低水準コードへの変換を自動化するが、その過程で非効率が入り込む場合がある。ある状況ではコードの性能が、別の状況ではプログラマーの生産性がより重要になるため、両方の言語が存在する。
We can think of low-level programming languages in terms of cooking: when you cook a meal from scratch, you control every ingredient and every detail of preparation, so the finished meal has precisely the taste and nutrition that you desire. An alternative is to prepare a meal that uses some prepared ingredients, and when you do that, you lose a lot of control over details, but the process is significantly faster and easier.
英語のヒント
低水準プログラミング言語は、料理にたとえて考えられる。一から料理を作るなら、すべての食材と調理の細部を管理できるため、望みどおりの味と栄養を持つ食事ができる。代わりに、下ごしらえ済みの食材を一部使う方法もある。この場合、細部を制御する力は大きく失われるが、調理はずっと速く、簡単になる。
There are many examples of low-level programming languages, but the most fundamental language understood by computers is made up of a sequence of digits.
低水準プログラミング言語には多くの例があるが、コンピューターが理解する最も基本的な言語は、数字の列でできている。
Machine Code機械語コード
The sequence of binary digits (bits) that can be understood and executed directly by a computer is called machine code (Figure 4.5). Machine code is the most low-level language. It is also known as binary code. It is a program in the native format that can be understood by a CPU, in the form of a long series of 0s and 1s. Machine code, or binary code, is the only computational model that a computer can execute; a program written in any other language must be compiled or interpreted into machine code before the program can run. The CPU of a computer is a computer chip capable of executing machine code programs (Figure 4.6). It is impractical for humans to work with machine code directly because a machine code program is not designed to be human-readable. The patterns of 0s and 1s are designed to be convenient for a CPU to decode, not for humans to manipulate; and such programs are long, typically millions or billions of bits long. Another obstacle is that machine code is hardware dependent. As discussed in 5.3 Machine-Level Information Representation, every processor architecture has its own machine language, so machine language written for one architecture (for example, INTEL X86) cannot work on any other architecture (such as ARM). When the very first digital computers were built, and programming languages had yet to be invented, programmers had no choice but to write machine code by hand. However, this is extremely time-consuming and prone to errors, so is almost never done today.
英語のヒント
コンピューターが直接理解し、実行できる2進数の桁(ビット)の列を、機械語コードという(図4.5)。最も低水準の言語で、バイナリーコードとも呼ばれる。0と1の長い列という、CPUが理解できる本来の形式のプログラムである。機械語コードは、コンピューターが実行できる唯一の計算モデルであり、ほかの言語で書かれたプログラムは、実行する前に機械語へコンパイルまたは解釈しなければならない。CPUは、この機械語のプログラムを実行できるチップである(図4.6)。機械語のプログラムは人間が読めるように設計されていないので、人間が直接扱うのは実用的でない。0と1のパターンは、人が操作しやすいようにではなく、CPUが解読しやすいように作られている。しかも、通常は数百万から数十億ビットもの長さになる。もう1つの障害は、機械語がハードウェアに依存することである。5.3節「機械水準の情報表現」で述べるように、プロセッサーのアーキテクチャーごとに機械語は異なるため、あるアーキテクチャー(たとえばINTEL X86)向けの機械語は、別のもの(ARMなど)では動かない。最初期のデジタルコンピューターが作られ、プログラミング言語がまだ発明されていなかったころ、プログラマーは機械語を手で書くしかなかった。しかし、これは非常に時間がかかり、誤りも生じやすいため、現在はほとんど行われない。
Figure 4.5

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 4.5 Machine code, with its 0s and 1s, is the only computational model a computer can execute. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.5 0と1からなる機械語コードは、コンピューターが実行できる唯一の計算モデルである。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
Figure 4.6

credit: modification of "IIci" by raneko/Flickr, CC BY 2.0 · CC BY 2.0 · Source出典

Figure 4.6 A computer chip is a computer’s central processing unit (CPU). (credit: modification of "IIci" by raneko/Flickr, CC BY 2.0)
図4.6 コンピューターチップは、コンピューターの中央処理装置(CPU)である。(クレジット:raneko/Flickrによる「IIci」を改変、CC BY 2.0)
Assembly Languageアセンブリ言語
The low-level language in which every statement corresponds directly to a machine instruction is called assembly language. Assembly language is a small step above machine code but is still a very low-level language. Assembly language is a textual representation of machine code. Just like a machine code program, an assembly language program is a series of instructions that a CPU will execute. However, rather than writing the instructions in a binary format of 0s and 1s, each instruction has a textual name such as “ADD” or “MOVE.” An assembler is a program that translates assembly language source code into machine code. As shown in Figure 4.7, an assembler translates each textual instruction into the corresponding list of 0 and 1 bits.
英語のヒント
各文が機械の命令へ直接対応する低水準言語を、アセンブリ言語という。機械語から少し上がった段階だが、依然として非常に低水準である。機械語を文字で表したもので、機械語のプログラムと同様に、CPUが実行する命令の列からなる。ただし、0と1の2進形式で書く代わりに、各命令には「ADD」「MOVE」などの文字による名前がある。アセンブラーは、アセンブリ言語のソースコードを機械語に変換するプログラムである。図4.7のように、各文字命令を対応する0と1のビット列に変換する。
Figure 4.7

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 4.7 An assembler is a program that translates assembly language source code into machine object code. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.7 アセンブラーは、アセンブリ言語のソースコードを、機械語のオブジェクトコードに変換するプログラムである。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
While it is practically impossible for a human to write a complete program in machine code, writing programs in assembly language is viable. Because assembly language is extremely low-level, these programs tend to run quickly, but they are labor-intensive to write, and are machine-dependent. This type of programming was sufficient in the 1960s, 1970s, and 1980s when software was written for one-off capital-intensive machines, such as multimillion-dollar mainframes or space vehicles. Programmer labor was comparatively cheap then, and there was no need to move programs to different hardware. But today, we expect applications to be compatible with multiple kinds of platforms, including phone, computer, and gaming systems. Programmer labor is more expensive than computer hardware, so writing entire programs in assembly language is uneconomical. Consequently, programs are often written predominantly in a high-level language, with assembly language used to write short excerpts on an as-needed basis. Writing code in a higher level language makes it easier to write correct code that does not have defects.
英語のヒント
人が機械語でプログラム全体を書くのは事実上不可能だが、アセンブリ言語なら実行可能である。非常に低水準なので、プログラムは速く動きやすい反面、書くのに手間がかかり、機種にも依存する。数百万ドルのメインフレームや宇宙機のように、多額の投資を要する特定の機械向けにソフトウェアを書いていた1960〜1980年代には、この方法で十分だった。当時はプログラマーの労働が比較的安く、別のハードウェアへ移す必要もなかった。しかし現在、アプリケーションには、電話、コンピューター、ゲーム機など、複数のプラットフォームへの対応が求められる。プログラマーの労働のほうがハードウェアより高価なので、全体をアセンブリ言語で書くのは不経済である。そのため、プログラムの大部分は高水準言語で書き、必要な箇所に限って、短い部分をアセンブリ言語で書くことが多い。より高水準の言語なら、欠陥のない正しいコードを書きやすい。