9.2 Translation Process

Recall from Chapter 6 that a program written in assembly language is input to the assembler, which translates the assembly-language instructions into machine code. The machine code, which is the output from the assembler, is then executed. With high-level languages, we employ other software tools to help with the translation process. Let’s look at the basic function of these tools before examining high-level languages.

Compilers

The algorithms that translate assembly-language instructions into machine code are very simple because assembly languages are very simple. By “simple,” we mean that each instruction carries out a fundamental operation. High-level languages provide a richer set of instructions that makes the programmer’s life even easier, but because the constructs are more abstract, the translation process is more difficult. Programs that translate programs written in a high-level language are called compilers. In the early days of computer programming, the output of a compiler was an assembly-language version of the program, which then had to be run through an assembler to finally get the machine-language program to execute. As computer scientists began to have a deeper understanding of the translation process, compilers became more sophisticated and the assembly-language phase was often eliminated. See FIGURE 9.2.

A figure represents the processes involved in “Compilation.”

FIGURE 9.2 Compilation process

A program written in a high-level language can run on any computer that has an appropriate compiler for the language. A compiler is a program; therefore, a machine-code version of the compiler must be available for a particular machine to be able to compile a program. Thus, to be used on multiple types of machines, each high-level language must have many compilers for that language.

Interpreters

An interpreter is a program that translates and executes the statements in sequence. Unlike an assembler or compiler that produces machine code as output, which is then executed in a separate step, an interpreter translates a statement and then immediately executes the statement. Interpreters can be viewed as simulators or virtual machines that understand the language in which a program is written. As Terry Pratt points out in his classic text on programming languages, both a translator and a simulator accept programs in a high-level language as input. The translator (assembler or compiler) simply produces an equivalent program in the appropriate machine language, which must then be run. The simulator executes the input program directly.4

Second-generation high-level languages came in two varieties: those that were compiled and those that were interpreted. FORTRAN, COBOL, and ALGOL were compiled; Lisp, SNOBOL4, and APL were interpreted. Because of the complexity of the software interpreters, programs in interpreted languages usually ran much more slowly than compiled programs. As a result, the trend was toward compiled languages—until the advent of Java.

Java was introduced in 1996 and took the computing community by storm. In the design of Java, portability was of primary importance. To achieve optimal portability, Java is compiled into a standard machine language called Bytecode. But how can there be a standard machine language? A software interpreter called the JVM (Java Virtual Machine) takes the Bytecode program and executes it. That is, Bytecode is not the machine language for any particular hardware processor. Any machine that has a JVM can run the compiled Java program.

The portability achieved by standardized high-level languages is not the same as the portability achieved by translating Java into Bytecode and then interpreting it on a JVM. A program written in a high-level language can be compiled and run on any machine that has the appropriate compiler; the program is translated into machine code that is directly executed by a computer. A Java program is compiled into Bytecode, and the compiled Bytecode program can run on any machine that has a JVM interpreter. That is, the output from the Java compiler is interpreted, not directly executed. (See FIGURE 9.3.) Java is always translated into Byte-code. In addition, there are compilers for other languages that translate the language into Bytecode rather than machine code. For example, there are versions of Ada compilers that translate Ada into Bytecode.

A figure explains the benefits of “Bytecode interpretation.”

FIGURE 9.3 Portability improvement because of Bytecode interpretation

The JVM is a virtual machine, just like the Pep/9 system discussed in Chapter 6. In that chapter, we defined a virtual machine as a hypothetical machine designed to illustrate important features of a real machine. The JVM is a hypothetical machine designed to execute Bytecode.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.149.242