Why does code written in anything other than machine code need to be translated before it can be run?
Because processors can only “understand” binary.
What would be used to translate assembly language into machine code?
An assembler.
What problems could arise when running assembly language on a machine other than the one it was written on?
The assemblers used by the two machines may be different (have a different instruction set) and could therefore translate the instructions incorrectly.
For example, one processor may represent LDA as 0000 whereas another may represent it as 1000 causing the translated code to be different.
What is an assembler?
A program that converts a program in assembly language (source code) into machine code (object code).
What is source code?
The input for a translator, written in assembly language or a high-level programming language.
What is object code?
The output of a translator, written in machine code.
What is a compiler?
A compiler is a program that translates a high-level language such as Java or C# into machine code. The code written by the programmer, the source code, is input as data to the compiler, which scans through it several times, each time performing different checks and building up tables of information needed to produce the final object code. Different hardware platforms will require different compilers, since the resulting object code will be hardware-specific. For example, Windows and Intel microprocessors comprise one platform, Apple and PowerPC processors another, so separate compilers are required for each. The object code can then be saved and run whenever needed without the presence of the compiler, making it faster to launch.
What is an interpreter?
An interpreter is a program that translates a high-level language such as Python into machine code. Once a programmer has written and saved a program ad instructs the computer to run it, the interpreter looks at each line of the source program, analyses it and (if it contains no syntax errors) translates it into machine code and runs it. The interpreter may scan through the entire code for certain basic errors such as a missing bracket before running any of the code. This is the case in many languages such as Python.
What name is given to code produced by a combination of compiling and interpreting?
Bytecode.
What is used to run bytecode?
A bytecode interpreter.
What are the main advantages of using byte code?
Give four advantages of using a compiler over an interpreter.
Give two advantages of using an interpreter over a compiler.
Give one disadvantage of using an interpreter over a compiler.
The program may run slower than a compiled program, because each statement has to be translated into machine code each time it is encountered. So if a loop of ten statements is performed twenty times, all ten statements are interpreted twenty times.