# A-level Computing/OCR/Unit 1.2.2 Applications Generation
## Open and Closed Source
### Closed Source

Closed source programs are ones where software is compiled and provided to the user, without revealing the source code. This prevents users altering the code for personal gain or malicious use. It is usually sold commercially, meaning it is thoroughly tested and usually has free support associated with it.

##### Key Points

* Source code not available
* Users purchase a license to use the software
* Dedicated help or contact lines
* Adaptive, corrective and perfective maintenance

### Open Source

The source code of open source software is openly available to download. This means anybody is able to modify the code as they wish and help develop the software further. The most well known open source project is Linux, which was the basis for the Android OS. It is often free, however some companies will provide free software but charge a support fee. Open source projects tend to be produced by armies of volunteers, coders and testers. They can lack the polish and thorough testing of closed source programs, which could leave them feeling slightly clunky. However, if there are many people willing to contribute to them, they will receive regular updates and any flaw of one volunteer can be made up by another.

##### Key points

* Source code available
* Users can modify, amend, study or share
* Often large developer base
* Usually free
A gaming company decides to release a new video games console. The console will use a modified version of an operating system called Linux.
Linux is open-source.
Explain how Linux being open-source would benefit the games company.
[2]
Answer:
This means that a lot of the core functionality they need is already available (1) so the company just has to make amendments/additions specific to their system (1) saving time and money (1).

## Utilities

A utility is a relatively small program which has a single purpose, usually involving system maintenance. Examples include Antivirus, Antispam, Firewalls, Backups, Disk Defragmenters, Compressors and File Managers.
Describe the following examples of utility software and state how they would be used.
Description [2]
Use [1]
Description [2]
Use [1]
Answer:
-Allows communication between the hardware and the operating system
-Converts commands from one into instructions that the other can carry out
Use: -Allow device to communicate with computer [3]
-It checks the system continually for signs of viruses
-Works in the background
-If an indication is found that a virus is present the file is quarantined
Use:
-Example: Teacher would use it to check files imported by students for viruses
-keep system virus free
-if a virus found it is deleted / quarantined [3]

## Translators

Translators convert source code to object code, whilst detecting errors in the source code.

### Source Code

Programs written in a high level language (such as Python, Basic, C++ or Ruby). These languages allow a halfway house between natural language and machine code - this is easier for development.
* However, the computer cannot run a source file
* It needs to translate it into machine code

### Object Code

Once a program has been converted into machine code it is called an object program, composed of object code. There is a one-to-one relationship between object/machine code and assembly code. Each opcode has a machine and assembly code equivalent. The only exception to this rule is labels, which are created on the fly, as actual locations replace labels in translation.

### Interpreters

Interpreters translates and runs a single line/statement of code at a time. An interpreter will report an error when it translates a line that contains an error, and will then stop interpretation and indicate the position of the error. Each time the program is run the interpreter must be present. The program will run slower due to the translation taking up time and some lines being translated many times, for example if they are in a loop. Interpreters are often used in program development so that bugs and errors can be found without compiling the program for each test.
Explain why an interpreter may be preferred to a compiler as a translator when writing a high-level language program. [5]
Answer:
* Whole program not written...
* so may not compile
* Testing needs to be done...
* diagnostics will be more complete
* Individual segments can be run...
* allowing errors to be isolated
* Running will be necessary after very minor changes.
* continual compilation of whole code is very wasteful/time consuming (1 per max 5)

### Assemblers

In assembly code, the OPCODE is represented by a mnemonic and the OPERAND is a denary or hex number. An assembler converts a low level program into machine code prior to its execution. It reserves storage for instructions and data, and replaced mnemonic opcodes with machine codes. Symbolic addresses are replaced with numeric addresses, and it creates a symbol table to match labels and addresses. It will also check the syntax and give error diagnostics.
* Takes each assembly instruction
* Checks for syntax errors
* Looks up value of mnemonic from opcode table
* Gives memory addresses labels
* Converts to machine code
(a) An assembler may be used to produce machine code.
Answer:
* assembly (language) [1]
* binary notation/executable form
* set of all instructions available
* (instructions operate on) bytes of data dependent on architecture/processor design [3]
* reserves storage for instructions & data
* replaces mnemonic opcodes by machine codes
* replaces symbolic addresses by numeric addresses
* creates symbol table ...
* ... to match labels & addresses
* checks syntax/gives error diagnostics [4]

### Compilers

Compilers translate whole high level programs as a unit and create an executable program. This protects the program from malicious use or modification. The compiled program is architecture specific, and during compilation the compiler may report many errors in the source code, some of which may be spurious. During compilation the program is optimised, which will improve its speed and reduce its size. Once the executable is created, the compiler is no longer required.

#### How a Compiler Works

There are several stages of compilation, as outlined below:

##### 1. Lexical Analysis

* All the comments and whitespace are removed from the program.
* High level code is turned into a series of tokens, using reserved words that the computer is able to pick out.
* A symbol table is created and used to keep track of variables and subroutines.

##### 2. Syntax Analysis

* Checks code is written in a valid syntax
* Compiles a list of errors for user where code does not follow the languages rules
* It produces an abstract syntax tree, which represents the program.
* Semantic analysis is carried out, this is where the compiler checks for logical errors:
** Type mismatch, e.g. attempting to assign a string literal to an integer
** Undeclared variables or accessing an out of scope variable
** Multiple declaration of a single variable in the same scope
** Accessing an array with an out of bounds integer literal

##### 3. Code Generation

* Here, code from the abstract syntax tree is converted into object code.

##### 4. Optimisation

* Any unnecessary code is removed.
* It looks for any more efficient alternatives to instructions or groups of instructions and replaces them.
http://www.ocr.org.uk/Images/142267-mark-scheme-unit-f453-advanced-computing-theory-january.pdf
Tokens are used during compilation. Describe tokens and state at what stage they are created. [3]
Tick one box in each row to show at which stage(s) each of the following statements is true. [6]
Answer:
Three from:
* Created during lexical analysis
* Used for reserved words/keywords
* Each token is a string of binary digits...
* ... of fixed length.

## Linkers, Loaders and Libraries
### Libraries

Libraries contain pre-written code which can be accessed and run by programs. They allow developers to use pre-written and pre-tested code, that is often designed to handle complex tasks such as graphics or cryptography, which often require a certain level of expert knowledge.They are accessed and used through an Application Programming Interface (API).
Library routines are examples of these pieces or pre-written code.They are pre-compiled and error free, and as such are ready to use, reducing work and time required when programming. They can be used multiple times, and many have been written in a different source language. It allows the programmer to make use of other peoples expertise, meaning you won't end up 'reinventing the wheel' unnecessarily.

### Linkers

The purpose of a linker is to include the code used from any libraries into the final program. They can be either static or dynamic; static linkers incorporate the code directly into the executable, which can dramatically increase its size. Dynamic linkers link to the code only when it is run, as it is stored on the programs computer, thus reducing the size of the executable.

### Loaders

Loaders are a part of the operating system responsible for loading a program and its dependencies (such as dynamically linked libraries) into memory. It handles the addresses when the program is run.
A program is to be run on a computer system.
Explain the purpose of the following in preparing the program to be run.
(i) linkers
(ii) loaders
Answer:
(i)
* Used to combine already compiled procedures...
* to produce an executable file
* Deals with external references from the main program to other (pre-compiled) modules
(ii)
* Copies object code/executable code into...
* primary memory ready for execution
* Deals with addressing anomalies/re-locatable addresses
(1 per -, max 4)
[4]