Download presentation
Presentation is loading. Please wait.
1
Mastering Translators and Compilers
WHY DO WE NEED TRANSLATORS? CONVERTING SOURCE CODE TO OBJECT CODE THE RELATIONSHIP BETWEEN ASSEMBLY LANGUAGE AND MACHINE CODE THE USE OF AN ASSEMBLER IN PRODUCING MACHINE CODE DEFINITION OF COMPILATION FEATURES OF A COMPILER THE DIFFERENCE BETWEEN INTERPRETATION AND COMPILATION WHAT IS THE PURPOSE OF INTERMEDIATE CODE IN A VIRTUAL MACHINE
2
Contrary to what some (non computer scientists!) think ….
Humans write Source code (like a command in Vb.Net or Java) Computers don’t really understand us! Maybe in the future they will (but I doubt it!) Computers are essentially comprised of billions of bits When we “speak” or “code” (in a particular language) it has to be translated into computer-speak. A computer has no idea what is going on unless our source code is converted into Machine Code. (Binary) *also called Object Code Machine code (ones and zeros) The source code (high level language Usually) is converted into… Source code
3
into So how do we convert: Source code Machine code? Enter:
Translators
4
There are three types of Translators
Assemblers (Low level – Assembly Language) Compilers (High level Languages) Interpreters (High Level Languages)
5
All of these “translators” are ….
Simply responsible for converting SOURCE CODE into OBJECT OR MACHINE CODE Here they are again: INTERPRETERS COMPILERS ASSEMBLERS
6
A few facts about Assembly Language
It is a LOW LEVEL programming language It allows for a strong (generally a one to one) correspondence between the language and the architectures machine code instructions. Each assembly language is specific to a particular computer’s architecture (i.e not PORTABLE across multiple architectures) Converted into executable code using an Assembler Uses Mnemonics to represent each low-level machine code instruction
7
History of Assembly Languages
They date to the introduction of the stored program computer All the early computers – ENIAC, the IAS machine, Manchester University’s Mark I, the IBM 701, and Whirlwind – were terribly difficult to program. In general, the first computers were programmed either in machine code, which consisted of binary numbers, or in codes (mnemonics) known as assembly languages, which were composed of letters, numbers, symbols, and short words, such as “add.”
8
How does an Assembler work?
An assembler translates assembly language into machine code. Assembly language consists of mnemonics for machine opcodes so assemblers perform a 1:1 translation from mnemonic to a direct instruction. For example: LDA #4 converts to Conversely, one instruction in a high level language will translate to one or more instructions at machine level. Chip designers know that writing in raw machine code (i.e. Binary) is terribly laborious and far too difficult so assembly language was developed with a much more people friendly mnemonic-code for each machine code instruction. Hexadecimal values were also used to represent the data that was being transferred. Ultimately all these mnemonic codes and hexadecimal values had to be converted back into Machine code. This is done by means of an ASSEMBLER
9
Describe the steps taken by an Assembler?
Answers from: OCR Computing specimen paper 1. Translates a program from assembly language into machine code 2. One assembly language instruction changed into one machine code instruction 3. Reserves storage for instructions and data 4. Replaces mnemonic opcodes by machine codes 5. Replaces symbolic addresses by numeric addresses 6. Creates a symbol table to match labels to addressess
10
Advantages of an Assembler
Very fast in translating assembly language to machine code as 1 to 1 relationship Assembly code is often very efficient (and therefore fast) because it is a low level language Assembly code is fairly easy to understand due to the use of English-like mnemonics
11
Disadvantages of an Assembler
Assembly language is written for a certain instruction set and/or processor Assembly tends to be optimised for the hardware it's designed for, meaning it is often incompatible with different hardware Lots of assembly code is needed to do relatively simple tasks, and complex programs require lots of programming time
12
There are three types of Translators
Assemblers (Low level – Assembly Language) Compilers (High level Languages) Interpreters (High Level Languages) THIS ONE NEXT
13
Compilers The purpose of a compiler is simply to convert SOURCE CODE into MACHINE CODE 1. The compiler will generate some code. 2. This code will be saved as an executable file. 3. Another name for this exe file is: “object” file 4. When the exe file (or object file) is run then the machine code commands that it contains start to be processed by the CPU 5. An example of an executable file is Notepad.exe (in Windows executable files have this extension) 6. Because different CPUs have a different set of machine codes, the compiler will necessarily need to have the target CPU defined before it carry out the translation 7. You could, for instance, purchase C++ commercial compilers and these would target the Intel x 86 family of processors specifically.
14
Hardware Software Both A Compiler is …..
A compiler is a computer program that transforms source code in a high level programming language (the source language), into another computer language (the target language, often having a binary form known as object or machine code - the most common reason for wanting to transform source code is to create an executable program. Software Both
15
Create your own compiler?
Compilers are large and complex programs, but systematic analysis and research by computer scientists has led to a clearer understanding of compiler construction and a large body of theory has been developed around them. Research into compiler construction has led to tools that make it much easier to create compilers, so that today computer science students can create their own small language and develop a simple compiler for it in a few weeks! One of the (or possibly the first ever) compiler was written by Grace Hopper
16
Taken from www.wikipedia.com
17
Compiler History The first compiler was written by Grace Hopper, in 1952, for the A-0 System language. The term compiler was coined by Hopper.[1][2]The A-0 functioned more as a loader or linker than the modern notion of a compiler The FORTRAN team led by John W. Backus at IBM is generally credited as having introduced the first complete compiler, in The first FORTRAN compiler took 18 person-years to create. The first autocode and its compiler were developed by Alick Glennie in 1952 for the Mark 1 computer at the University of Manchester and is considered by some to be the first compiled programming language. Acknowledgement: and The COBOL compiler for the UNIVAC II was probably the first to be written in a high level language, namely FLOW-MATIC, by a team led by Grace Hopper.
18
So what is the point of a compiler? What does it do?
1. A compiler translates source code into machine code 2. A compiler translates the whole program as one complete unit 3. It creates an executable file 4. It is able to report on errors in the code (error checking) 5. It may also report “spurious” errors 6. It does not need to be present to “run” the code. 7. It can optimise source code – so it runs as efficiently as possible 8. Code Portability: A compiler can target different processors using the same source code.
19
Feature of a Compiler: HANDLING ERRORS
People make mistakes! 2. A compiler will handle these mistakes and hopefully tell the programmer where he/she has gone wrong. 3. Each programming language has its own grammar! For instance JavaScript insists on a “;” after most lines 4. Missing out the “;” is something the compiler will spot!
20
Other types of Error Handling
There is another type of Error handling called “type checking” 2. What will happen if you keep pouring the milk on the right? 3. There isn’t enough room/space in the cup! 4. In computing terms this is like trying to stuff into a variable a value that is too large for it 5. var x = Byte x = 10,000,000 In this situation the compiler will notice that x is only assigned a byte and the programmer (source code) appears to be trying to stuff ten million into it! This is called a TYPE CHECK
21
So what happens after the errors have been “handled”?
Compiler will run through the translation process It will report the errors found in the form of a report The programmer will (hopefully) fix all the highlighted errors in source code When the code is error free it will be translated into an executable file
22
Remember the final output of the compiler is the exe file
What’s in the executable file? It basically contains the entire machine code of the software/application. The machine code will run on the CPU and this will happen only once the exe file has been loaded into main memory as a process by the operating system
23
The advantages of compiling (and having an exe file)
It runs very quickly. (compared to interpreting each line of source code – this is hat an interpreter does) The original source code is absent (it doesn’t need to be there). This makes it a convenient way of distributing software as well as protecting copyright Interesting fact: In the software licence conditions of most applications (the EULA that you agree to during installation) you will usually find a clause that makes it a breach of copyright to “reverse engineer” the executable back into source code. See the above point: Making unauthorised changes to the software would be difficult. Machine code is rather hard to understand and so it would not be easy for someone to alter the software if it was distributed as an executable file
24
Code optimisation 1. Makes code as EFFICIENT as possible
When a compiler is used to produce executable code, code generation includes OPTIMISATION. The below points outline what happens during the process of optimisation. 1. Makes code as EFFICIENT as possible 2. Increases the processing speed 3. Number of instructions is reduced. 4. The programmer can chose between speed and size.
25
Recap: Definition of Compiler – and its features
1. A compiler translates source code into machine code 2. A compiler translates the whole program as one complete unit 3. It creates an executable file 4. It is able to report on errors in the code (error checking) 5. It may also report “spurious” errors 6. It does not need to be present to “run” the code. 7. It can optimise source code – so it runs as efficiently as possible 8. Code Portability: A compiler can target different processors using the same source code.
26
What are the differences between a Compiler/Interpreter
From OCR Mark scheme answers Compiler Interpreter Assembler Turn the textboxes in to the relevant colour (e.g. Red for if it is a compiler feature) Uses high level source code Gives a list of errors at the end Produces an executable file Converts the whole program as a unit to an intermediate language Reports errors as and when they are found (not at the end) Uses high level source code Gives a list of errors at the end of compilation Translates and runs one statement at a time (not as a whole unit) Uses low level source code
27
What are the differences between a Compiler/Interpreter
From OCR Mark scheme answers Compiler Interpreter Assembler Turn the textboxes in to the relevant colour (e.g. Red for if it is a compiler feature) Uses high level source code Gives a list of errors at the end Produces an executable file Converts the whole program as a unit to an intermediate language Reports errors as and when they are found (not at the end) Uses high level source code Gives a list of errors at the end of compilation Translates and runs one statement at a time (not as a whole unit) Uses low level source code
28
What are the differences between a Compiler/Interpreter
From OCR Mark scheme answers Compiler Interpreter Assembler Uses high level source code Reports errors as and when they are found (not at the end) Gives a list of errors at the end Converts the whole program as a unit to an intermediate language Translates and runs one statement at a time (not as a whole unit) Uses low level source code Gives a list of errors at the end of compilation Produces an executable file Uses high level source code
29
There are three types of Translators
Assemblers (Low level – Assembly Language) Compilers (High level Languages) Interpreters (High Level Languages) SEE THE POWERPOINT ON THIS TOPIC
30
What is the main purpose of a Translator?
From OCR Mark scheme answers 1. To convert source code into machine or object code 2. To detect errors in source code Some compilers produce intermediate code rather than executable code True
31
What is intermediate code?
From OCR Mark scheme answers 1. Some compilers produce intermediate code rather than executable code 2. Intermediate code: is simplified code / partly translated code 3. Intermediate code can be run on any machine / or virtual machine 4. Portability is improved (as it can run on any machine) 5. Sections of programs can be written in different languages 6. Runs more slowly than executable code
32
Why bother with intermediate code?
1. If a compiler translates the source code into machine code directly then (for each new machine), a full native compiler is needed If you want to read about intermediate code and find it out how it works/is generated and the different types see: Why have intermediate code at all? (example above has been taken and adapted from this site) 2. Intermediate code eliminates the need for a full new compiler for each unique machine (this is because it keeps the analysis portion the same for all compilers. The second synthesis portion is changed according to the target machine) 3. It is easier to apply source code modifications to improve code performance by applying code optimisation techniques on intermediate code
33
Explain why intermediate code may be more useful than executable code?
From OCR Mark scheme answers 1. Can run on a variety of computers 2. Same intermediate code can be obtained from different high level languages. 3. The production of intermediate code (see above point) therefore improves portability.
34
Further advantages of Intermediate code?
1. It is platform independent 2. May be used on a variety of different machines 3. Intermediate code programs have been compiled so will actually be error free.
35
State one disadvantage of using intermediate code?
From OCR Mark scheme answers 1. It is possible that the program will run more SLOWLY 2. The program has to be translated each time it is run 3. The use of intermediate code may require more software Additional software required: 1. Interpreter 2. Virtual Machine
36
Explain the meaning of "virtual machine" and how intermediate code is run on it.
From OCR Computing Specimen paper 1. A virtual machine is a theoretical (or generalised) computer on which the program can run 2. Intermediate code is run using an interpreter 3. The interpreter that is needed for the intermediate code will be specific to the computer
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.