3.3 Programming Languages (B03) Flashcards
how does CPU run high level code?
Your high level code is translated to ‘low level’ instructions by a compiler or interpreter.
An assembler then converts the assembly code instruction into binary code (known as machine code and sometimes as object code) that the CPU can understand.
Some translators convert your high level code straight to machine code.
features of high level languages
More ‘English-like’ and hence easier to learn.
Less error-prone.
Easier to debug and to maintain.
Quicker to write – this can be more important in the business world than having code that runs as fast as possible.
Can work on different machines (portable).
A translator (interpreter or compiler) is required to convert the code into low level code before it can be run.
There are built-in functions for programmers to use. For example in Python, using the built-in function print makes the Python code output something to the screen. The programmer does not need to know the complexity of how the print statement is executed, just that it will do the job.
Similarly, for high-level languages, there are libraries of functions that can be imported to solve specific problems. For example in Python, you can import the random module (library) which lets you carry out various functions related to random numbers and all without needing to know exactly what the computer is doing.
features of low level languages
One assembly code construction = one line of machine code, doing one specific action.
Assembly code instructions are specific to a particular machine/CPU architecture.
Programmer needs to know about specific operations of the CPU, e.g. how memory is managed.
More control over what the hardware does, therefore can be more efficient.
Can be executed directly without needing to translate/interpret.
Difficult to manage, read or update code.
key difference between high- and low-level languages (speed and why)
High level languages are friendlier to read, but programs run more slowly because the code structure is based on a human-friendly structure.
In contrast, programs written in low level languages can run very quickly as it is based on the computer’s hardware architecture.
exam question!
machine code
the lowest level programming language - written in binary
commands that can be directly executed by a processor
There is a 1-1 relationship between what physically goes on inside a computer and a single instruction in machine language
instruction set
all the different machine code instructions a microprocessor (CPU) can execute
machine code pros and cons
Advantages:
Programs can execute very quickly.
Programs take up less memory.
Disadvantages:
Slow and difficult to write.
Error-prone.
Hard to remember.
Machine code is specific to each single machine.
assembly language
low-level language written in mnemonics
usually a one-to-one relationship between an assembly instruction (e.g. MOV) and the corresponding machine code
pros and cons of assembly language
how does writing code differ in high vs low-level languages
tasks for which high- or low-level languages are better suited
compilers vs interpreters (seperate file?, when, how often, errors)
assembler
Translate assembly language into machine code. Computers do not understand Assembly, hence the need for it to be converted.
Each different type of machine/processor requires an assembler of its own.
(special type of compiler)
compiler
translate a program written in one language into another language
source code -> compiler -> object code
machine code is saved and stored as an executable file (.exe) separately to the high-level code. This means users do not have access to the source code, preventing them editing or stealing it.
compiler is not needed to run the object code, so it easy to distribute the program – users just have to execute the final file
difficult to test line by line, but: points out all syntax errors
C++ is a compiled programming language.
interpreter
translates code into machine code, instruction by instruction – the CPU executes each instruction before the interpreter moves on to translate the next instruction
does not create an independent final set of source code – it takes a line of source code at a time and converts it to machine code (which the computer runs straight away)
Interpreted languages include JavaScript, PHP, Python and Ruby.
using compilers vs interpreters