You are ChatGPT, a large language model trained by OpenAI.
- User
System: You are ChatGPT, a large language model trained by OpenAI. User: I will send you a body of text, I will then state what you do next. User: 238 29 Classification of programming languages and translationINTRODUCTION Most programming is now done using high-level programming languages. In this chapter we will look at how programming languages have developed over time, in particular looking at the main types of programming languages and the different methods (or paradigms) of programming. Linked with this is the way in which the code that you write as a programmer is converted into 0s and 1s so that it can be understood and processed by the processor.SPECIFICATION COVERAGE 3.6.2 Classifi cation of programming languages3.6.3 Types of translator LEARNING OBJECTIVES In this chapter you will learn: •how programming can be performed in machine code, assemblylanguage and high-level languages •that there are different methods of programming known asparadigms •how interpreters and compilers are used to turn programming codeintomachine code (0s and 1s) •what bytecode is. ●Types of programming languagesAlthough computers as we know them have only been around since the mid-1940s they have changed beyond all recognition in that short timespan. Programming the early computers was a very tedious business asprograms were entered as a sequence of switch settings. Each switch waseither on or off so by combining enough switches the programmer couldrepresent different things.TYPES OF PROGRAMMING LANGUAGES239Thankfully, programming computers has become a much simpler process, though the ever-increasing complexity of the computers themselves brings its own problems.As computers have developed then so have the programming languages that can be used on them. There are three main types of languages: ●Machine code●Assembly language ●High-level languageMachine code The processor of a computer can only work with binary digits (bits). This means that all the instructions given to a computer must, ultimately, be a pattern of 0s and 1s. Programs written in this format are said to be written in machine code . Having to write everything down as a sequence of 0s and 1s means t hat the programs are going to be very long-winded, and because everything is entered as bits there is a high risk of making a mistake. One way to make these bits easier to understand is to show a sequence of bits as either a decimal or hexadecimal number.To compound the problem further, it is also going to be very difficult to track down any errors or bugs that exist in the code. One final problem is that because machine code is written for a specific processor, it is unlikely to be very portable. This means that it will only work on a computer with the same type of processor as it was written for. Nevertheless because you are writing instructions that can be used directly by the processor, they will be executed very quickly and the processor will do exactly what you tell it to do. Assembly languageThe need to make programs more programmer-friendly led to the development of assembly languages . Rather than using sequences of 0s and 1 s, an assembly language allows programmers to write code using words. There is a very strong connection between machine code and assembly language. This is because assembly language is basically machine code with words. The number of words that can be used in an assembly language is generally small. Each of these commands translates directly into one command in machine code. This is called a one-to-one relationship. Most, but not all, assembly codes use a system of abbreviated words called mnemonics . In the small section of assembly code below, the mnemonic LDR stands for Load Register and STR means Store Register . ADD and SUB should be rather more obvious. In this simplified example all the numbers following each mnemonic refer to memory addresses – the place in memory where numbers are written to or read from. LDR 20ADD 43STR 20 SUB 41STR 45These two are known as low-level languages KEYWORD Machine code: the lowest level of code made up of 0s and 1s. KEYWORDS Assembly language: a way of progr amming that involves writing mnemonics. Mnemonics: short codes that are used as instructions when programming, e.g. LDR, ADD.29 Classification of programming languages and translation 240This code does the following: ●loads the accumulator with the contents of address 20●adds the value held in memory location 43 to the value held in theacc umulator ●stores the contents of the accumulator to address 20●subtracts the value held in memory location 41 from the value held in the accumulator ●stores the contents of the accumulator to address 45.Al though assembly language uses words, some of which you might recognise, the code is not particularly easy to understand. Before any code written in assembly language can be executed it has to be converted into machine code. This is because the processor can only understand binary digits (0s and 1s) – the words are meaningless to the computer, so the words must be converted into these binary digits. This conversion process is carried out by an assembler. The assembler will be able to identify some, but not all, of the errors that are likely to be hidden somewhere in the code. The code that the programmer creates is known as the source code . The assembler takes this source code and translates it into a machine code version that is known as the object code . As with machine code, assembly languages are based on one processor so they are generally not very portable. Assembly language is still used in certain situations today as it has a number of advantages over high-level languages: ●Programs are executed quickly as a compiler does not optimise thema chine code that it produces as effectively as a programmer who codesin an assembly language that maps directly to machine code. ●Program code is relatively compact for the same reason. ●Assembly language allows direct manipulation of the registers on thepr ocessor, giving high levels of control. Its main uses are where direct manipulation of hardware is required, for example in embedded systems where a low level of interaction is required with hardware. The processors in these systems may be relatively slow and have limited memory in which case the efficiency of an assembly language is needed. This may mean that very few commands are needed to operate the device. Therefore, these instructions can be programmed directly to the processor using an assembly language. Real-time applications may also use assembly language as they need to respond very quickly to inputs. This is particularly relevant where it is an embedded system. Assembly language does have other uses where only low-level coding is required. For example, some device drivers are written in assembly language, particularly for customised hardware and software. KEYWORDSource code: programming code that has not yet been compiled into an executable file. Assembler: a program that transl ates a program written in assembly language into machine code. Object code: compiled code that can be run as an executable on any computer.TYPES OF PROGRAMMING LANGUAGES 241High-level languagesMachine code and assembly languages are known collectively as low-level languages . The increasingly complex demands made on computers meant that writing programs in assembly code was too slow and cumbersome. High-level languages were developed to overcome this problem. Whereas low-level languages are machine-oriented, high-level languages are problem-oriented. This means that the commands and the way the program is structured are based on what the program will have to do rather than the components of the computer it will be used with. This means that a program written in a high-level language will be portable. It can be written on one type of computer and then executed on other types of computer. There are many different high-level languages available, each being written to cope with the demands of a specific type of problem. For example, some high-level languages are specifically designed for scientific applications, others for manipulating databases, whilst others are used to create web pages. The language the programmer chooses will depend to a large extent on the nature of the problem they are trying to solve. High-level languages are often classified into three groups based on the programming paradigm we saw in Chapter 5. A paradigm is a concept for the way something works: ●Imperative languages: Al so known as procedural languages, these workby typing in lists of instructions (known as subroutines or procedures) that the computer has to follow. Every time the program is run, it followsthe same set of instructions. ●Object-oriented languages: Th ese work by creating objects where theinstr uctions and data required to run the program are contained withina single object. Objects can be further grouped into classes. ●Declarative languages: Th ese work by describing what the program should accomplish rather than how it should accomplish it. One typeof declarative language is logic programming, which is used widely inthe fields of artificial intelligence and works by programming in factsand rules, rather than instructions. The program then uses these factsand rules to interrogate the data and provide results. Another type ofdeclarative language is a functional language , which works by treating pro cedures more like mathematical functions. The building blocks of theprogram therefore are functions rather than lists of instructions. The main characteristics of a high-level language are: ●It is easier for a programmer to identify what a command does as thekeywo rds are more like natural language. ●Like assembly languages, high-level languages need to be translated. ●Unlike assembly code, one command in a high-level language might berepr esented by a whole sequence of machine code instructions. This is called a one-to-many relationship. ●They are portable. ●They make use of a wide variety of program structures to make thepro cess of program writing more straightforward. As a result they arealso easier to maintain.KEYWORDS Imperative language: a lang uage based on giving the computer commands or procedures to follow. Object-oriented language: a progr amming paradigm that encapsulates instructions and data together into objects. Declarative languages: langua ges that declare of specify what properties a result should have, e.g. results will be based on functions. Functional language: a progr amming paradigm that uses mathematical functions.KEYWORDS Low-level language: machine code and as sembly language. High-level language: a prog ramming language that allows programs to be written using English keywords and that is platform independent.29 Classification of programming languages and translation242●Translating high-level languages One of the main features of high-level languages is that they are programmer-friendly. Unfortunately this means the computer will not understand any ofthe high-level language source code, so it will have to be translated in someway. This process is called translation and in order to carry it out a specialpiece of systems software called a translator is needed. The asembler is the translator used for low-level languages and there are two types of translator for high-level languages: an interpreter and acompiler. InterpreterAn interpreter works by reading a statement of the source code and immediately performing the required action. It may do this by interpreting the syntax of each statement, or by calling predefined routines. In somecases, the source code is translated into an intermediate format before itis executed. Interpreters may examine every statement, which can reduceefficiency. For example, in an iterative statement, the interpreter will readthe same statement once for each iteration. Typically an interpreter only converts what needs to be translated. For example when the line of code: If Age<17 Then Output = "Cannot drive a car"needs to be translated, only the first section of the code If Age<17 Thenis translated and executed. The second part of the code is only translated if the condition is true. This saves time as only the code that is needed isconverted. Some interpreters translate an entire line of code before theyexecute it. These are known as line interpreters. Benefits of using an interpreter: ●You do not need to compile the whole program in order to run sectionsof code. You can execute the code one statement at a time. ●As the code is translated each time it is executed, program code can be run on processors with different instruction sets. ●Because of this, an interpreter is most likely to be used whilst a programis be ing developed. Drawbacks of using an interpreter: ●No matter how many times a section of code is revisited in a program itwill need translating every time. This means that the overall time needed to execute a program can be very long. ●The source code can only be translated and therefore executed on a computer that has the same interpreter installed. ●The source code must be distributed to users, whereas with a compiledpro gram, only the executable code is needed. CompilerA compiler converts the whole source code into object code before the program can be executed. The good thing about this is that once you have carried out this process you will have some object code that can be executed immediately every time, so the execution time will be quick. This is ideal once you have sorted out all the bugs in your program.KEYWORD Compiler: a program that translates a high-level language into machine code by translating all of the code.KEYWORDSTranslator: the general name for any program that translates code from one language to another, for example translating source code into machine code. Interpreter: a program for translating a high-level language by reading each statement in the source code and immediately performing the action.BYTECODE243Benefits of using a compiler: ●Once the source code has been compiled you no longer need thecomp iler or the source code. ●If you want to pass your object code on to someone else to use they willfind it difficult to work out what the original source code was. This process of working out what the source code was is known as reverse engineering. Drawbacks of using a compiler: ●Because the whole program has to be converted from source code toobj ect code every time you make even the slightest alteration to yourcode, it can take a long time to debug. ●The object code will only run on a computer that has the sameplat form. ●BytecodeSome programming languages use bytecode , which is an instruction set that can be executed using a virtual machine. The virtual machine can emulate the architecture of a computer, meaning that the source codewritten using bytecode can be translated into a format that can be executedon any platform. For example, Java bytecode is compiled using either one or two bytes todefine the instruction and then any number of bytes to pass the parameters.This code can then be executed on any computer that is running the JavaVirtual Machine regardless of what type of processor or operating system isbeing used. Microsoft Common Intermediate Language (CIL) works on a similar basiswhere, as the name suggests, rather than translating source code intomachine code that is specific to a platform, the source code is translatedinto an intermediate code. This intermediate code can then be executed bythe virtual machine. Practice questions can be found at the end of the section on page 264. KEYWORDBytecode: an instruction set usedfor programming that can be executed on any computer using a virtual machine.29 Classification of programming languages and translation 244TASKS1 Under what circumstances would you compile a high-level computer program?2 Explain the benefits of using a high-level language compared to an assembly language.3 Explain the differences between a compiler and an interpreter. 4 Why are machine code and assembly languages said to be machine-oriented?5 Explain why there are so many different high-level languages. 6 Explain why some programmers still write programs in an assembly language.7 Explain why assembly language and machine code are said to have a ‘one-to-one’ relationship.8 Why must assembly language programs be assembled before they can be executed?STUDY / RESEARCH TASKS1 What is meant by the term artificial intelligence and what type of programming language might you use if working in this area?2 What features would you expect to find in a programming language that was designed to work in a control environment?3 There are dozens of programming languages available. Select one particular language, e.g. Java, C#, Python or Visual Basic, and explain why the language was originally developed.KEY POINTS•There are three main typesof pro gramming languages: machine code, assemblylanguage and high-levellanguages. •Machine code and assemblylanguage are known as low- level languages. •Machine code uses 0s and 1s. •Assembly languages usemnemonics. •High-level languages usenatural language keywords. •Assembly language needs tobe co nverted to machine codeusing an assembler. •High-level languages needto be converted to machine code using an interpreter or acompiler. •There are three mainprogramming paradigms: imperative (procedural),declarative and object-oriented. •Bytecode is an instructionset that can be implemented using a virtual machineand is therefore platform-independent. User: Make the following text more clear. Format it properly. Do NOT change the meaning of the words or methods used to obtain the answer or anything like that. This is for AQA A(S)-Level Computer Science. I repeat, do not alter the words as it could have an effect on the marks acquired from the marking scheme. However, remove any redundant information or USELESS examples that are not needed for a student to know, or simplify unnecessary information (if it has a definition or keywords, it is not unnecessary). If you can, use different values for the examples. For things that will not score a mark or are generally useless, for example, "we will cover" or a fun fact, change the wording. This is for AS Level, do not include information about topics that are ONLY A-Level Content (if it says it is) Link the 'sections' of your answer to the following spec-points (by title): e.g. " (Spec Points: a.b.c. Whatever) 3.6.2 Classification of programming langs Content Additional information Show awareness of the development of types of programming languages and their classification into low-and high-level languages. Know that low-level languages are considered to be: machine-code assembly language. Know that high-level languages include imperative high level-language. Describe machine-code language and assembly language. Understand the advantages and disadvantages of machine-code and assembly language programming compared with high-level language programming. Explain the term ‘imperative high-level language’ and its relationship to low-level languages.
- Assistant