# A-level Computing/OCR/Unit 1.2.4 Types of Programming Language
## Low Level Languages

A low level language is one who's programming statements are geared towards a particular CPU family, such as the x86 family of processors. They are almost, but not quite, machine code. An example of a low level language is Assembly Language. Chip makers, such as Intel or ARM, provide programmers with an Assembly Language with which to code their particular CPU. The language is CPU specific and makes direct use of its internal registers. Mnemonics are used as programming code, such as "MOV" or "ADD" and make use of opcodes and operands. An opcode indicates the action the CPU must take, while the operand hold the address or the data to be operated on. There are many different memory modes that can be used, and labels are used as reference points, to allow the code to jump from one part to another.
Low level languages are excellent for close control of the CPU, for example many device drivers are written in a low level language. Their programs can be very efficient if well optimised and written. They can however be difficult to use as the programming commands can be quite obscure. A good assembly programmer must know a lot about the internal structure of the CPU, about its specific registers and memory management methods. Low level languages also produce the least portable source code.
Lain wants to build a computer game. She decides on using assembly as it would offer the best performance. Give an explanation as to why she should consider using a higher level language [2]
Answer:
High level languages are easier to code than low level languages (1 mark) They are closer to English and the same output can be achieved in fewer lines of code (1 mark)
Accept any valid explanations on the advantages of high level languages relating to the context(processor specific, performance inst needed, complex problem)

### Little Man Computing

Little Man Computer (LMC) is a ‘conceptual’ instruction set which consists of just eleven instructions. While this may seem simplistic, it captures many important features of a real CPU.
* INP - Input data. The input is loaded into the Accumulator, overwriting any value that was already there.
* ADD - Add data. Add the value found in the specified memory location to the value in the accumulator.
* SUB - Subtract data. Subtract the value found in the specified memory location from the value in the accumulator.
* STA - Store data. Stores the contents of the Accumulator into the specified memory location.
* LDA - Load data. Loads the Accumulator with the contents of the specified memory location overwriting any value that was already there.
* BRA - Branch always. Branch to the given labelled instruction.
* BRZ - Branch if zero. If the value in the accumulator is 0, branch to the given labelled instruction.
* BRP - Branch if positive or zero. If the value in the accumulator is zero or positive, branch to the given labelled instruction.
* OUT - Output data. Outputs the contents of the accumulator to the Output Box
* HLT - Halt. Ends the program.
* DAT - Data. Note that ‘DAT’ is not an instruction per se but rather a directive to the assembler to reserve space for a variable. This is why it does not have an opcode.
A program has been written using the Little Man Computer instruction set (Fig 1)
Explain the purpose of instructions 018 to 022 in the context of the Little Man Computer.
(3 marks)
The program uses immediate direct addressing. Using an example from the program, describe what is meant by direct addressing.
(2marks)
(Fig 1)
001 INP
002 STA total
003 INP
004 STA toadd
005 addagain LDA total
006 ADD toadd
007 STA total
008 LDA count
009 ADD one
010 STA count
011 LDA total
012 SUB tofind
013 BRP output
014 BRA addagain
015 output LDA count
016 OUT
017 HLT
018 toadd dat 0
019 count dat 0
020 total dat 0
021 one dat 1
022 tofind dat 10
Answer:
1 mark for each of the following points:
•To tell the assembler that two memory locations are required.
•To give an identifier to the memory locations.
•To initialise the memory locations/give starting values
1 mark for each of the following points:
•The operand stores the memory location of the data.
* e.g. ADD toadd — ‘toadd’ points to the memory location where the data are stored.

## Procedural Languages

A procedural language operates in a step-by-step manner; specific instructions are coded for the computer to carry out in a "do this, then this, followed by this" format. It is one of the most common programming paradigms in use. They are an example of 3rd generation languages, which make writing code easier by allowing people to easily read and understand it. This is achieved by naming variables and using functions/subroutines. Examples of procedural languages include FORTRAN, COBOL, Pascal and C.
Procedural languages are excellent for general purpose programming, and there are many resources available on well tried and tested algorithms. They offer a good level of control without knowing the intricacies of the CPU, and provide source code which is portable. However, for a programmer to get work they must often specialise in a language - the requirements of a C programmer will be different to those of a COBOL specialist. To get a good quality program, the programmer must be very precise and knowledgeable about the programming instructions required, so a fully debugged and working program takes longer to construct than a 4th generation language. They are not as efficient as low level source code, and are poor at handling fuzzy conditions, which can be found in areas such as AI (Artificial Intelligence).
Sequence is one of the basic constructs of procedural programming. Name the other two procedural programming concepts. (2 marks)
Answer:
1 mark for each of the following points:
* Selection.
* Iteration.

## Object Orientated Code

Object orientated languages make use of the idea of classes and objects. A program written in OOC will be made up of a number of these classes and objects, which are manipulated through their internal methods. Before object orientated programming (OOP) was created, procedural languages such as C treated data and programming instructions that act upon the data as separate things. This made it very easy to write faulty code, such that one part of the program would overwrite data at the incorrect time.

### The Concept

OOP uses the idea of self-contained objects, which contain both the programming routines and the data being processed. The program is designed as a collective of objects which interact through the use of their public methods. Object classes may be used in other programs. OOP's tend to be more reliable, as objects are self-contained & so are easier to program. There are no hierarchical structures as in the traditional top-down design.
The benefits of an OOP are they make it easier to write working code; a class can be fully tested & released for other coders in the team to use. Classes can be treated as black boxes, as other coders don't need to know how it works internally, just how to manipulate it through its methods. There are many design patterns already available which solve common programming tasks. A coder can pick up a pattern for a UI for example, and begin coding straight away. Classes can easily be used in multiple projects as they can be reused. The code is also portable, as it only requires the CPU specific compiler.
Drawbacks of OOP include its steep learning curve, as becoming proficient in an OOP language can be difficult, as it requires a different way of thinking about problems to a standard programming language. This complexity requires skill to create efficient and flexible classes. They are also not as efficient and compact as low level language coding.
* Class - A template for a set of objects which have state and behaviour.
** Describes properties and methods of a real world entity. A "Blueprint" for actual objects created later.
** Each specific object is an instance of a class.
* Object - An instance of a class.
** A real world entity.
** Holds attributes and methods.
* Attributes - An attribute describes an individual data item within an entity
* Methods- A method is a procedure associated with an object
* Inheritance - The ability of a class (the derived class) to use properties and methods of another class (the parent class).
** Derived class is the class resulting from the inheritance process.

### Polymorphism & Overloading
#### Overloading

This is when a method is defined more than once within a class, for use in different situations. It may occur when different types or quantities of data are being put into an object. The supplied parameters determine what method is used.
Overloading is when calling one method will have a different effect depending on the data supplied to it.
For example:
    Tom might have a method Do_Work.
    Calling Do_Work('Computing') will have him work on his computing coursework.
    Whereas Do_Work('Maths') will have him do some maths homework.
Alternatively, overloading can act differently depending on the type of data given:
    Tom might have a method Sleep.
    Calling Sleep(8) will have him sleep for 8 hours.
    Calling Sleep('Tight') will have him sleep tight.

#### Polymorphism

A specialist form of overloading, which allows for very general object structures which can be used for a variety of data types.
Polymorphism is when multiple objects act consistently when the same method name is called on them.
For example:
    Tom might have a method Sleep.
    Tom's dog might have a method Sleep.
    Calling Sleep on either Tom or his dog will have the same effect- sending them to sleep.

### Data Encapsulation

Data Encapsulation is the combining of properties and methods for an object. Assigning and retrieving object property values are only done using methods to access the data.
Encapsulation - Where technical details of methods or properties are hidden within the object, and data can only be changed by using the appropriate methods. Ideally all methods & properties of an object will be encapsulated. It is the pattern of making attributes in a class private, but allowing them to be changed and accessed through public methods.
Data encapsulation is one of the key advantages of object-oriented code. It means that once you've written code that works, you can guarantee that it is not the source of any future problems as, since only its public methods (which you've rigorously tested) can be used to change its attributes, it will never go wrong. There is something very appealing about being able to create something useful and package it away such that it will always work.

## Non-Procedural/Declarative Language

This form of language tells a computer what to do, but not how to do it. An example of this type of language could be Prolog or SQL. They are concerned with facts, rules, making queries or asking questions. They are typically used in AI, logic based and knowledge based systems, or expert systems.

## Functional Languages

These languages are concerned with providing answers to problems purely by applying calculations to input data. They don't change anything other than the input data provided at that point. They have no "side effects" unlike procedural languages. In functional languages, calculations are referred to as functions. They are used extensively in mathematical environments including engineering, finance and network engineering.

## Memory Addressing Modes
### Direct Addressing

* The operand represents the location of data in memory.
* e.g. LOAD 18 means load from address 18.

### Indirect Addressing

* The memory location the operand represents holds the memory address of the data.
* e.g. LOAD 1 means load the contents of the address referenced in #1. If #1 contains #3 and #3 contains 45, then 45 is loaded.

### Immediate Addressing

* The operand is the actual value wanted.
* e.g. LOAD 2 loads the value 2 into the accumulator.

### Indexed Addressing

* One of the CPU registers is the index register. This holds the base memory address.
* Addresses used are relative to the base address stored in the index register.
* e.g. if the index register contains 2, then LOAD 1 will load the contents of memory address 3 (1+2=3)
* Iteration can be achieved by incrementing the register.