Back

More on translators

The need for translators
Whether the source code for a program is written in a low-level language or a high-level language, it must be translated into the code that the computer can use, the ones and zeros, before the CPU can actually run it. The source code is passed to a special translating program that then converts it into ‘object code’. The object code is usually in the 'ones and zeros' form of the computer and for this reason, object code is used interchangeably with the terms machine code or executable code or even executable machine code! This is the way most languages work although just to confuse things, some languages such as Java do not produce machine code upon translation but produce an ‘intermediate code’ that is then converted into machine code when executed. This will be discussed in more detail in the chapter to do with object oriented languages. Some languages such as C take the whole source code and translate it in one go using a compiler. The object code then runs very quickly. Other languages such as BASIC take one line of the source code at a time. It translates that one line using an interpreter and then runs that one line. Then it gets the next line and repeats the process. Interpreted code runs much slower than compiled code but it is very useful for writing, developing and debugging programs because the program will run correctly up to any error in the program. It will stop if it finds an error. The programmer can then examine the code at that point and re-run it, without the need for re-compilation. This is very useful and a much simpler process than for debugging compiled languages.

Why companies usually distribute object code and not source code
When you buy an application or a game, for example, you are usually buying just the object code, not the source code.

    • If you were sold the source code, you would need to ensure that you had the correct translator program so that you could convert your application into something the computer could understand, into object code! Suppose you didn’t have the correct translator? You wouldn’t be able to run the program! If you received the object code when you bought an application or game, you could simply execute it. You wouldn’t need to translate it.
    • You also you wouldn’t need to use up so much of your valuable RAM. This is because you wouldn’t need RAM to store the source code, or the translator program, or any temporary storage whilst the translation was being done. You would only need the RAM to store the object code.
    • If you do only have the object code in your possession, however, you would not be able to modify it in any way. It is very difficult to take some object code and reverse engineer it back into source code.
    • The source code itself would be jealously guarded by the company who wrote the application. They would want to protect their copyright. (Writing software is a very expensive business). If they needed to make any changes or updates to the application, they would be made to the source code as required. After any modifications, the source code would need to be translated again and the updated object code made available to users, perhaps via the web.

Different types of translators
There are three different types of translating program. Which one you would use depends upon the actual language you are writing the source code in. The three types of translator are:

    • Compilers
    • Interpreters
    • Assemblers

We can summarise the use of the three types of translator as follows:

    • If you wrote a program in a low-level language called Assembly then you would translate the source code into object code with an assembler.
    • If you wrote a program with certain high level languages such as Pascal, C or COBOL, you would translate the source code into object code using a compiler.
    • If you wrote a program with certain high level languages such as BASIC or Perl, you would translate the source code into object code using an interpreter.
    • If you wrote a program with certain high level languages such as VB or JAVA you would translate the source code into an intermediate code using a compiler. The intermediate code would then be run with an interpreter.

We can summarise what happens with the following diagram.

translatorsBack