编程短课 (A Short Course In Programming by Tom Pittman)

译者序

在高级编程语言普及的如今,外行人常把机械指令和组合语言神化成屠龙技,也有不少自以为內行的人认为用机械指令编程是一种不再被需要的技术。哈哈,这么说可以都有点过时了,这年头的科幻电影里关闭人工智能都是用 sudo 了,可能知道组合语言的外行都不多了(暴论,笑)。组合语言,更常见叫「汇编」,在《組合語言之藝術》中被咬文嚼字了一番,认为把 Assembly 译成汇编是沒文化的体现,嗯,大概是 Compile 才是汇编的意思吧,不管它了。实际上我翻译这书的目的就是让更多中文用户,尤其是来自文学而不只限于计算机科学的知道,计算机原来是这样简单的。

本书介绍的 COSMAC ELF 使用 RCA 1802 微处理器,是 70 年代出现的几种早期个人电脑之一,说是电脑也只不过是以电路图出版需要拥有者自己组装的玩具,不比今天的计算器复杂多少。1802 机械指令的特別之处,是其远在今天十分流行的 RISC(Reduced Instruction Set Computer,精简指令集计算机) 这一概念提出前就实现了 RISC 的主要特点:有数十个之多的通用寄存器,和很小的指令集。就连今天几乎所有计算机都在用的 amd64 虽并非 RISC,却也是利用 microcode 在更底层实现了类似 RISC 的环境。在不过很可惜的是,COSMAC ELF 这样的 8 位计算机在数据动辄几 GB 的今天什么都干不了。各位可不要伤心,在熟悉 COSMAC ELF 之后很快就能学会最先近的现代 RISC 计算机。

就算从来沒有组装过电子设备,也请不要担心,COSMAC ELF 有一些相当成熟的模拟器,比实体更方便了解其构造。

(本文是 2018 年起草的手稿,因为精力有限,后面部分没有翻译,贴上原文以示意)

http://www.elf-emulation.com/emulator.html

https://www.emma02.hobby-site.com

LdBeth

第零章 如何读这本书

编程如开车——读书不能教会人开车,得真刀真枪地上。但书能让人知道车灯要怎么开,方向盘要怎么打;编程同理。本书要教你计算机是怎么工作的,这样你就能开始自学编程了。

开车用少量肌力;编程用少许脑力。开车是转方向盘,踩油门,等;编程是输入指令,读输出,等。大多数人有足够肌力开车;很少有人沒有足够脑力编程。只要你知道怎么开车,然后学完本书,你就既会开车又能编程了。

坐在驾驶席上动小指不能学会开车;坐在安乐椅上翻书也不能学会编程。准备好每章花上几小时乃至几晚,不过前两章并不是很难。读完本书,你就比寻常计算机爱好者知道更多了。

自输入/出与分支,每章都会有些示意某种功能的小程式。载入程式并运行是最好的学习办法(如果不知道这是什么意思,在 ELF II 我会解释),请试着理解程式的功能与原理。为了助于理解,我会就你所看到的现象提问。请尽量回答问题,如果实在答不出的话,人之常情嘛,在问题之后的几句话里我会给出提示,或者隐晦地解答。在解決疑问之前不要看后面章节,这样除了感到更加疑惑之外你什么都得不到。

这本书以每章的內容都不会涉及后面章节为前安排內容。每遇到新的术语我会以粗体标出并给出定义。

还有一件事™,计算机并沒有比你聪明,只会完全照指令工作,而不能自动理解你想做什么。当计算机沒有像你预期那样工作时,99.99% 是你的错。本书中的程式亦已经验证无误。所以,当你觉得程式的结果不正常时,请确认输入。如果程式与书上內容核对无误,那么第二种可能是你并沒有理解程式的原理,请重读当前章节。

最后,希望你不要被吓到。只要找到要领,计算机还是很有趣的。能让计算机做复杂的任务固然很强,让计算机完全听从自己的命令更高。那么,放松,看下一章计算机是什么。

第一章 计算机是什么

首先,什么不是计算机?文具店里百来圆的科学计算器不是计算机;隔壁马阿姨的电动缝纫机不是计算机;楼下打印店的打印机不是计算机;电视放的录像帯不是计算机。它们可能和计算机有关,但确实不是。什么是计算机?

计算机是种自动对数据执行任意操作序列的设备。打印机自动对数据执行操作(打印),但不能操行预设以外的操作。计算器能执行任意操作,但不是自动执行,不过更高级的可编程计算器的确可以算计算机。电动缝纫机能自动操行操作,但它操作的是布料和纺线,而不是数据。那么,什么是数据?

数据是对问题给出的答案。这答案可以是数字(问:7 加 3 后除以 8 ?答:1.25。问:第四个数?答:4。)也可以是真假(问:如果所有人都会死,苏格拉底是人,那么苏格拉底会死吗?答:是)也可以是一组字符 (Question: How is my name spelled? Answer: “Tom”.). The answer may be long (Question: How did Columbus come to America? Answer: In three ships, the Pinta, the Nina, and the Santa Maria, which were paid for by the Spanish royal government in 1492, etc…) or very short (Question: How many apples are in an empty paper bag? Answer: 0.). A single Datum may be the answer to several questions at once (Questions: Who was the 38th President of the United States? What is the name of the second largest automobile maker in the world? How do you cross a shallow stream when there is no bridge? Answer to all three: Ford.) or it may be the answer to a question nobody thought of asking: (Question: ? Answer: “@+!*=”.). It is data if there could be at least one question for which these are the answers.

When the computer is not actually operating on it, the data is kept in memory. This is just as you might keep all the answers you know in your mind’s memory. We often say the data is stored in the memory. For convenience, the memory is organized into a number of words, all the same size. A word may contain one answer or it may contain several small answers; large answers often require many words in memory. The smallest answer we can possibly have in a computer is either a “yes” or a “no”. This is the same size as 0 or 1, True or False, Hot or Cold, or any other answer to a question which has only two possible answers. The amount of memory needed to store one of these smallest answers is called a bit. Each word in the computer memory is some fixed number of bits, say 8 or 16 or 60; the size of the computer word is determined by the person who designed the computer.

Most computers have several different kinds of memory. One kind is called RAM and will have hundreds or (usually) thousands of words. Another kind is called file storage and may have a million or more words, but it is harder for the computer to use this memory; more about this later. A third type of memory which most computers have is very easy for the computer to use. It is called registers, and there are usually only a few words of these. Often we tell computers apart by the number and kinds of registers they have.

Among the computer’s registers, one (or sometimes several) is particularly important, because it holds the answer the computer is working on at any particular instant. Such a register is usually called an accumulator. It is normally a single word in the computer, and almost everything the computer is able to do to its data, it does to the data in the accumulator. I will mention some other useful registers shortly.

With thousands of words of data in memory (and we usually mean RAM when we say “memory”), one very important question to be answered is, “Where is the word I want?” We answer this question by numbering the words in memory, starting with zero (this turns out to be easier for the computer than starting at one). The number of each word is called its address, something like the number on your house. Obviously, every answer stored in memory has another answer associated with it, namely, “What is its address?” Often there are special registers in the computer that are particularly suited to holding this kind of data, that is, the addresses of other data stored in memory. We sometimes call these address registers. The computer is able to operate on the data in the address registers also.

Another very important kind of data in the computer memory answers the question, “What operation will the computer perform next?” This is important because we control the sequence of operations the computer performs by the proper arrangement of this particular kind of data. Each datum which answers the question, “What operation should the computer perform?” is called an Instruction, because it “instructs” the computer in its task. The whole sequence of instructions is called a Program. Of course there is an address register that points to the next instruction the computer will obey. This register is called the Program Counter or PC. Notice that the only difference between the data which you consider to be the answers to your questions and the data which are the answers to the computer’s questions about what to do next is this: Who is doing the asking? When the computer asks, “What next?” the answer that comes out of memory is, by definition, an instruction. When the computer asks for data to operate on according to your program, the answer that comes out of memory is not an instruction, but “real data”. (People usually prefer to say that the program is not data so we can use the word “data” to mean the answers to our questions, not those of the computer.)

When a computer is operating normally, we say it is running. It continually goes through the following pair of steps: First, the Program Counter is used to address an instruction; that is, the computer looks at the word in memory whose address is in the PC. This is called the instruction fetch. If the instruction occupies more than one word, all the words of this instruction are fetched. The PC is incremented (added to) by the number of words in the instruction, so that it now points to (i.e. addresses) the next sequential instruction in the program. Second, the computer performs or executes the operation specified by the instruction just fetched. The computer repeats this fetch-execute cycle for the next instruction, and so on, many thousands and millions of times.

The continuous repetition of the fetch-execute cycles in the computer may seem a little monotonous, until you realize that the instructions being executed are quite varied, and their sequence can make for programs having widely differing results. There are, for example, instructions to copy a datum from someplace in memory into the accumulator and others to copy the datum in the accumulator into some memory word; we call these loads and stores. There are instructions to add data to or subtract data from the accumulator, or to perform certain logical operations on it (which we will describe in detail in Chapter 5). There are instructions to manipulate the data in the other registers. Perhaps most important, there are instructions which can change the contents of the Program Counter; we call these branches. Chapter 3 covers these in some detail, but for now it is important to realize that the computer is able to alter its own sequence of program execution. Finally, there are instructions which interact with the outside world.

Up to now we have not mentioned how data gets into or out of the computer. I suppose it would still be a computer if it had no way to pass data out to the rest of the world or to accept new data to operate on, but it would not be worth much (a few of the really fast super-computers work this way, but they cheat). We mortals need what we call Input/Output or I/O.

Input refers to the way data gets into the computer. Usually the computer will have special registers which can be loaded with data from some external source, and perhaps some instructions by which the computer is able to copy this data into its internal memory. Output is the reverse operation: Some type of instruction enables the computer to copy data from its internal memory into special output registers, which the outside world is able to examine. Often these special Input and Output registers are called I/O ports. Input might come from a human being (such as some buttons or switches or a typewriter-like keyboard), or it may come from special equipment which operates the file storage. Similarly, output may go to lights for humans to look at, or perhaps to control the picture on a TV set; it may also go to the file storage.

Most file storage, as I mentioned earlier, is more difficult for the computer to use than its internal memory (RAM and registers). This is because it tends to be relatively slow, and the computer must pick off (on input) or send (on output) the words one at a time, at the right speed. If the file consists of magnetic tape (such as used with audio cassette systems) the speed may also depend on the human who is operating the controls.

In summary, then, we have seen that a computer consists of some kind of control mechanism (we really did not talk much about it, only assumed that it existed), and a memory consisting of registers and RAM, and some I/O. Perhaps there may be some file memory attached to the I/O. The computer operates on some of the data in memory, according to instructions in another part of the memory called the program.

In the next chapter we will look at a particular computer, and see how it fits with this generalized picture.

Leave a Reply