Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать
proc
mov ax,@data mov ds,ax mov ah,9
mov dx,offset my_message

20

Part I: Programming a Computer

Essentially, computers really understand only one language, which consists of zeroes and ones, also known as machine language. A typical program that you could write in machine language might look something like the following example:

0010 1010 0001 1101

0011 1100 1010 1111

0101 0110 1101 0101

1101 1111 0010 1001

Machine language has the following two major drawbacks:

You can easily type a 0 or 1 by mistake, thereby preventing you from giving the computer the correct instructions.

Machine language takes a long time to write (and an even longer time to understand what the language is actually telling the computer to do).

Because of these two huge problems, few people write programs in machine language. To make writing a program easier, programmers quickly invented a simpler programming language known as assembly language.

The joy of assembly language

The whole purpose of assembly language is to enable you to write programs faster and easier than using machine language. So rather than force programmers to write cryptic programs using 0s and 1s, assembly language uses short, easy-to-remember (to programmers, that is) phrases such as JMP, MOV, and ADD, which represent specific machine-language instructions.

Not only does this convention make assembly language source code shorter and easier to write, but it also makes the code easier to read and modify later. A typical assembly language program looks like the following example:

title Nap Program

; This program displays “Take a nap!” on the screen dosseg

.model small

.stack 100h

.data

my_message db ‘Take a nap!’,0dh,0ah,’$’

.code main

Chapter 2: All about Programming Languages

21

int 21h

mov ax,4C00h int 21h main endp

end main

Making programs easy to read and modify is crucial because most programs never work right the first time you use them. And if you want to add new features to a program later, you need to understand how the current program works so that you know how to modify it.

Programmers created assembly language for their convenience only. The computer itself has no idea how to read or use any instructions written in assembly language.

Because computers can’t read assembly language instructions, programmers created special programs that translate assembly language into machine language. These special programs are known as assemblers. If you give your computer a program written in assembly language without an assembler, your computer won’t have the slightest idea how to read assembly language.

So after you write a program in assembly language, you have to feed it to an assembler, which translates your assembly language program into machine code, which your computer can understand.

Assembly language offers the following two distinct advantages over machine language:

Assembly language programs are easier to read than machine language programs.

Assembly language programs are easier to write (and modify) than machine language programs.

Of course, assembly language has the following disadvantages:

Programs that you create by using assembly language run slower and gobble up more space (both physical disk space and memory) than equivalent programs that you may create with machine language.

You can’t easily transfer (or, to use programming lingo, port) a program that you write in assembly language for one computer to another computer.

Writing a program in assembly language can prove extremely tedious, time-consuming, and complicated. That’s why few people bother to write large programs in assembly language.

22

Part I: Programming a Computer

In general, the easier the programming language is to read and write, the slower and larger are the programs it creates. The Holy Grail of computer programming is to create programs that are easy to write, run as fast as possible, and take up as little space as possible.

C: The portable assembler

To combat the drawbacks of assembly language, programmers created a wide variety of different programming languages with names such as COBOL and FORTRAN. (See the following section, “High-level programming languages,” to find out more about the advantages and disadvantages of these types of programming languages.)

But some programmers felt that they needed a language that offers the power to access hardware (as does assembly language) but is easier to read, write, and modify (as are COBOL and FORTRAN). Eventually, they invented a programming language known simply as C.

Programmers based the C programming language on an early programming language by the name of B (although no programming language known as A ever existed).

Programmers wanted to make programming as easy as possible for themselves, so they made the C programming language look more like actual words that people can understand, as the following example demonstrates:

main()

{

printf (“Take a nap!\n”);

}

This C program is equivalent to the assembly language program found in the preceding section of this chapter that displays “Take a nap!” on-screen. Comparing the two, you can see that the C language source code is smaller and easier to read than the equivalent assembly language source code.

By using assembly language, programmers sacrifice readability for speed and size. A program that you write in C runs slower and creates larger program files than does an equivalent assembly language program. That’s because assembly language is closer to the native language of computers (which is machine code) than C. So C programs need to first get translated into assembly language code before finally being converted into machine language code. This two-step process tends to be less efficient than writing an equivalent assembly language program. C source code, however, is much easier to read, write, and modify than assembly language source code (and far easier to read, write, and modify than an equivalent machine-language source code).

Chapter 2: All about Programming Languages

23

The programmers who created the C programming language had the following three main goals:

To create a language that’s easier to read and write than assembly language.

To offer programmers the capability to access all the parts of the computer just as they can by using assembly language.

To provide a small, simple language that you can easily port from one computer to another. Programs that you write in C can run on different computers without massive rewriting, which is the main drawback with assemblyand machine-language programs.

This third goal may look strange, so here’s the rationale behind it: Computers don’t understand C any better than they understand assembly language. (Computers are notorious for not understanding much of anything, which is why programming must be so precise.) If you write an entire program using C, your computer doesn’t have the slightest clue how to read your instructions.

To make a computer read and understand instructions written in C, you must convert your C program into equivalent machine-language instructions. Programmers created special programs, known as compilers, to do this conversion for them. A compiler takes your C program and converts it into machine language, which is like translating a Jules Verne novel from French into English.

As is true of translations between human languages, the simpler the language, the easier is the translation. Translating a children’s book from French into Japanese is much easier than translating a mathematics dissertation from French into Japanese, mainly because a children’s book uses simple words, while a mathematics dissertation uses more complicated words. Similarly, translating C into machine language code is more difficult than translating assembly language into machine language code.

So the only way that you can run a C program on another computer is if someone’s already written a C compiler for that other computer. Because C is a simple language, writing C compilers for different computers is relatively easy, especially if you compare it with the same task for other programming languages, such as Ada or LISP.

Because C compilers are fairly easy to write, you can find C compilers for almost every computer in the world. Theoretically, you can write a C program for the Macintosh, copy it to a computer running Windows XP, recompile it, and run the program with little or no modification.