0% found this document useful (0 votes)
21 views71 pages

K11 Computer

The document discusses the history and concepts of computers including their invention by Charles Babbage and Konrad Zuse. It describes the Von Neumann architecture which separates a computer into a central processing unit, memory, and input/output components that are connected by a data bus.

Uploaded by

DunsScoto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views71 pages

K11 Computer

The document discusses the history and concepts of computers including their invention by Charles Babbage and Konrad Zuse. It describes the Von Neumann architecture which separates a computer into a central processing unit, memory, and input/output components that are connected by a data bus.

Uploaded by

DunsScoto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Advanced R

Chapter 1: How does a computer calculate?

Daniel Horn & Sheila Görz

Summer Semester 2022

Daniel Horn & Sheila Görz FPR Summer Semester 2022 1 / 41


What is a computer?

What is a computer?

Daniel Horn & Sheila Görz FPR Summer Semester 2022 2 / 41


What is a computer?

Definitions?

What does it do?


It computes / executes programs.
It provides access to the internet.
It shows me my vacation photos.

What does it consist of?


Black box beneath the desk (case)
Display, keyboard, mouse, printer, ...
Processor, hard drive, graphics card, ...

Wikipedia
A computer is a machine that can be instructed to carry out sequences of
arithmetic or logical operations automatically via computer programming.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 3 / 41


What is a computer?

Who invented it?

Charles Babbage (b 1791, d 1871), English


mathematician, philosopher, inventor and economist
1822: Design of a difference engine for evaluation
of polynomial functions
1837: Proposition of the Analytical Engine – a
mechanical computer
Powered by a steam engine, it was 19m high, 3m
wide and had 12kB of memory.
Due to financial and technical difficulties it was
never built, but it is said to be functional.

Images retrieved from de.wikipedia.org/wiki/Analytical_Engine on Aug. 1, 2017

Daniel Horn & Sheila Görz FPR Summer Semester 2022 4 / 41


What is a computer?

Who invented it?


Konrad Zuse (b 1910, d 1995), was a German civil
engineer, inventor and businessman
Worked as a structural engineer, but was too lazy
to calculate by hand.
1937: Zuse Z1 – Mechanical computer
It was not fully operational
1941: Z3 – Replacing mechanical switches by relays
The Z3 is considered to be the world’s first
operational computer – yet it is of no relevant
practical use.
It was destroyed during World War II.
Zuse was not aware of Babbage’s work.
Images retrieved from https://de.wikipedia.org/wiki/Konrad_Zuse and
https://de.wikipedia.org/wiki/Zuse_Z3 on Aug. 1, 2017
Daniel Horn & Sheila Görz FPR Summer Semester 2022 5 / 41
What is a computer?

Who invented it?

John von Neumann (b 1903 in Budapest, d 1957 in


Washington, D.C.), mathematician and polymath
Countless contributions to the field of Mathematics
(e.g. founder of game theory)
Advancement of the ENIAC with J. Presper Eckert
and John W. Mauchly
ENIAC: Electronic Numerical Integrator and
Computer (to calculate artillery firing tables)
The ENIAC was programmed by manipulating
plugboard wires and rotary switches.
1945: A ’First Draft of a Report on the EDVAC’
describes the modern computer.
Images retrieved from https://de.wikipedia.org/wiki/John_von_Neumann und
https://de.wikipedia.org/wiki/ENIAC on Aug. 1, 2017
Daniel Horn & Sheila Görz FPR Summer Semester 2022 6 / 41
Von Neumann architecture

Von Neumann architecture

Daniel Horn & Sheila Görz FPR Summer Semester 2022 7 / 41


Von Neumann architecture

Von Neumann architecture

Processor

ALU
(Arithmetic Control unit
Logic Unit)

Data bus
Input and
Memory
output
devices

Daniel Horn & Sheila Görz FPR Summer Semester 2022 8 / 41


Von Neumann architecture

Characteristics of the von Neumann architecture I

1 A computer consists of these components: memory, ALU, control unit,


I/O devices and a data bus.

2 Its structure is independent of the task at hand – the task is specified


by the interchangeable contents of the memory.

3 Instructions and data are stored in the same physical memory.

4 Memory is divided into equally sized cells following consecutive


numeration.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 9 / 41


Von Neumann architecture

Characteristics of the von Neumann architecture II

5 A program is a set of instructions stored in cells with ascending


addresses in the order of execution.

6 Deviation from the stored order of instructions can be triggered by


so-called jump instructions.

7 All values are represented by binary digits (bit vectors).

8 Bit vectors contain no explicit information about the type they are
representing. Their interpretation must be derivable from the context
they are used in.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 10 / 41


Von Neumann architecture

The other components

Memory
1 Bit = Information: 0 or 1 Input/output devices
1 Byte = 8 Bit = Words like Display, printer, ...
00101001 Keyboard, mouse, ...
Numbered cells sized 1 Byte Also: network, internet, ...
Different techniques: hard
drives, CD, DVD, ... Control unit
Program counter: current
Data bus instruction
Transfers data between Responsible for loading
components instructions and data into
Electric current either exists the ALU...
or it does not

Daniel Horn & Sheila Görz FPR Summer Semester 2022 11 / 41


Von Neumann architecture

The von Neumann cycle of the control unit

von Neumann cycle


1 FETCH: Loads current instruction (as specified by the program
counter) from the memory and increments program counter by 1.
2 DECODE: Decodes the instruction for the ALU.
3 FETCH OPERANDS: Loads operands from the memory.
4 EXECUTE: The ALU executes the given instruction.
5 WRITE BACK: Writes back possible results into the memory.

Let’s take a (very simplified) look at the ALU:


Given a set of binary words it calculates an arithmetic/logical (e.g.
addition) result. How does this work?

Daniel Horn & Sheila Görz FPR Summer Semester 2022 12 / 41


How does the ALU perform addition?

How does the ALU perform addition?

Daniel Horn & Sheila Görz FPR Summer Semester 2022 13 / 41


How does the ALU perform addition?

Electric circuits

Electric circuits
A computer knows two possible states: there either exists electric
current or there does not.
A computer can control the flow of electric charge by opening or
closing switches.

1
Emitter Switch Collector

Daniel Horn & Sheila Görz FPR Summer Semester 2022 14 / 41


How does the ALU perform addition?

Electric circuits

Electric circuits
A computer knows two possible states: there either exists electric
current or there does not.
A computer can control the flow of electric charge by opening or
closing switches.

1
Emitter Switch Collector

Daniel Horn & Sheila Görz FPR Summer Semester 2022 14 / 41


How does the ALU perform addition?

Transistors
How can a computer operate a switch?
Solution: transistors are electric switches
Electric switches
Element with 2 inlets and 1 outlet:
If electric current is registered at the control inlet c, then the signal of the
other inlet is transmitted to the outlet. Otherwise, not.

Emitter Mass
1 0

1 T
Emitter Collector
Transistor

Daniel Horn & Sheila Görz FPR Summer Semester 2022 15 / 41


How does the ALU perform addition?

Transistors
How can a computer operate a switch?
Solution: transistors are electric switches
Electric switches
Element with 2 inlets and 1 outlet:
If electric current is registered at the control inlet c, then the signal of the
other inlet is transmitted to the outlet. Otherwise, not.

Emitter Mass
1 0

1 T
Emitter Collector
Transistor

Daniel Horn & Sheila Görz FPR Summer Semester 2022 15 / 41


How does the ALU perform addition?

Logic gates: AND

Our first logic gate


A B A∧B
0 0 0
2 bit input, 1 bit output 0 1 0
Output is 1 if both inputs are 1 1 0 0
1 1 1

Input A Input B
A B

AND c c

1 T T
Output

Daniel Horn & Sheila Görz FPR Summer Semester 2022 16 / 41


How does the ALU perform addition?

Logic gates: AND

Our first logic gate


A B A∧B
0 0 0
2 bit input, 1 bit output 0 1 0
Output is 1 if both inputs are 1 1 0 0
1 1 1

Input A Input B
A B

AND c c

1 T T
Output

Daniel Horn & Sheila Görz FPR Summer Semester 2022 16 / 41


How does the ALU perform addition?

Logic gates: AND

Our first logic gate


A B A∧B
0 0 0
2 bit input, 1 bit output 0 1 0
Output is 1 if both inputs are 1 1 0 0
1 1 1

Input A Input B
A B

AND c c

1 T T
Output

Daniel Horn & Sheila Görz FPR Summer Semester 2022 16 / 41


How does the ALU perform addition?

Logic gates: AND

Our first logic gate


A B A∧B
0 0 0
2 bit input, 1 bit output 0 1 0
Output is 1 if both inputs are 1 1 0 0
1 1 1

Input A Input B
A B

AND c c

1 T T
Output

Daniel Horn & Sheila Görz FPR Summer Semester 2022 16 / 41


How does the ALU perform addition?

Logic gates: OR

Our second logic gate


A B A∨B
0 0 0
2 bit input, 1 bit output 0 1 1
Output is 1 if at least one input is 1 1 0 1
1 1 1

Input A Input B
A B
c
OR
T
1 c
T Output

Daniel Horn & Sheila Görz FPR Summer Semester 2022 17 / 41


How does the ALU perform addition?

Logic gates: OR

Our second logic gate


A B A∨B
0 0 0
2 bit input, 1 bit output 0 1 1
Output is 1 if at least one input is 1 1 0 1
1 1 1

Input A Input B
A B
c
OR
T
1 c
T Output

Daniel Horn & Sheila Görz FPR Summer Semester 2022 17 / 41


How does the ALU perform addition?

Logic gates: OR

Our second logic gate


A B A∨B
0 0 0
2 bit input, 1 bit output 0 1 1
Output is 1 if at least one input is 1 1 0 1
1 1 1

Input A Input B
A B
c
OR
T
1 c
T Output

Daniel Horn & Sheila Görz FPR Summer Semester 2022 17 / 41


How does the ALU perform addition?

Logic gates: OR

Our second logic gate


A B A∨B
0 0 0
2 bit input, 1 bit output 0 1 1
Output is 1 if at least one input is 1 1 0 1
1 1 1

Input A Input B
A B
c
OR
T
1 c
T Output

Daniel Horn & Sheila Görz FPR Summer Semester 2022 17 / 41


How does the ALU perform addition?

Half adder

Adding 2 bits
A B C S
We want to add 2 bits
0 0 0 0
Note: Carry (C) is an AND 0 1 0 1
S is also known as XOR (Symbol ⊕) 1 0 0 1
Realization of ⊕ → exercises 1 1 1 0

A AND C

B XOR S

Daniel Horn & Sheila Görz FPR Summer Semester 2022 18 / 41


How does the ALU perform addition?

Full adder

Adding 3 bits
A B Cold Cnew S
Usually, we have to add 3 bits:
number 1, number 2 and carry. 0 0 0 0 0
0 1 0 0 1
Carry and sum are still logical
1 0 0 0 1
operations.
1 1 0 1 0
S = A ⊕ B ⊕ Cold 0 0 1 0 1
Cnew = (A ∧ B) ∨ (A ∧ Cold ) ∨ (B ∧ 0 1 1 1 0
Cold ) 1 0 1 1 0
Circuit diagram → exercises 1 1 1 1 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 19 / 41


How does the ALU perform addition?

Adding numbers of arbitrary length

Strategy
A and B are bit vectors, i.e. A = (an , ..., a1 ) with ai ∈ {0, 1}.
We are seeking bit vector S = A + B of length n + 1.
Leading zeros are allowed in all numbers.
We are now performing addition by hand:

Sketch of the addition circuit


1 Add a and b with a half adder receiving s und c .
1 1 1 1
2 For i ∈ {2, ..., n}:
Add ai , bi and ci−1 with a full adder receiving si und ci .
3 The result is given by the bit vector cn , sn , ..., s1 .

Daniel Horn & Sheila Görz FPR Summer Semester 2022 20 / 41


Machine and assembly language

Machine and assembly language

Daniel Horn & Sheila Görz FPR Summer Semester 2022 21 / 41


Machine and assembly language

So, how the heck do we program this now?

Machine language
Program instructions are binary strings (what else?).
The control unit decodes the instructions so they can be executed by
the respective system.
Common instructions are:
1 Move the contents of a memory cell
2 Instruct the ALU to perform some operation
3 Alter the program counter (jump instruction)

Assembly language
Assembly language is basically the same thing as machine language –
except that binary instructions are replaced by more comprehensible
instructions like ADD and GOTO.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 22 / 41


Machine and assembly language

Our simple computer

Components
Memory: consisting of 32 cells with 8 bits each, numbered from 0-31.
Each cell contains either an instruction or a positive integer between 0
and 28 − 1 in binary representation.
Program counter: Which of the 32 cells contains the current
instruction? Starts at 0 and is incremented by 1 after each instruction.
ALU: can add / subtract two binary numbers.
Accumulator (Acc): provides 16 bit of memory for arithmetic results.
The result of an operation is always stored in the acc.

Instructions
Our machine is capable of a few instructions of 8 bit length: The first 3
bits encode the instruction while the last 5 bits specify the memory cell.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 23 / 41


Machine and assembly language

List of instructions
Code Abbr. Outcome
001 LOAD Load the cell’s content into the acc.
010 SAVE Save the acc’s content into the cell.
100 ADD Add the cell’s content onto the acc.
101 SUB Subtract the cell’s content from the acc.
110 GOTO Set program counter to given constant.
111 IFGT Content of acc = 0: Set counter to constant
000 EXIT Terminate the execution of the program.

Examples
10010001 = ADD 10001 = Add the content of cell 17 onto the
accumulator.
11100010 = IFGT 00010 = If the accumulator’s content is 0, then set
the program counter to 2.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 24 / 41


Machine and assembly language

Exemplary program: multiplication

Initialization Cell Content


Program stored in cells 0 to 8. 0 LOAD 11
Input in cells 9 and 10, final result in cell 1 ADD 9
11, constant in cell 12 2 SAVE 11
3 LOAD 10
Procedure 4 SUB 12
1 Load the result into the accumulator. 5 SAVE 10
6 IFGT 8
2 Increment the result by the first factor and
7 GOTO 0
write it back to the memory.
8 EXIT
3 Load the second factor into the acc, 9 NUM1
decrement by 1 and write back to memory. 10 NUM2
4 If acc = 0: Go the end of the program. 11 0
5 Otherwise: Go to the start of the program. 12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 25 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 1

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
0 – – - 2 SAVE 11
1 0 LOAD 11 0 3 LOAD 10
2 1 ADD 9 5 4 SUB 12
3 2 SAVE 11 5 5 SAVE 10
4 3 LOAD 10 3 6 IFGT 8
5 4 SUB 12 2 7 GOTO 0
6 5 SAVE 10 2 8 EXIT
7 6 IFGT 8 2 9 5
8 7 GOTO 0 2 10 3
11 0
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 26 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 1

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
0 – – - 2 SAVE 11
1 0 LOAD 11 0 3 LOAD 10
2 1 ADD 9 5 4 SUB 12
3 2 SAVE 11 5 5 SAVE 10
4 3 LOAD 10 3 6 IFGT 8
5 4 SUB 12 2 7 GOTO 0
6 5 SAVE 10 2 8 EXIT
7 6 IFGT 8 2 9 5
8 7 GOTO 0 2 10 3
11 0
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 26 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 1

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
0 – – - 2 SAVE 11
1 0 LOAD 11 0 3 LOAD 10
2 1 ADD 9 5 4 SUB 12
3 2 SAVE 11 5 5 SAVE 10
4 3 LOAD 10 3 6 IFGT 8
5 4 SUB 12 2 7 GOTO 0
6 5 SAVE 10 2 8 EXIT
7 6 IFGT 8 2 9 5
8 7 GOTO 0 2 10 3
11 0
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 26 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 1

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
0 – – - 2 SAVE 11
1 0 LOAD 11 0 3 LOAD 10
2 1 ADD 9 5 4 SUB 12
3 2 SAVE 11 5 5 SAVE 10
4 3 LOAD 10 3 6 IFGT 8
5 4 SUB 12 2 7 GOTO 0
6 5 SAVE 10 2 8 EXIT
7 6 IFGT 8 2 9 5
8 7 GOTO 0 2 10 3
11 5
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 26 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 1

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
0 – – - 2 SAVE 11
1 0 LOAD 11 0 3 LOAD 10
2 1 ADD 9 5 4 SUB 12
3 2 SAVE 11 5 5 SAVE 10
4 3 LOAD 10 3 6 IFGT 8
5 4 SUB 12 2 7 GOTO 0
6 5 SAVE 10 2 8 EXIT
7 6 IFGT 8 2 9 5
8 7 GOTO 0 2 10 3
11 5
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 26 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 1

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
0 – – - 2 SAVE 11
1 0 LOAD 11 0 3 LOAD 10
2 1 ADD 9 5 4 SUB 12
3 2 SAVE 11 5 5 SAVE 10
4 3 LOAD 10 3 6 IFGT 8
5 4 SUB 12 2 7 GOTO 0
6 5 SAVE 10 2 8 EXIT
7 6 IFGT 8 2 9 5
8 7 GOTO 0 2 10 3
11 5
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 26 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 1

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
0 – – - 2 SAVE 11
1 0 LOAD 11 0 3 LOAD 10
2 1 ADD 9 5 4 SUB 12
3 2 SAVE 11 5 5 SAVE 10
4 3 LOAD 10 3 6 IFGT 8
5 4 SUB 12 2 7 GOTO 0
6 5 SAVE 10 2 8 EXIT
7 6 IFGT 8 2 9 5
8 7 GOTO 0 2 10 2
11 5
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 26 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 1

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
0 – – - 2 SAVE 11
1 0 LOAD 11 0 3 LOAD 10
2 1 ADD 9 5 4 SUB 12
3 2 SAVE 11 5 5 SAVE 10
4 3 LOAD 10 3 6 IFGT 8
5 4 SUB 12 2 7 GOTO 0
6 5 SAVE 10 2 8 EXIT
7 6 IFGT 8 2 9 5
8 7 GOTO 0 2 10 2
11 5
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 26 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 1

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
0 – – - 2 SAVE 11
1 0 LOAD 11 0 3 LOAD 10
2 1 ADD 9 5 4 SUB 12
3 2 SAVE 11 5 5 SAVE 10
4 3 LOAD 10 3 6 IFGT 8
5 4 SUB 12 2 7 GOTO 0
6 5 SAVE 10 2 8 EXIT
7 6 IFGT 8 2 9 5
8 7 GOTO 0 2 10 2
11 5
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 26 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 2

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
9 0 LOAD 11 5 2 SAVE 11
10 1 ADD 9 10 3 LOAD 10
11 2 SAVE 11 10 4 SUB 12
12 3 LOAD 10 2 5 SAVE 10
13 4 SUB 12 1 6 IFGT 8
14 5 SAVE 10 1 7 GOTO 0
15 6 IFGT 8 1 8 EXIT
16 7 GOTO 0 1 9 5
10 2
11 5
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 27 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 2

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
9 0 LOAD 11 5 2 SAVE 11
10 1 ADD 9 10 3 LOAD 10
11 2 SAVE 11 10 4 SUB 12
12 3 LOAD 10 2 5 SAVE 10
13 4 SUB 12 1 6 IFGT 8
14 5 SAVE 10 1 7 GOTO 0
15 6 IFGT 8 1 8 EXIT
16 7 GOTO 0 1 9 5
10 2
11 5
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 27 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 2

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
9 0 LOAD 11 5 2 SAVE 11
10 1 ADD 9 10 3 LOAD 10
11 2 SAVE 11 10 4 SUB 12
12 3 LOAD 10 2 5 SAVE 10
13 4 SUB 12 1 6 IFGT 8
14 5 SAVE 10 1 7 GOTO 0
15 6 IFGT 8 1 8 EXIT
16 7 GOTO 0 1 9 5
10 2
11 10
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 27 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 2

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
9 0 LOAD 11 5 2 SAVE 11
10 1 ADD 9 10 3 LOAD 10
11 2 SAVE 11 10 4 SUB 12
12 3 LOAD 10 2 5 SAVE 10
13 4 SUB 12 1 6 IFGT 8
14 5 SAVE 10 1 7 GOTO 0
15 6 IFGT 8 1 8 EXIT
16 7 GOTO 0 1 9 5
10 2
11 10
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 27 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 2

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
9 0 LOAD 11 5 2 SAVE 11
10 1 ADD 9 10 3 LOAD 10
11 2 SAVE 11 10 4 SUB 12
12 3 LOAD 10 2 5 SAVE 10
13 4 SUB 12 1 6 IFGT 8
14 5 SAVE 10 1 7 GOTO 0
15 6 IFGT 8 1 8 EXIT
16 7 GOTO 0 1 9 5
10 2
11 10
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 27 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 2

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
9 0 LOAD 11 5 2 SAVE 11
10 1 ADD 9 10 3 LOAD 10
11 2 SAVE 11 10 4 SUB 12
12 3 LOAD 10 2 5 SAVE 10
13 4 SUB 12 1 6 IFGT 8
14 5 SAVE 10 1 7 GOTO 0
15 6 IFGT 8 1 8 EXIT
16 7 GOTO 0 1 9 5
10 1
11 10
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 27 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 2

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
9 0 LOAD 11 5 2 SAVE 11
10 1 ADD 9 10 3 LOAD 10
11 2 SAVE 11 10 4 SUB 12
12 3 LOAD 10 2 5 SAVE 10
13 4 SUB 12 1 6 IFGT 8
14 5 SAVE 10 1 7 GOTO 0
15 6 IFGT 8 1 8 EXIT
16 7 GOTO 0 1 9 5
10 1
11 10
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 27 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 2

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
9 0 LOAD 11 5 2 SAVE 11
10 1 ADD 9 10 3 LOAD 10
11 2 SAVE 11 10 4 SUB 12
12 3 LOAD 10 2 5 SAVE 10
13 4 SUB 12 1 6 IFGT 8
14 5 SAVE 10 1 7 GOTO 0
15 6 IFGT 8 1 8 EXIT
16 7 GOTO 0 1 9 5
10 1
11 10
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 27 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 3

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
17 0 LOAD 11 10 2 SAVE 11
18 1 ADD 9 15 3 LOAD 10
19 2 SAVE 11 15 4 SUB 12
20 3 LOAD 10 1 5 SAVE 10
21 4 SUB 12 0 6 IFGT 8
22 5 SAVE 10 0 7 GOTO 0
23 6 IFGT 8 0 8 EXIT
24 8 EXIT 0 0 9 5
10 1
11 10
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 28 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 3

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
17 0 LOAD 11 10 2 SAVE 11
18 1 ADD 9 15 3 LOAD 10
19 2 SAVE 11 15 4 SUB 12
20 3 LOAD 10 1 5 SAVE 10
21 4 SUB 12 0 6 IFGT 8
22 5 SAVE 10 0 7 GOTO 0
23 6 IFGT 8 0 8 EXIT
24 8 EXIT 0 0 9 5
10 1
11 10
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 28 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 3

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
17 0 LOAD 11 10 2 SAVE 11
18 1 ADD 9 15 3 LOAD 10
19 2 SAVE 11 15 4 SUB 12
20 3 LOAD 10 1 5 SAVE 10
21 4 SUB 12 0 6 IFGT 8
22 5 SAVE 10 0 7 GOTO 0
23 6 IFGT 8 0 8 EXIT
24 8 EXIT 0 0 9 5
10 1
11 15
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 28 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 3

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
17 0 LOAD 11 10 2 SAVE 11
18 1 ADD 9 15 3 LOAD 10
19 2 SAVE 11 15 4 SUB 12
20 3 LOAD 10 1 5 SAVE 10
21 4 SUB 12 0 6 IFGT 8
22 5 SAVE 10 0 7 GOTO 0
23 6 IFGT 8 0 8 EXIT
24 8 EXIT 0 0 9 5
10 1
11 15
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 28 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 3

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
17 0 LOAD 11 10 2 SAVE 11
18 1 ADD 9 15 3 LOAD 10
19 2 SAVE 11 15 4 SUB 12
20 3 LOAD 10 1 5 SAVE 10
21 4 SUB 12 0 6 IFGT 8
22 5 SAVE 10 0 7 GOTO 0
23 6 IFGT 8 0 8 EXIT
24 8 EXIT 0 0 9 5
10 1
11 15
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 28 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 3

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
17 0 LOAD 11 10 2 SAVE 11
18 1 ADD 9 15 3 LOAD 10
19 2 SAVE 11 15 4 SUB 12
20 3 LOAD 10 1 5 SAVE 10
21 4 SUB 12 0 6 IFGT 8
22 5 SAVE 10 0 7 GOTO 0
23 6 IFGT 8 0 8 EXIT
24 8 EXIT 0 0 9 5
10 0
11 15
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 28 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 3

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
17 0 LOAD 11 10 2 SAVE 11
18 1 ADD 9 15 3 LOAD 10
19 2 SAVE 11 15 4 SUB 12
20 3 LOAD 10 1 5 SAVE 10
21 4 SUB 12 0 6 IFGT 8
22 5 SAVE 10 0 7 GOTO 0
23 6 IFGT 8 0 8 EXIT
24 8 EXIT 0 0 9 5
10 0
11 15
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 28 / 41


Machine and assembly language

Calculate 5 * 3: Iteration 3

Cell Content
0 LOAD 11
Iteration Counter Instruction Acc 1 ADD 9
17 0 LOAD 11 10 2 SAVE 11
18 1 ADD 9 15 3 LOAD 10
19 2 SAVE 11 15 4 SUB 12
20 3 LOAD 10 1 5 SAVE 10
21 4 SUB 12 0 6 IFGT 8
22 5 SAVE 10 0 7 GOTO 0
23 6 IFGT 8 0 8 EXIT
24 8 EXIT 0 0 9 5
10 0
11 15
12 1

Daniel Horn & Sheila Görz FPR Summer Semester 2022 28 / 41


Machine and assembly language

Disadvantages of machine language

Machine language directly addresses the computer’s hardware and is


therefore efficient while allowing control of every detail
No normal human being can read machine language – even in
assembly language only short programs are comprehensible.
We humans require simpler programming languages which we can
understand better and actually use:
Development of a variety of high-level = more comprehensible
programming languages, e.g. R.

res = 0
for (i in 1:3)
res = res + 5

The computer itself only understands machine language.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 29 / 41


Programming paradigms

Programming paradigms

Daniel Horn & Sheila Görz FPR Summer Semester 2022 30 / 41


Programming paradigms

High-level programming languages

Replace blocks of machine language by more comprehensible


operators.
Examples
Use variables and assignments.
Use mathematical infix operators: 3 + 5.
Use loops instead of GOTO.

FORTRAN (1957) by IBM is considered the first high-level


programming language.
Since then: numerous new developments / advancements.
Notable languages: C (1972), S (1976), C++ (1983), Java (1995), ...
Languages realize different programming paradigms.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 31 / 41


Programming paradigms

Imperative programming I

The name stems from the Latin word imperare – to command.


A program is a sequence of commands / instructions.
Typical imperative programming code looks like this:
Carry out instruction 1
Carry out instruction 2
Carry out instruction 3
.
.
Return result

Assembly languages fulfill the imperative paradigm.


The imperative principle is closely related to machine language which
also processes a sequence of instructions. Therefore, it is the oldest
paradigm.
Daniel Horn & Sheila Görz FPR Summer Semester 2022 32 / 41
Programming paradigms

Imperative programming II

Procedural programming
Until now: A program is a long sequence of instructions.
Now: Divide it into subroutines that can call upon each other.
Every subroutine possesses well-defined input and output parameters.
In theory, it can be realized through jump instructions.

Structural programming
Refrains from using jump instructions as they are incomprehensible.
Instead, looping and branching statements are used.

In principle, we know all this already – it corresponds to our intuitive way


of programming.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 33 / 41


Programming paradigms

Object-oriented programming

A program consists of two components: data and functions.


Classical (imperative) programming languages keep them separated, yet in
complex programs they are connected often times.

Object-oriented programming structures the interaction of data and


functions by linking them both to the overarching principle of the object.

There are entire books dedicated to object-oriented programming – and


numerous philosophical discussions as well. We will only look at the bare
minimum needed to understand object-oriented programming in R.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 34 / 41


Programming paradigms

Object

An object is a thing – whatever this is


An objects knows something (data)
An object is capable of doing something (functions)

In principle it holds: Everything is an object.

Example
Object: My cat Midna
Data: Position, size, weight, color, ...
Methods: Running, eating, napping, ...

Objects can communicate with each other, i.e. exchange data and call
functions.
Daniel Horn & Sheila Görz FPR Summer Semester 2022 35 / 41
Programming paradigms

Class

A class is a conceptual description of objects.


Every object is an instance of a class. It knows and is capable of
everything described in its class.
Each object’s data manifests individually.

Example
Midna is an instance of the class cat. Her sister Phai is too.
Both have a weight, but each has their own specific weight.

Classes are arranged hierarchically, e.g. cats are mammals.


Specialized classes inherit their characteristics from more general
classes.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 36 / 41


Programming paradigms

Object-oriented programming languages

They trace back to Simula (1967) and Smalltalk (1972).


They are very popular since the 1990’s (especially C++ and Java).
By now, the entire process of software development is based on an
object-oriented approach.
Most modern programming languages offer methods for imperative
and object-oriented programming alike.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 37 / 41


Programming paradigms

Declarative programming languages

Declarative languages follow a very different approach:


Only describe what is to be done, but not how. The how is left for the
computer to decide (usually by recursion).
Explicit instructions like looping statements are not possible.

Example: Multiplication
1 a×0=0
2 a × b = a + (a × (b − 1))

Functional programming
The entire program is a (nested) function call.
Functions are just like all other data objects. For example, they can be
the input or output of another function.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 38 / 41


Programming paradigms

Compiler versus Interpreter I

Problem: The computer comprehends machine language, but we can only


program reasonably well in a high-level programming language.
Compiler
Step 1: Translate the program into machine language.
Step 2: Execute the program.
During compilation, the program can be (automatically) checked for
errors and optimized.

Interpreter
A program is running in the background, the so-called interpreter.
Program code is executed directly at runtime (one instruction at a
time).

Daniel Horn & Sheila Görz FPR Summer Semester 2022 39 / 41


Programming paradigms

Compiler versus Interpreter II


Compiler Interpreter
Pros
Efficient code Direct feedback
Programs are portable Simple error search:
between platforms Program stops at an
(after new compilation) error

Cons
Increased effort: Slow, since some code
Requires new chunks need to be
compilation after each interpreted multiple
modification times (loops)
Makes error search The interpreter itself
more complicated needs resources

Daniel Horn & Sheila Görz FPR Summer Semester 2022 40 / 41


Programming paradigms

Where does R fit in here?

R supports almost all of the presented concepts.


R is (obviously) an imperative programming language.
R can be used for object-oriented programming for which it supports
several different systems. However, R is not a typical object-oriented
language.
R offers all tools needed for functional programming.
R is an interpreted programming language.
R offers the possibility of embedding compiled code from other
programming languages.

Daniel Horn & Sheila Görz FPR Summer Semester 2022 41 / 41

You might also like