Programming Paradigms
Why do we need different programming paradigms
-different paradigms better suited for different problem types
-allows to choose the most appropriate one to create efficient and effective code
- each has its own strengths and weaknesses
-different levels of abstraction.
-different trade-offs between readability, maintainability, and performance
- some languages support only one paradigm e.g Small Basic, some support multiple e.g Python
Using multiple paradigms
Declarative Language
Functional programming
Logic programming
-uses code to define facts and rules based on the problem, with queries used to find answers.
Imperative Languages:
Procedural Programming:
Object-Oriented Programming:
Comparison of Procedural vs Object-Oriented Paradigms:
High level languages
pros of High level language
cons of high level languages
Assembly language
Pros of assembly language
-requires less memory and same execution efficiency as machine code. useful in device drivers when fast execution is required
-complete control of the code, individual statements can manipulate specific hardware components and can work directly on memory locations
- direct control of hardware and how memory is used via addressing mode
- provides direct access to system-level features w/o having to go through an abstraction layer provided by a software interface (e.g library/OS/framework - e.g open( ) for file system abstraction)
- easier to optimise and is optimised for a specific architecture
- programmers can choose the exact instructions so can write code that is highly efficient
- can make use of special machine-dependent instructions e.g. in the instruction set for the chip.
- best suited for when compilers or interpreters don’t exist for target CpU, & embedded systems highest possible performance is critical as memory limited
cons of assembly language
Little man computing
Little Man Computing Instructions/Assembly Mneumonics
0 - HLT - Stop program
1 - ADD - Add the contents of the memory address specified to the accumulator
2 - SUB - Subtract the contents of the memory address specified from the accumulator
3 - STA/STO - Store the value currently stored in the accumulator in the memory address specified
4 - Unused instruction that generates an error message
5 - LDA - Load the contents of the memory address into the accumulator
6 - BRA - jump always to the designated memory address/point in program
7 - BRZ - jump to the designated memory address/point in program if the accumulator is zero
8 - BRP - jump to the designated memory address/point in program if the accumulator is zero or positive
9 - INP - allows the user to input a value which will be held in the accumulator
10 - OUT - prints the value held in the accumulator
11 - DAT - reserves the memory address followed by DAT for storing data when the instruction is compiled
Little Man Computing example (uses branching and looping, end and loop are labels made myself)
inp
sta x
loop lda x
out
sub one
sta x
brz end
bra loop
end out x
hlt
x dat
one dat 1
Modes of Addressing Memory
Immediate addressing
Direct addressing
Indirect addressing
Indexed addressing