Chapter 5 (Section 2) Final Revision #1
No description yet. This curated deck is ready for daily study.
Card preview (24)
Why Translation is Needed
Why must programs be translated into machine code before execution?
CPU hardware can only process instructions in machine code (binary), because it is made up of logic gates that only handle 0s and 1s. Therefore, any program written in a high-level or assembly language must be converted into machine code and loaded into main memory before it can run.
Define a translator.
Systems software that converts a source program written in a language other than machine code into a format the computer can execute.
Source Code, Object Code, and Mapping
What is source code?
The original program written by the programmer in a high-level or assembly language, before it has been translated.
What is object code?
The machine code version of a program produced after the source code has been processed by a compiler or assembler.
How does the mapping of instructions differ between low-level and high-level languages?
Low-level language instructions generally have a one-to-one mapping with machine code. One high-level language instruction usually translates into multiple machine code instructions.
What does it mean for a program to be machine independent?
The program code can be translated to run on any processor / platform — it is not tied to a specific hardware architecture.
Types of Translators
What is an assembler?
A program that translates a source program written in a low-level assembly language into machine code (binary).
What is a compiler?
A program that translates a source program written in a high-level language into machine code or p-code (object code) as a single, complete unit, before the program is run.
What is an interpreter?
A program that analyses and executes a high-level language program one line at a time, without producing a separate object code file.
Bytecode and Partial Compilation
What is bytecode?
Partially compiled code generated by a compiler. It can only be interpreted on a virtual machine, and it is machine independent.
Describe how Java source code is translated (two-step process).
• Java uses a two-step (partial-compilation) translation process. • Java code is first translated into intermediate code (bytecode) using the Java compiler. • The bytecode is then interpreted by the Java Virtual Machine (JVM) at run-time.
Why are some high-level languages partially compiled and partially interpreted?
• Partially compiled programs can be used on different platforms, as they are interpreted when run. • Code is optimised for the CPU, as machine code is generated at run-time. • The source code does not need recompiling, making it more efficient to run.
Compiler vs Interpreter — Performance
How does execution speed compare between compiled and interpreted programs?
Compiled programs run faster, because translation is performed once before execution. Interpreted programs run slower, because each line must be translated every time the program is executed.
How do memory requirements differ between compiled and interpreted programs?
A compiled program only requires the object code to be in memory. An interpreted program requires both the source code AND the interpreter software to be present in memory during execution.
Which translator is more efficient for programs that use loops, and why?
A compiler — it translates the loop code only once. An interpreter must re-translate every instruction inside the loop on every iteration.
Compiler vs Interpreter — Development and Distribution
Why is an interpreter preferred during the initial development phase?
• Use an interpreter while writing the program — to test/debug the partially completed program. • Because errors can be corrected and processing can continue from where execution stopped // errors can be corrected in real time // errors are identified one at a time.
How do compilers and interpreters handle error reporting differently?
A compiler scans the entire program and provides a complete list of all syntax errors at the end of compilation. An interpreter stops execution the moment it encounters a single error.
Why is a compiler better for protecting Intellectual Property (IP)?
• Program can be distributed without source code — so it cannot be edited/stolen/plagiarised. • Users do not require the translator to run the program — so time is not spent retranslating.