[learncpp.com] 0 2 Introduction to programming languages 介绍编程语言

仅仅是翻译给自己以及一些有需要的同学看,并不是专业翻译,个人能力有限,勿喷。
如果有好的提议,请私信我!
文章原址:https://www.learncpp.com/cpp-tutorial/introduction-to-programming-languages/


0 2 Introduction to programming languages

Modern computers are incredibly fast, and getting faster all the time. However, computers also have some significant constraints: they only natively understand a limited set of commands, and must be told exactly what to do.

现代电脑的处理速度都十分的快,并且一直在加快。然而,电脑还是有一些关键的限制:他们只能理解一些简单的指令,且必须被告知做什么。

A computer program (also commonly called an application) is a set of instructions that the computer can perform in order to perform some task. The process of creating a program is called programming. Programmers typically create programs by producing source code (commonly shortened to code), which is a list of commands typed into one or more text files.

一个电脑程序(也被叫做应用)是一套说明电脑要完成一些任务该做些什么。创造这样一个程序的过程叫做编程。编程者通常创造源代码(简称代码)来生成程序,这一套指令被写入一个或者多个文件中。

The collection of physical computer parts that make up a computer and execute programs is called the hardware. When a computer program is loaded into memory and the hardware sequentially executes each instruction, this is called running or executing the program.

组成计算机物理部分并执行程序的集合组成了一台电脑。当一个电脑程序被加载到内存,随后硬件提取这些指令,这叫做运行程序。

Machine Language 机器语言

A computer’s CPU is incapable of speaking C++. The limited set of instructions that a CPU can understand directly is called machine code (or machine language or an instruction set).

一个电脑CPU没有能力说C++。有限的指令集使CPU能直接理解叫做一个机器代码的东西(或者叫机器语言或者指令集)。

Here is a sample machine language instruction: 10110000 01100001

这里有个简单的机器语言指令:10110000 01100001

Back when computers were first invented, programmers had to write programs directly in machine language, which was a very difficult and time consuming thing to do.

回看当电脑第一次被发明出来时,程序员要直接通过机器语言写程序,这是多么困难和费时的事情。

How these instructions are organized is beyond the scope of this introduction, but it is interesting to note two things. First, each instruction is composed of a sequence of 1’s and 0’s. Each individual 0 or 1 is called a binary digit, or bit for short. The number of bits that make up a single command vary -- for example, some CPUs process instructions that are always 32 bits long, whereas some other CPUs (such as the x86 family, which you are likely using) have instructions that can be a variable length.

关于这些指令如何被组织起来的问题已经超出了课程范围,但是有趣的是注意到两件事。第一,每一个指令都是1或者0组成的,每个0或者1被叫做一个二进制数,或者bit。这个比特数组成了一个简单的指令变化,例如,一些CPU指令通常32位宽,还有一些其他的CPU有着可变长度的指令。

Second, each set of binary digits is interpreted by the CPU into a command to do a very specific job, such as compare these two numbers, or put this number in that memory location. However, because different CPUs have different instruction sets, instructions that were written for one CPU type could not be used on a CPU that didn’t share the same instruction set. This meant programs generally weren’t portable (usable without major rework) to different types of system, and had to be written all over again.

第二,每一个二进制数集合被CPU解释成一个个指令去做特殊的工作,例如比较两个数字,或者把某个数放在指定的存储位置。然而,因为不同的CPU有不同的指令集,写在这个CPU上的指令,却不能被另一个CPU给读取。这意味着程序是不可移植的(不可复用于两个不同的系统),必须重新写一遍。

Assembly Language 汇编语言

Because machine language is so hard for humans to read and understand, assembly language was invented. In an assembly language, each instruction is identified by a short abbreviation (rather than a set of bits), and names and other numbers can be used.

因为机器语言对人类来说阅读和理解是如此的困难,汇编语言被发明了出来。在汇编语言上,每个指令都用一个简称来区分(而不是一串bit流),并且可以使用名称和其他数字。

Here is the same instruction as above in assembly language: mov al, 061h

这里有个与上面相同的汇编语言指令:mov al, 061h

This makes assembly much easier to read and write than machine language. However, the CPU can not understand assembly language directly. Instead, the assembly program must be translated into machine language before it can be executed by the computer. This is done by using a program called an assembler. Programs written in assembly languages tend to be very fast, and assembly is still used today when speed is critical.

这使得汇编语言相比较机器语言更容易阅读和书写,然而,CPU并不能直接理解汇编语言。汇编语言必须被翻译成机器语言才能被电脑给读取。这是通过使用一个叫做汇编程序的程序来完成的。通过汇编编写的程序运行的会更加快,在今天一些对运行速度要求苛刻的场合下,汇编仍然在被使用。

However, assembly still has some downsides. First, assembly languages still require a lot of instructions to do even simple tasks. While the individual instructions themselves are somewhat human readable, understanding what an entire program is doing can be challenging (it’s a bit like trying to understand a sentence by looking at each letter individually). Second, assembly language still isn’t very portable -- a program written in assembly for one CPU will likely not work on hardware that uses a different instruction set, and would have to be rewritten or extensively modified.

然而,汇编还是有一些缺点。第一,汇编语言仍然需要大量指令去做简单的任务。尽管单个指令可读,但理解整个程序还是具有挑战性(有点像通过看每个字母来理解一个句子)。第二汇编语言仍然非常的不好移植,一个汇编程序用不同的指令集来写就没法工作了,需要重写或者大量修改。

High-level Languages 高级语言

To address the readability and portability concerns, new programming languages such as C, C++, Pascal (and later, languages such as Java, Javascript, and Perl) were developed. These languages are called high level languages, as they are designed to allow the programmer to write programs without having to be as concerned about what kind of computer the program will be run on.

为了解决可阅读性可移植性的问题,新的程序语言例如c,c++(或者其他语言)诞生了。这些语言被称为高级语言,而他们被设计出来就是为了让程序员写程序不需要再担心自己的程序需要跑在哪台电脑上。

Here is the same instruction as above in C/C++: a = 97;

这里有一个与上面相同的指令(用CC++编写的): a = 97

Much like assembly programs, programs written in high level languages must be translated into a format the computer can understand before they can be run. There are two primary ways this is done: compiling and interpreting.

很像汇编语言,通过高级语言写的程序在运行前必须被翻译成电脑可以理解的格式。这里有两个基本的方式:编译和解释。

A compiler is a program that reads source code and produces a stand-alone executable program that can then be run. Once your code has been turned into an executable, you do not need the compiler to run the program. In the beginning, compilers were primitive and produced slow, unoptimized code. However, over the years, compilers have become very good at producing fast, optimized code, and in some cases can do a better job than humans can in assembly language!

编译器是一段阅读源代码,产生一段独立的被执行程序的程序。一旦你的代码已经生成了可执行文件,你就不需要再用编译器来运行程序了。开始,编译器是原始的,产生缓慢的、未优化的代码。然而,经过数年,编译器已经很擅长产生快速优化的程序,在某些情况下,可以比人类做的汇编语言更好。

Here is a simplified representation of the compiling process:

这是一个简单的编译过程

[learncpp.com] 0 2 Introduction to programming languages 介绍编程语言

Since C++ programs are generally compiled, we’ll explore compilers in more detail shortly.

由于C++程序通常是编译的,我们将很快更详细地探讨编译器。

An interpreter is a program that directly executes the instructions in the source code without requiring them to be compiled into an executable first. Interpreters tend to be more flexible than compilers, but are less efficient when running programs because the interpreting process needs to be done every time the program is run. This means the interpreter is needed every time the program is run.

解释器是一段直接从源代码中提取指令而不需要将他们编译成可执行文件。解释器比编译器更灵活,但是在运行程序时没有编译器高效,因为每次运行都要完成解释过程。这意味着解释器在程序运行时一直都需要运行。

Here is a simplified representation of the interpretation process:

这里是解释器运行过程

[learncpp.com] 0 2 Introduction to programming languages 介绍编程语言

Optional reading选择阅读

A good comparison of the advantages of compilers vs interpreters can be found here.

这里可以很好地比较编译器和解释器的优势。

Most languages can be compiled or interpreted, however, traditionally languages like C, C++, and Pascal are compiled, whereas “scripting” languages like Perl and Javascript tend to be interpreted. Some languages, like Java, use a mix of the two.

许多语言可以被编译和解释,然而,传统的语言例如C,C++和Pascal是被编译的,然而一些脚本语言例如Perl,Javascript倾向被解释,一些语言,例如java,两者都可。

High level languages have many desirable properties.

高级语言有许多令人满意的特性

First, high level languages are much easier to read and write because the commands are closer to natural language that we use every day. Second, high level languages require fewer instructions to perform the same task as lower level languages, making programs more concise and easier to understand. In C++ you can do something like a = b * 2 + 5; in one line. In assembly language, this would take 5 or 6 different instructions.

首先,高级语言更容易阅读和书写因为指令更接近我们每天使用的自然语言。第二,高级语言需要极少的指令就可以完成一些简单的任务相比较于低级语言来说,是的程序更加精简和易于理解,在C++你可以做一些事例如a = b * 2 + 5,只需要一条指令,而在汇编语言中,你需要5到6条不同的指令。

Third, programs can be compiled (or interpreted) for many different systems, and you don’t have to change the program to run on different CPUs (you just recompile for that CPU). As an example:

第三,程序可以被编译(或者解释)给不同的系统,你不需要因为CPU的不同而修改你的程序。例如

[learncpp.com] 0 2 Introduction to programming languages 介绍编程语言

There are two general exceptions to portability. The first is that many operating systems, such as Microsoft Windows, contain platform-specific capabilities that you can use in your code. These can make it much easier to write a program for a specific operating system, but at the expense of portability. In these tutorials, we will avoid any platform specific code.

可移植性有两个一般的例外。首先是许多操作系统,例如微软windows,包含特定于平台的功能,你可以在代码中使用,这使得你更容易在操作系统中编写程序,但是代价是可移植性,在教程中,我们会避免使用平台特性的代码。

Some compilers also support compiler-specific extensions -- if you use these, your programs won’t be able to be compiled by other compilers that don’t support the same extensions without modification. We’ll talk more about these later, once you’ve installed a compiler.

一些编译器还支持编译特殊扩展,如果你使用了他,你的程序将不能被其他编译器编译,我们将会在你安装编译器时谈论这个。

Rules, Best practices, and warnings 规则,最佳方案以及警告

As we proceed through these tutorials, we’ll highlight many important points under the following three categories:

当我们继续这些章节,我们会强调以下三个重要的点

::: alert-info

Rule

Rules are instructions that you must do, as required by the language. Failure to abide by a rule will generally result in your program not working.

:::

规则是你必须按照语言的要求去做的指令。不遵守规则通常会导致您的程序无法运行。

::: alert-success
Best practice

Best practices are things that you should do, because that way of doing things is generally considered a standard or highly recommended. That is, either everybody does it that way (and if you do otherwise, you’ll be doing something people don’t expect), or it is superior to the alternatives.

:::

最佳方案是一些你应该去做的事情,因为有些事情是标准且极力推荐的,要么每个人都这样做(如果你不做,就不能达到更好的效果),或者它优于其他选择。

::: alert-danger

Warning

Warnings are things that you should not do, because they will generally lead to unexpected results.

:::

警告是你不应该做的事情,因为它们通常会导致意想不到的结果。


[learncpp.com] 0 2 Introduction to programming languages 介绍编程语言

上一篇:数据分析的介绍


下一篇:syslog的使用