Beginning Programming All-in-One For Dummies. Wallace Wang
Чтение книги онлайн.
Читать онлайн книгу Beginning Programming All-in-One For Dummies - Wallace Wang страница 19
Machine language is considered the native language of CPUs, but almost no one writes a program in machine language because it’s so tedious and confusing. Mistype a single 1 or 0, and you can accidentally give the wrong instruction to the CPU. Because writing instructions in machine language can be so difficult and error-prone, computer scientists have created a somewhat simpler language: assembly language.
Using assembly language as a shortcut to machine language
The whole purpose of assembly language is to make programming easier than machine language. Basically, one assembly language command can replace a dozen or more machine language commands. So, instead of requiring you to write ten machine language commands (and risk making a mistake in all ten of those commands), assembly language lets you write one command that does the work of ten (or more) machine language commands.
Not only does this reduce the chance of mistakes, but it also makes writing a program in assembly language much faster and easier. Best of all, assembly language commands represent simple mnemonics such as MOV
(move) or JMP
(jump). These mnemonic commands make assembly language much easier to understand than a string of binary commands (1s and 0s).
To understand how assembly language works, you must first understand how processors store and manipulate data. The processor is the “brain” of the computer that does all the work. By itself, the processor is fairly useless.
Think of Einstein's brain floating in a jar of formaldehyde. It may be one of the smartest brains in the world, but if it can’t communicate with the outside world, it’s completely useless as anything other than a very unusual paperweight. Like Einstein’s brain in a jar, your computer’s processor is useful only if it can communicate with the outside world. The processor communicates with the other parts of the computer through a series of wires called a bus.
When a processor needs to work with data, it retrieves it from another part of the computer (such as the hard disk or memory) and temporarily stores that data in a storage area called a register, as shown in Figure 1-1.
FIGURE 1-1: A processor uses its registers to temporarily store data.
The processor then edits the data in its registers and sends the changed data back to another part of the computer, such as the memory or hard disk.
So, computer programming progressed from physically rearranging wires and switches (with ENIAC), to flipping switches using 1s and 0s (with machine language), to telling the computer which data to store in which registers and how to manipulate that data (with assembly language).
A typical assembly language command might look like this:
mov al, 061h
This command tells the processor to move (mov
) the hexadecimal number 061h
into the specific register named al
. Other assembly language commands might tell the processor to add (add
) or subtract (sub
) a value from the number stored in a specific register.
When you use assembly language, you have to tell the processor what data to store in which registers, how to manipulate the data in the registers, and when to remove data out of the registers.
Sound tedious? It is. Although assembly language is far easier to understand and write than machine language, it's still too complicated to use for creating really big computer programs, like word processors or video games.
In the old days, most programs were written in assembly language, but as programs grew larger and more complicated, assembly language proved too cumbersome to write, edit, and modify.
The biggest problem with assembly language is that you need to manipulate the processor’s registers just to do the simplest tasks. If you wanted to add two numbers together, you’d have to tell the processor to store a number in a register, add a second number to the number in the register, and then yank the result out of the register.
Forcing people to know how to manipulate the processor’s registers before they can program a computer is like forcing people to know how their carburetor works before they can drive a car. Ideally, you don’t want to tell the processor how to manipulate data in its registers; you just want the processor to add two numbers without worrying about specific registers. So, to make computer programming even easier, computer scientists have hidden the technical details of manipulating registers by creating high-level languages.
Every processor understands only its own particular assembly language. So an Intel processor won’t understand the assembly language of an Advanced RISC Machine (ARM) processor and vice versa. However, some companies make processors that work identically to other processors. For example, a company called Advanced Micro Devices (AMD) makes processors that work just like Intel processors, so an assembly language program written for an Intel processor also works on an AMD processor.
Hiding the details of a computer with a high-level language
The whole purpose of high-level languages is to make programming more intuitive. So, rather than tell the computer to store the number 2
in register al
, add the number 3
to the number stored in register al
, and then yank out the result from register al
, high-level languages let you tell the computer what to do and not worry about how the computer does it. So, a typical high-level language command might look like this:
Total = 2 + 3
As you can see, high-level languages are much easier to read and understand, even if you know nothing about programming. Where assembly language forces you to tell the processor what to do and how to do it, high-level languages just let you tell the processor what to do.
Early popular high-level languages include Fortran (formerly FORTRAN, short for FORmula TRANslator), BASIC (short for Beginner's All-purpose Symbolic Instruction Code), COBOL (short for COmmon Business Oriented Language), and Pascal (named after the French philosopher Blaise Pascal).
Besides making programming