Mastering Translators and Compilers

Slides:



Advertisements
Similar presentations
Assembly Language – 1.
Advertisements

GCSE Computing Lesson 5.
compilers and interpreters
The Assembly Language Level
SYSTEM PROGRAMMING & SYSTEM ADMINISTRATION
Programming Types of Testing.
Introduction to a Programming Environment
CS 104 Introduction to Computer Science and Graphics Problems Software and Programming Language (2) Programming Languages 09/26/2008 Yang Song (Prepared.
CEN 226: Computer Organization & Assembly Language :CSC 225 (Lec#1) By Dr. Syed Noman.
Compiled by Benjamin Muganzi 3.2 Functions and Purposes of Translators Computing 9691 Paper 3 1.
Activity 1 - WBs 5 mins Go online and spend a moment trying to find out the difference between: HIGH LEVEL programming languages and LOW LEVEL programming.
1 Chapter-01 Introduction to Computers and C++ Programming.
CHAPTER 4: INTRODUCTION TO COMPUTER ORGANIZATION AND PROGRAMMING DESIGN Lec. Ghader Kurdi.
Systems Software & Operating systems
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
Madiha Liaqat Lecturer, UET, Taxila Programming Fundamentals.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
1 History of compiler development 1953 IBM develops the 701 EDPM (Electronic Data Processing Machine), the first general purpose computer, built as a “defense.
Just as there are many human languages, there are many computer programming languages that can be used to develop software. Some are named after people,
Robert Crawford, MBA West Middle School.  Explain how the binary system is used by computers.  Describe how software is written and translated  Summarize.
Introduction to Computer Application (IC) MH Room 517 Time : 7:00-9:30pm.
Compilers and Interpreters. HARDWARE Machine LanguageAssembly Language High Level Language C++ Visual Basic JAVA Humans.
The Functions and Purposes of Translators Translators, Interpreters and Compilers - High Level Languages.
Compilers and Interpreters
The Functions and Purposes of Translators Translators, Interpreters and Compilers - High Level Languages.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
Introduction to computer software. Programming the computer Program, is a sequence of instructions, written to perform a specified task on a computer.
Evolution and History of Programming Languages
Computer Basics.
Computer Systems Nat 5 Computing Science
CHAPTER NINE.
Why don’t programmers have to program in machine code?
Lecture 1b- Introduction
Advanced Computer Systems
Component 1.6.
Visit for more Learning Resources
High or Low Level Programming Language? Justify your decision.
High and low level languages
Introduction to programming
Operating System Interface between a user and the computer hardware
Programming Language Hierarchy, Phases of a Java Program
CSCI-235 Micro-Computer Applications
F453 Computing Questions and Answers
COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE
Computer Systems Nat 5 Computing Science
Microprocessor and Assembly Language
A451 Theory – 7 Programming 7A, B - Algorithms.
History of compiler development
Application Development Theory
Chapter 4 Computer Software.
Teaching Computing to GCSE
TRANSLATORS AND IDEs Key Revision Points.
Teaching Computing to GCSE
Translators & Facilities of Languages
Compiler Construction
Lesson 2 Programming constructs – Algorithms – Scratch – Variables Intro.
Lesson Objectives Aims Key Words Compiler, interpreter, assembler
CMP 131 Introduction to Computer Programming
Chapter One: An Introduction to Programming and Visual Basic
Introduction to Computer Programming
Mastering Memory Modes
PROGRAMMING FUNDAMENTALS Lecture # 03. Programming Language A Programming language used to write computer programs. Its mean of communication between.
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Dept. of Computer & Information Sciences (Course Introduction)
1.3.7 High- and low-level languages and their translators
WJEC GCSE Computer Science
Running & Testing Programs :: Translators
Presentation transcript:

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

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

into So how do we convert: Source code Machine code? Enter: Translators

There are three types of Translators Assemblers (Low level – Assembly Language) Compilers (High level Languages) Interpreters (High Level Languages)

All of these “translators” are …. Simply responsible for converting SOURCE CODE into OBJECT OR MACHINE CODE Here they are again: INTERPRETERS COMPILERS ASSEMBLERS

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

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.”

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 0001001000100100 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

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

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

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

There are three types of Translators Assemblers (Low level – Assembly Language) Compilers (High level Languages) Interpreters (High Level Languages) THIS ONE NEXT

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.

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

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

Taken from www.wikipedia.com

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 1957. 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: http://en.wikipedia.org/wiki/UNIVAC_I and http://en.wikipedia.org/wiki/Manchester_Mark_1 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.

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.

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!

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

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

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

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

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.

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.

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

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

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

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

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

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

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: http://www.tutorialspoint.com/compiler_design/compiler_design_intermediate_code_generations.htm 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

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.

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.

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

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