0% found this document useful (0 votes)
15 views125 pages

233 Notes Section1

The document covers binary representation in computer systems, including conversions between decimal, binary, and hexadecimal formats for positive integers. It explains the impact of word length on integer representation and details various methods for representing signed integers, including sign-magnitude, offset binary, and two's complement. Additionally, it addresses the representation of real numbers through rational, fixed-point, and floating-point formats, highlighting the limitations of exact representation in computers.

Uploaded by

Xtreme Ashmit
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)
15 views125 pages

233 Notes Section1

The document covers binary representation in computer systems, including conversions between decimal, binary, and hexadecimal formats for positive integers. It explains the impact of word length on integer representation and details various methods for representing signed integers, including sign-magnitude, offset binary, and two's complement. Additionally, it addresses the representation of real numbers through rational, fixed-point, and floating-point formats, highlighting the limitations of exact representation in computers.

Uploaded by

Xtreme Ashmit
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/ 125

Computational techniques and computer systems

Module 1: Computer systems


1.2 Binary representation

ENGSCI 233

1
Objectives of 1.2 Binary representation

• Understand and convert between decimal, binary, and hexadecimal


representations of positive integers.
• Understand the impact of positive integer representations on computers
with different word length.
• Understand binary and hexadecimal representations of negative integers.
• Understand the structure of binary oating-point representation.

2
fl
Positive integer decimal representation

• Base 10.
• Digits, dn, can be any of 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.
• Write as (dN dN−1 . . . d2 d1 d0)10, or simply dN dN−1 . . . d2 d1 d0, to
represent
(dN × 10N ) + (dN−1 × 10N−1) + . . . + (d2 × 102) + (d1 × 101) + (d0 × 100)
• For example
4210 = (4 × 101) + (2 × 100)
= (4 × 10) + (2 × 1)
and
172910 = (1 × 103) + (7 × 102) + (2 × 101) + (9 × 100)
= (1 × 1000) + (7 × 100) + (2 × 10) + (9 × 1)
• Most common representation of integers for use by humans.

3
Positive integer binary representation

• Base 2.
• Digits, dn, can be either 0, or 1.
• Binary digits, dn, are called bits.
• Write as dN dN−1 . . . d2 d1 d0 2 (or 0b dN dN−1 . . . d2 d1 d0 in C and
Python) to represent
(dN × 2N ) + (dN−1 × 2N−1) + . . . + (d2 × 22) + (d1 × 21) + (d0 × 20)
• For example, convert integer from binary to decimal:
0b101010 = 1010102
= (1 × 25) + (0 × 24) + (1 × 23) + (0 × 22) + (1 × 21) + (0 × 20)
= (1 × 32) + (0 × 16) + (1 × 8) + (0 × 4) + (1 × 2) + (0 × 1)
= 4210
• Most common representation of positive integers for use by by computers.

4
Convert positive integer from decimal to binary

• To convert a positive integer from base 10 to base 2:


• Let n be the decimal integer;
• Let m = “” be the string representation of the binary integer, initially empty;
• Repeat until n = 0
• r ← rem(n, 2); # returns the integer remainder after dividing n by 2 (will be either 0 or 1)
• n ← div(n, 2); # returns the integer result of dividing n by 2
• m ← string(r) + m; # concatenates the string representation of r (either “0” or “1”) with m

• For example, convert 4210 to binary:


• n = 42, r ← rem(n, 2) = 0, n ← div(n, 2) = 21, m ← string(r) + m = “0”
• n = 21, r ← rem(n, 2) = 1, n ← div(n, 2) = 10, m ← string(r) + m = “10”
• n = 10, r ← rem(n, 2) = 0, n ← div(n, 2) = 5, m ← string(r) + m = “010”
• n = 5, r ← rem(n, 2) = 1, n ← div(n, 2) = 2, m ← string(r) + m = “1010”
• n = 2, r ← rem(n, 2) = 0, n ← div(n, 2) = 1, m ← string(r) + m = “01010”
• n = 1, r ← rem(n, 2) = 1, n ← div(n, 2) = 0, m ← string(r) + m = “101010”

5
Positive integer hexadecimal representation

• Base 16.
• Digits, dn, can be any of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, or f.
• Write as dN dN−1 . . . d2 d1 d0 16 (or 0x dN dN−1 . . . d2 d1 d0 in C and
Python) to represent
(dN × 16N ) + (dN−1 × 16N−1) + . . . + (d2 × 162) + (d1 × 161) + (d0 × 160)
• a16 = 1010, b16 = 1110, c16 = 1210, d16 = 1310, e16 = 1410, f16 = 1510,
• For example
0x2a = 2a16
= (2 × 161) + (a × 20)
= (2 × 16) + (10 × 1)
= 4210
• Hexadecimal is a more compact representation than decimal and binary.

6
Convert positive integer from hexadecimal to binary

• Each hexadecimal digit, dn, can be exactly represented as 4 binary digits


(bits):
016 = 00002 116 = 00012 216 = 00102 316 = 00112
416 = 01002 516 = 01012 616 = 01102 716 = 01112
816 = 10002 916 = 10012 a16 = 10102 b16 = 10112
c16 = 11002 d16 = 11012 e16 = 11102 f16 = 11112
• To convert from hexadecimal to binary, simply replace each hexadecimal
digit with its equivalent 4 bit representation.
• For example
2a16 = 0010 1010
= 001010102
= 1010102

7
Convert positive integer from binary to hexadecimal

• Each hexadecimal digit, dn, can be exactly represented as 4 binary digits


(bits):
016 = 00002 116 = 00012 216 = 00102 316 = 00112
416 = 01002 516 = 01012 616 = 01102 716 = 01112
816 = 10002 916 = 10012 a16 = 10102 b16 = 10112
c16 = 11002 d16 = 11012 e16 = 11102 f16 = 11112
• To convert from binary to hexadecimal, partition binary digits into groups
of 4 (padding leftmost signi cant digits with 0, if required), then replace
each binary group of 4 digits with its hexadecimal equivalent.
• For example
110110000012 = 0110 1100 0001
= 6c116

8
fi
Convert positive integer from hexadecimal to decimal

• For example
0x6c1 = 6c116
= (6 × 162) + (c × 161) + (1 × 160)
= (6 × 256) + (12 × 16) + (1 × 1)
= 172910

9
Convert positive integer from decimal to hexadecimal

• To convert a positive integer from base 10 to base 16:


• Let n be the decimal integer;
• Let m = “” be the string representation of the hexadecimal integer, initially empty;
• Repeat until n = 0
• r ← rem(n, 16); # returns the integer remainder after dividing n by 16 (ranges from 0 to f)
• n ← div(n, 16); # returns the integer result of dividing n by 16
• m ← string(r) + m; # concatenates the string representation of r (from “0” to “f”) with m

• For example, convert 172910 to hexadecimal:


• n = 1729, r ← rem(n, 16) = 1, n ← div(n, 16) = 108, m ← string(r) + m = “1”
• n = 108, r ← rem(n, 16) = c, n ← div(n, 16) = 6, m ← string(r) + m = “c1”
• n = 6, r ← rem(n, 16) = 6, n ← div(n, 16) = 0, m ← string(r) + m = “6c1”

10
Computer word length

• Computers are designed to handle instructions, memory addresses, and


data in a xed-size number of bits, called a word.
• The number of bits in a word, called the word length, varies between
computers.
• The word length in modern computers is typically:

8 bit (1 byte);
1983 Nintendo 8 bit
16 bit (2 byte);
1990 Super Nintendo 16 bit
32 bit (4 byte);

64 bit (8 byte). 1994 PlayStation 32 bit

1996 Nintendo 64 bit

11
fi
Effect of word length on the range of positive integers

• 8 bit range:
from 000000002 = 010
to 111111112 = 25510
• 16 bit range:
from 00000000 000000002 = 010
to 11111111 111111112 = 65,53510
• 32 bit range:
from 00000000 00000000 00000000 000000002 = 010
to 11111111 11111111 11111111 111111112 = 4,294,967,29510
• 64 bit range:
00000000 00000000 00000000 00000000...
from
. . .00000000 00000000 00000000 000000002 = 010
to
11111111 11111111 11111111 11111111...
. . .11111111 11111111 11111111 11111112 = 18,446,744,073,709,551,61510

12
Signed integer binary representation: sign-magnitude

• In sign-magnitude representation, the most Bit pattern Sign-


signi cant bit (the leftmost or sign bit) is 0 for a (8 bit) magnitude
positive number and 1 for a negative number, while 00000000 0
the remaining bits represent the magnitude. 00000001 1
00000010 2
• In an 8 bit sign-magnitude, 7 bits are used to ⋮ ⋮
represent the magnitude (0 to 127), and 1 bit 01111101 125
represents the sign. 01111110 126
• There are two representations of 0 which may be 01111111 127
confusing: 10000000 –0
00000000 → 0 and 10000000 → –0 (for 8 bits). 10000001 –1
10000010 –2
• Addition and subtraction require different ⋮ ⋮
behaviour depending on the value of the sign bit. 11111101 –125
11111110 –126
11111111 –127

13
fi
Signed integer binary representation: offset binary

• In offset binary representation (also called excess-K Bit pattern Eight bit
or biased), a signed integer is represented by the bit (8 bit) excess-128
pattern corresponding to the unsigned number plus 00000000 –128
K, where K is the biasing value or offset. 00000001 –127
00000010 –126
• Thus 0 is represented by K, and −K is represented ⋮ ⋮
by an all-zero bit pattern. 01111101 –3
• There is only one representation of 0: 01111110 –2
10000000 → 0 (for 8 bits). 01111111 –1
10000000 0
10000001 1
10000010 2
⋮ ⋮
11111101 125
11111110 126
11111111 127

14
Signed integer binary representation: two’s complement

• In two’s complement representation, a negative Bit pattern Two’s


number has the bit pattern corresponding to the (8 bit) complement
bitwise NOT (complement) of the positive number, 00000000 0
plus 1. 00000001 1
00000010 2
• Negating a number (whether negative or positive) is ⋮ ⋮
achieved by inverting all the bits and then adding 01111101 125
one to it. 01111110 126
• There is only one representation of 0: 01111111 127
00000000 → 0 (for 8 bits). 10000000 –128
10000001 –127
• Addition and subtraction have the same behaviour
10000010 –126
irrespective of the value of the sign bit. ⋮ ⋮
• Two’s complement is the most widely used 11111101 –3
computer representation of signed integers. 11111110 –2
11111111 –1

15
Counting sheep in 16 bit two’s complement

16
Representation of real numbers

• In general, real numbers cannot be represented exactly by computers.


• Because the set of real numbers is not countable, computers cannot
represent them exactly using a nite number of bits.
• Instead, real numbers are represented approximately as:
• Rational numbers;
• Fixed-point numbers;
• Floating-point numbers.

17
fi
Representation of real numbers as rational numbers

• A rational number is represented as an integer numerator divided by a non-


zero integer denominator.
• Real numbers that are rational will have a decimal expansion that that
either terminates after a nite number of digits
(e.g. 3/4 = 0.750000…),
or eventually repeats the same sequence of digits
(e.g. 6/7 = 0.857142857142857142… where the overline denotes a
repeating sequence).
• There is currently no standard way of storing and sharing rational numbers,
although some computer languages provide built-in support for rational
data types (e.g. Clojure, Julia, Haskell, Ruby).

18
fi
Representation of real numbers as xed-point numbers

• A xed-point number approximates real numbers with a xed number of


digits in the fractional part.
• A binary xed-point number with n binary digits in the fractional part is
usually stored as a binary integer that is implicitly scaled by 2−n.
• Addition and subtraction is exact, following standard rules of integer
addition and subtraction, always retaining results that are correct to n
binary digits.
• Multiplication and division require rescaling of the result.
• Fixed-point numbers are commonly used for digital signal processing in
low-cost microprocessors and microcontrollers that lack oating-point
hardware.
• There is currently no standard way of storing and sharing xed-point
numbers (Ada provides built-in support for xed-point data types).

19
fi
fi
fi
fi
fi
fl
fi
Representation of real numbers as oating-point numbers

• A oating-point number approximates real numbers.


• An international standard (IEEE 754) governs oating point formats and
operations.
• The most commonly used IEEE 754 formats are single precision
(binary32) and double precision (binary64).
• Single precision and/or double precision oating point operations (such as
+, –, ×, and /) are often implemented in hardware on advanced chips.

Sign Exponent (8 bit) Fraction (23 bit)

31 23 0

Sign Exponent (11 bit) Fraction (52 bit)

63 52 0

20
fl
fl
fl
fl
IEEE 754 single precision

• The 8 bit exponent is offset binary, represented as excess-127, which can


range from −127 to 128.
• The 24 bit signi cand is made up of an implied bit 24 followed by 23
fraction bits.
• When the exponent is between −126 and 127:
• The signi cand is assumed to have 1 as its 24th bit followed by the 23 bit fraction.
• The signi cand is equivalent to log10(224) ≈ 7.22 decimal digits.

Sign Exponent (8 bit) Fraction (23 bit)

31 23 0

21
fi
fi
fi
IEEE 754 single precision

• When the exponent is −127 (bits all 0):


• The 24 bit signi cand is assumed to have 0 as its 24th bit followed by the 23 bit fraction.
• Zero is represented by both exponent and fraction bits all zero.
• Denormalised numbers are represented by exponent bits all zero, but some non-zero fraction
bit.

• When the exponent is 128 (bits all 1):


• In nity is represented by the exponent bits all 1, and fraction bits all 0.
• Not an number (NaN) is represented by the exponent bits all 1, and fraction bits not all 0.

Sign Exponent (8 bit) Fraction (23 bit)

31 23 0

22
fi
fi
Next section

• 1.3 Structured data

👍
23
Image references

• Slide 11: https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/NES-Console-Set.jpg/1600px-NES-


Console-Set.jpg?20201231095234
• Slide 11: https://commons.wikimedia.org/wiki/File:SNES-Mod1-Console-Set.jpg
• Slide 11: https://commons.wikimedia.org/wiki/File:PSX-Console-wController.jpg
• Slide 11: https://commons.wikimedia.org/wiki/File:Nintendo-64-wController-L.jpg
• Slide 16: https://xkcd.com/571/

24
Computational techniques and computer systems
Module 1: Computer systems
1.3 Structured data

ENGSCI 233

1
Objectives of 1.3 Structured data

• Understand role of endianness in data storage.


• Understand how text data are represented in computers.
• Know how to handle situations where other forms of data need to be
stored.

2
Which way does a computer read?

• Two possible ways that a computer can number bytes in larger groups:
• Little-endian:
• Stores least signi cant byte at the smallest memory address.
• Used in most PCs.

• Big-endian:
• Stores most signi cant byte at the smallest memory address.
• Used in mainframes and network equipment.

Little endian Big-endian

https://commons.wikimedia.org/wiki/File:Little-Endian.svg
https://commons.wikimedia.org/wiki/File:Big-Endian.svg

3
fi
fi
Which way does a computer read?

• Your code is not usually affected by whether your computer is little-endian


or big-endian because programming languages do a good job of hiding this
detail.
• Endianness is important when you move data from one computer to
another (e.g. by network or memory stick).

https://geek-and-poke.com/geekandpoke/2011/9/7/simply-explained.html

4
Binary

• Computer data are based on bits.


• A bit can have one of two values, usually denoted by “0” or “1”.
• One bit can thus assume 2 distinct states {0, 1}.
• Two bits can assume 4 distinct states {00, 01, 10, 11}.
• Three bits can assume 8 distinct states {000, 001, 010, 011, 100, 101, 110,
111}.
• In general n bits can assume 2n distinct states.

5
Character representation

• Character data can be represented in binary code.


• Text can be represented as strings of characters.
• There are several standard character encodings that enable character data to
be unambiguously stored and shared between different computers:
• US-ASCII
• Unicode

6
Character representation: US-ASCII

• US-ASCII is an old standard 7 bit encoding of characters.


• Of the possible 128 = 27 distinct characters:
• 95 are printable characters (e.g. 'A' to 'Z', 'a' to 'z', '0' to '9')
• 33 are control characters (e.g. LF (line feed), BS (backspace))

• e.g. The string "Bye!" is represented in 7 bit US-ASCII as


1000010 1111001 1100101 0100001

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
000 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI
001 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
010 ! “ # $ % & ‘ ( ) * + , - . /
011 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
100 @ A B C D E F G H I J K L M N O
101 P Q R S T U V W X Y Z [ \ ] ^ _
110 ` a b c d e f g h I j k l m n o
111 p q r s t u v w x y z { | } ~ DEL

7
The world is much richer than just ASCII

8
Character representation: Unicode

• Unicode is a set of standard encodings of characters.


• Each Unicode character has a unique code point denoted as U+0000
through U+10FFFF (e.g. U+00F7 maps to the division sign “÷”).
• It currently represents all 149,186 characters maintained by the Unicode
Consortium covering 161 modern languages, symbols, emoji, as well as
non-visual control and formatting codes.
• There are several standard mappings from each code point to standard code
units, the most common being UTF-8, UTF-16, UTF-32.
• UTF-8, the most widely used encoding, uses one to four bytes for each
character.
• The rst 128 characters of UTF-8 correspond with US-ASCII.
• UTF-8 is supported by all major operating systems.

9
fi
How are Unicode characters stored?

• UTF-8 uses one, two, three, or four 8-bit code unit per code point.
• UTF-16 uses one or two 16-bit code units per code point.
• UTF-32 uses one 32-bit code unit per code point.

Encoding Bits A
☕ 💩
Unicode code
U+0041 U+2615 U+1F4A9
point

UTF-8 8, 16, 24, or 32 0x41 0xE2 98 95 0xF0 9F 92 A9

UTF-16 16 or 32 0x0041 0x2615 0xD83D DCA9

UTF-32 32 0x00000041 0x00002615 0x0001F4A9

10
Some entities use more than one Unicode character

• e.g. ZWJ (zero width joiner) is the Unicode character (U+200D) that joins
two or more characters together in sequence to create a new emoji.

🏳🌈 = 🏳 + ZWJ + 🌈
👪 = 👨 + ZWJ + 👨 + ZWJ + 👧
👩🏭 = 👧 + ZWJ + + ZWJ + 🏭

11
What does this mean for you?

• ASCII strings are always 1 byte per character.


• UTF-8 and UTF-16 have a variable character size.
• How do you gure out the length of a string in memory?
• Python cheats and uses the smallest constant-size representation that will
work…
• You can’t assume that displayed characters and numbers are
interchangeable any more!
• You can’t predict required display space from string length.

12
fi
What other types of data need to be represented?

• Use a standard to maintain compatibility with others!


• Files often contain a magic number or signature:
• PNG image les begin with a an 8 byte signature that includes the ASCII string “PNG” plus
information about the image encoding;
• PDF les begin with a header that includes the ASCII string “%PDF-” plus information
about the version of the format and data types encoded in the le.

• Metadata is usually stored in the same le as the data.


Data type Standard Link

Portable network graphics


Images http://www.libpng.org/pub/png/
(ISO/IEC 15948:2004)
Portable Document Format https://www.iso.org/standard/
Formatted text
(ISO 32000-2:2020) 75839.html
https://learn.microsoft.com/en-
Windows executable code Portable executable us/windows/win32/debug/pe-
format

13
fi
fi
fi
fi
What about pointers?

• Don’t keep pointers around for posterity!


• A pointer has the size in memory dictated by the computer architecture so
won’t be generally portable across platforms.

1983 Nintendo 8 bit

1990 Super Nintendo 16 bit

1994 PlayStation 32 bit

1996 Nintendo 64 bit

14
Next section

• 1.4 Computer architecture

15
Image references

• Slide 3: https://commons.wikimedia.org/wiki/File:Little-Endian.svg
• Slide 3: https://commons.wikimedia.org/wiki/File:Big-Endian.svg
• Slide 4: https://geek-and-poke.com/geekandpoke/2011/9/7/simply-explained.html
• Slide 8: https://thenextweb.com/news/the-psychology-of-emojis
• Slide 8: https://preview.free3d.com/img/2017/10/2269153662355900306/nt44jac9.jpg
• Slide 8: https://www.google.com/url?sa=i&url=https://www.pinterest.com/pin/the-rosetta-
stone--522136150556126123/&psig=AOvVaw17J_ud965tgK1U0SU-
daza&ust=1678854504369000&source=images&cd=vfe&ved=0CBAQjRxqFwoTCPChh8XK2v0CFQAAAAAd
AAAAABAE
• Slide 8: https://cdn.pixabay.com/photo/2020/03/20/15/37/maya-script-4951005_1280.png
• Slide 14: https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/NES-Console-Set.jpg/1600px-NES-
Console-Set.jpg?20201231095234
• Slide 14: https://commons.wikimedia.org/wiki/File:SNES-Mod1-Console-Set.jpg
• Slide 14: https://commons.wikimedia.org/wiki/File:PSX-Console-wController.jpg
• Slide 14: https://commons.wikimedia.org/wiki/File:Nintendo-64-wController-L.jpg
• Slide 15: In ection, by Randall Munroe, from https://xkcd.com/1709/

16
fl
Computational techniques and computer systems
Module 1: Computer systems
1.4 Computer architecture

ENGSCI 233

1
Objectives of 1.4 Computer architecture

• Understand and demonstrate breaking a process into computable steps.


• Understand the relationship between programming languages, assembly
code, and machine code.
• Understand the parts of a computer.
• Understand processor performance characteristics.
• Understand multi-processor systems.

2
What is a computer?

3
What is a computer? Human computers?

Computing Division of the US Veterans Bureau, 1924; NASA Ames “computers” at wind tunnel test facility, 1943

4
Computers have four parts

Processor Data transfer

Memory Input/output

5
Computers have four parts

• Several parts could be on the same silicon chip.

6
Computer systems add software

• Operating system:
• Sets standards for access to storage, transport, and I/O;
• Manages multiple programs running together.

• Drivers:
• Provide interface between hardware and software.

• We will discuss this in greater depth in a later section.

7
How do programs run on computers?

• Interpreter:
• A computer program that directly executes instructions written in a programming or scripting
language.

Python Matlab Ruby Julia

• Compiler:
• A computer program that translates instructions written in a programming language into an
executable.

C Fortran Ada Rust

8
What does code do?

• The following code is written in C.


• How do we compile this to run on an ARM microcontroller?
void Commutate(uint16_t *A, uint16_t *B, uint16_t *C, ...
int16_t Command, uint16_t Position)
{
if(Command > PWM_IDLE)
{
Command = PWM_IDLE;
}
else if(Command < -PWM_IDLE)
{
Command = -PWM_IDLE;
}
*A = 0;
*B = PWM_IDLE + Command;
*C = PWM_IDLE - Command;
}

9
Assembly language is dif cult to understand

• How do we compile this to run on an ARM microcontroller?


• C code is black, machine code blue, assembly code green, and comments red.
• [n] denotes memory address n.
*B = PWM_IDLE + Command;
08005050: 887b ldrh r3, [r7, #2] @ load to r3 from [r7 + 210]
08005052: f603 0334 addw r3, r3, #2100 @ to r3 add r3 and 210010
08005056: b29a uxth r2, r3 @ to r2 copy r3
08005058: 68bb ldr r3, [r7, #8] @ load to r3 from [r7 + 810]
0800505a: 801a strh r2, [r3, #0] @ store from r2 to [r3]
*C = PWM_IDLE - Command;
0800505c: 887b ldrh r3, [r7, #2] @ load to r3 from [r7 + 210]
0800505e: f5c3 6303 rsb r3, r3, #2096 @ to r3 subtract r3 from 209610
08005062: 3304 adds r3, #4 @ to r3 add r3 and 410
08005064: b29a uxth r2, r3 @ to r2 copy r3
08005066: 687b ldr r3, [r7, #4] @ load to r3 from [r7 + 410]
08005068: 801a strh r2, [r3, #0] @ store from r2 to [r3]

10
fi
What does the processing?

• Semiconductor chips.
• May use many computing units in parallel on one or many separate chips.

M2 Max system on a chip, 12 core CPU, 38 core GPU, 64 bit ARMv8 instruction set, 67 billion transistors.

11
Processor performance

• How many bits {8, 16, 32, 64}?


• How many instructions per second?
• What kinds of instructions?
• Reduced instruction set computer (RISC), e.g. ARM, PIC:
• One clock cycle per instruction;
• Uniform instruction format;
• All registers can be used for source and destination of instructions;
• Simple addressing modes;
• Few data types.

• Complex instruction set computer (CISC), e.g. Intel/AMD/Cyrix/IBM x86:


• One or more clock cycles per instruction;
• Nonuniform instruction format that may occupy variable number of bits;
• Both registers and memory locations may be used for source and destination of instructions;
• Mixture of addressing modes;
• Many data types.

12
Processor performance

• Modern high-end systems on a chip integrate:


• multiple specialised processors (CPU, GPU);
• memory (multiple levels of cache, RAM);
• high-speed bus;
• input/output.

• Integration to smaller scales improves ef ciency and speed by reducing


power consumption per transistor and communication path lengths.
• Graphics processing units (GPUs) are optimised for computer graphics and
image processing:
• Typically have many processors/computing units;
• Typically use a simple instruction set;
• 3D graphics requires fast mathematics;
• Many computations can be performed in parallel, achieving computational speeds of
>10 TFLOPS (1013 oating point operations per second);

13
fl
fi
50 years of microprocessor trend data
50 Years of Microprocessor Trend Data
Transistors
(thousands)
107

106

Single-thread
105 performance
(SpecINT×103)

104
Frequency
(MHz)
103
Typical power
(W)
102
Number of
cores

101

100

1970 1980 1990 2000 2010 2020

Original data up to the year 2010 collected and plotted by M. Horowitz, F. Labonte, O. Shacham, K. Olukotun, L. Hammond, and C. Batten
Year
New plot and data collected for 2010-2021 by K. Rupp <https://github.com/karlrupp/microprocessor-trend-data>
Original data up to the year 2010 collected and plotted by M. Horowitz, F. Labonte, O. Shacham, K. Olukotun, L. Hammond, and C. Batten
New plot and data collected for 2010-2021 by K. Rupp 14
Supercomputers

• Supercomputers have very high performance compared to general purpose


computers.
• Typically constructed from thousands of CPUs and GPUs.
• Millions of cores.
• Used for computationally intensive tasks, including: quantum mechanics,
weather forecasting, climate research, oil and gas exploration, molecular
modeling, and physical simulations.
• Typically use Linux operating system.
• Parallel architectures typically require specialised programming techniques
to exploit their speed.
• Often use standard interfaces to distributed processing tools such as
Message Passing Interface (MPI) and Parallel Virtual Machine (PVM).

15
Supercomputers

• Fastest supercomputer at 2024 November is El Capitan, an HPE Cray


EX255a at Lawrence Livermore National Laboratory, United States.
• 1,051,392 cores (43,808 × 24-core), 5.4375 petabyte (5.4375 × 1015 byte)
memory.
• Peak oating point performance of 2.746 exaFLOPS (2.746 × 1018 ops).
• 30 MW power.

16
fl
Supercomputers

Moore’s law

17
Future?

• System on a chip performance gains will be obtained by increasing the


number of (specialised) cores.
• Careful management of very high speed memory accessible to multiple
cores will continue to be crucial.
• Computer languages will need to evolve to free the programmer from
explicit control (especially low-level constructs) of parallelism, leaving
compilers to automatically infer opportunities for parallel computation
from the structure of the code.
• Pure functional languages ( rst-class functions, higher-order functions, and
immutable data) simplify automatic identi cation of parallel computation
because purely functional parts of the computation never interact.

18
fi
fi
Next section

• 1.5 Storage and transport

19
Image references

• Slide 3: https://static.scienti camerican.com/sciam/cache/ le/A057E419-


FAE5-4072-9BF11CAE1A8A5206_source.jpg?w=590&h=800&F2EC7DFC-756B-4CF0-AA375254329DED62
• Slide 3: https://store.storeimages.cdn-apple.com/8756/as-images.apple.com/is/refurb-mbp14-space-m1-2021?
wid=1144&hei=1144&fmt=jpeg&qlt=90&.v=1638575247000
• Slide 3: https://store.storeimages.cdn-apple.com/8756/as-images.apple.com/is/iphone-14-pro- nish-
select-202209-6-7inch-gold?wid=5120&hei=2880&fmt=p-jpg&qlt=80&.v=1663703841907
• Slide 3: https://media.parallax.com/wp-content/uploads/2020/07/28151203/32701a.jpg
• Slide 4: https://kartsci.org/wp-content/uploads/2018/10/US-Veterans-Bureau-Computing-
Division-1924-768x623.jpg
• Slide 4: https://pbs.twimg.com/media/ELiNxXNU8AAOgmj.jpg
• Slide 5: https://www.apple.com/newsroom/images/product/mac/standard/Apple-M2-chips-M2-
Max-230117_big.jpg.large_2x.jpg
• Slide 5: https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Electronic_Memory.jpg/1478px-
Electronic_Memory.jpg?20080229140357
• Slide 5: https://www.backblaze.com/blog/wp-content/uploads/2016/03/USB_Type-C_macbook.png
• Slide 5: https://www.apple.com/newsroom/images/product/mac/standard/Apple-Studio-Display-Magic-Trackpad-
Keyboard-Mouse-220308_big.jpg.large_2x.jpg
• Slide 6: https://web.archive.org/web/20140414084027/http://blog.ioactive.com/2007_11_01_archive.html
• Slide 8: https://upload.wikimedia.org/wikipedia/commons/thumb/c/c3/Python-logo-notext.svg/242px-Python-logo-
notext.svg.png

20
fi
fi
fi
Image references

• Slide 8: https://upload.wikimedia.org/wikipedia/commons/2/21/Matlab_Logo.png
• Slide 8: https://upload.wikimedia.org/wikipedia/commons/thumb/7/73/Ruby_logo.svg/1280px-Ruby_logo.svg.png
• Slide 8: https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/Julia_Programming_Language_Logo.svg/
1920px-Julia_Programming_Language_Logo.svg.png
• Slide 8: https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/The_C_Programming_Language_logo.svg/
1280px-The_C_Programming_Language_logo.svg.png
• Slide 8: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b8/Fortran_logo.svg/1280px-Fortran_logo.svg.png
• Slide 8: https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Ada_horizon_green_logo_with_slogan.svg/
2560px-Ada_horizon_green_logo_with_slogan.svg.png
• Slide 8: https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/Rust_programming_language_black_logo.svg/
1280px-Rust_programming_language_black_logo.svg.png
• Slide 11: https://www.apple.com/newsroom/images/product/mac/standard/Apple-M2-chips-M2-
Max-230117_big.jpg.large_2x.jpg
• Slide 14: Original data up to the year 2010 collected and plotted by M. Horowitz, F. Labonte, O. Shacham, K.
Olukotun, L. Hammond, and C. Batten. New plot and data collected for 2010-2021 by K. Rupp <https://github.com/
karlrupp/microprocessor-trend-data>
• Slide 16: https://www.geekwire.com/2019/cray-develop-insanely-fast-600m-el-capitan-supercomputer-u-s-nuclear-
simulations/
• Slide 17: https://upload.wikimedia.org/wikipedia/commons/thumb/b/b8/Supercomputing-rmax-graph2.svg/660px-
Supercomputing-rmax-graph2.svg.png

21
Computational techniques and computer systems
Module 1: Computer systems
1.6 Low level networking

ENGSCI 233

1
Objectives of 1.6 Low level networking

• Understand the physical means used to transfer data from one computer to
another.
• Discuss different ways that information can be encoded.
• Understand the use of error-correcting codes.
• Describe the key characteristics of ethernet.

2
How does computer networking operate?

• How, does that cat video/news/


electrocardiograph/etc. get to your
device?
• Where does it come from?
• How can you see it at home? On a
train? At the top of a mountain?
• How many engineers spent their
lives guring this out?

3
fi
Computer networking is layered (like an onion)

Layer Purpose Activity

Group of applications requiring network Generates data and requests


Application
communications connections

Establishes the connection between applications Establishes connections with


Transport
on different devices remote host

Responsible for creating packets that move across Transfers packets with virtual (IP)
Network
the network addresses

Responsible for creating frames that move across Transfers frames with physical
Data link
the network (MAC) addresses

Physical Tranceiver that drives the signals on the network Transmits and receives bits

e.g. 5 layer TCP/IP model (7 layer OSI Model is more detailed)

4
Physical layer

• The physical layer speci es the


physical characteristics of the
communications media (e.g. for
electrical, optical, and radio media).
• We need change in a physical
medium to communicate.
• Some changes are simple
(like on/off).
• Some are subtle
(like the shape of a smoke signal…).

• There are limits to how fast we can


change
(thus determining bandwidth).

5
fi
Physical layer: electrical wire

• The physical layer describes electrical hardware standards for ethernet


(IEEE 802.3) and standard pin connectors (e.g. RS-232).
• Uses the same electrical signals as internal data buses.
• Physically robust.
• Limited bandwidth.
• Capacitance/inductance smear
out signals.

• Limited length.
• Signals get weaker.
• Electrical interference.

6
Physical layer: optical bre

• Requires optical/electrical conversion.


• Physically fragile.
• Extremely high bandwidth.
• Limited by the electronics that generate signals (e.g. LED or diode laser) and detect signals
(e.g. photodiodes), as well as characteristics of the optical bre (e.g. dispersion).

• Long lengths.
• Tens of kilometres.
• Across oceans with ampli ers…

7
fi
fi
fi
Physical layer: radio

• The physical layer describes electromagnetic hardware standards for


wireless Wi-Fi (IEEE 802.11), and Bluetooth (IEEE 802.15.1).
• Versatile, requiring no mechanical connection.
• Pass through walls, trees, people, etc.
• Lose power rapidly with distance.
1
• Inverse square law, r 2

• Broadcast to everyone!
• Serious interference possible.

• Increasing frequency:
• Higher bandwidth;
• Shorter range;
• Greater attenuation by walls, etc.

8
Multiplexing: Sharing a medium

• Time division multiplexing (TDM):


• Transmit and receive independent signals over a common
signal path by synchronising switches at each end of the
transmission line.

• Frequency division multiplexing (FDM):


• Total bandwidth available in a communication medium is
divided into a series of non-overlapping frequency bands, each
of which is used to carry a separate signal.

• Code division multiplexing (CDM):


• Several channels simultaneously share the same spectral
bandwidth which is much higher than the bit rate or symbol
rate. One form is frequency hopping. Another form is direct
sequence spread spectrum where each channel transmits its
bits as a coded channel-speci c sequence of pulses called
chips.

• Wavelength division multiplexing (WDM):


• Multiplexes several optical/radio carrier signals onto a single
medium by using different wavelengths, enabling bidirectional
communications over a single strand of ber

9
fi
fi
Modulation: representing data in the medium

• Various modulation techniques Time


enable digital signals to be
represented on a carrier signal in an Digital
t

1 0 1 0 modulating
analogue medium. signal

• In amplitude-shift keying (ASK) a


Carrier
nite number of amplitudes are signal
used.
• In frequency-shift keying (FSK) a ASK

nite number of frequencies are


used.
FSK
• In phase-shift keying (PSK) a nite
number of phases are used.
PSK
• Can combine above modulations to
represent more than two states.

10
fi
fi
fi
Data link layer

• The data link layer handles how data is transferred between devices on the
network, and may provide mechanisms to detect and possibly correct errors
that occur in the physical layer.
• Data are encapsulated in frames, enabling transmission between two nodes
connected by the physical layer.

11
Data link layer frame

• To transmit data, we need a standard way to represent information in


frames (speci ed in IEEE 802.3).
• An ethernet frame has the following structure:
• Destination MAC address (6 byte);
• Source MAC address (6 byte);
• Ethernet type (2 byte) indicates size in bytes of, or protocol used, in the payload;
• Payload/data (46 byte to 1500 byte) typically a packet from the network layer;
• CRC checksum (4 byte).

• Errors are detected (but not corrected) using a 32-bit (4 byte) cyclic
redundancy check (CRC) or checksum.
0 4 8 12 16 20 24 28 31 bit
Destination MAC address
Destination MAC address Source MAC address
14 byte
Source MAC address
Ethernet type
46 byte to
Data
1500 byte
Checksum 4 byte

12
fi
Data link layer address

• To establish a data link, we need a standard way to address which devices


we wish to communicate with.
• All network devices have unique 48 bit (6 bytes or 12 hex digits) media
access control (MAC) addresses for their communication ports (ethernet,
bluetooth, WiFi,…).
• Ethernet networks are switched:
• The local switch knows the MAC addresses of devices attached to each of its ports.
• Packets are sent only to the device expecting them.

13
Error detecting codes

• Interference and noise can cause incorrect transmission and/or data loss.
• Error detecting codes use redundancy to detect transmission errors.
• A cyclic redundancy check (CRC) is widely used to detect errors in digital
data.
• CRC adds a xed-length check value to data before transmission.
• The check value is based on a simple algorithm applied to the data being sent.
• After transmission the check value is recalculated and compared with the CRC attached
before transmission.
• If the check values don’t match, corrective action must be taken (e.g. retransmission).

• Simplest CRC is parity bit (number of 1s in the data) which can only detect
a 1 bit error.
• 2-bit and higher CRCs can detect more than single bit errors.

14
fi
Error correcting codes

• Error correcting codes use Triplet received Interpreted as


redundancy to both detect and x
transmission errors. 000 0 (error free)

• e.g. triple redundancy. 001 0


• 0 = 000; 1 = 111
• Any 1-bit error can be corrected. 010 0
• Only have 1/3 of throughput.
100 0
• More complicated error correcting
codes require less redundancy. 111 1 (error free)

• Need to avoid long strings of zeros 101 1


or ones to keep track of bit position.
110 1

011 1

15
fi
Next section

• 1.7 High level networking

16
Image references

• Slide 2: Wi vs Cellular, by Randall Munroe, from https://xkcd.com/1865/ (CC BY-NC 2.5)


• Slide 3: Neil Drumm, https://www. ickr.com/photos/drumm/2989736147 (CC BY-SA 2.0)
• Slide 4: Adapted from Microchip Technology, from https://microchipdeveloper.com/tcpip:tcp-ip- ve-layer-model
• Slide 5: Swaf emeister, from https://old.reddit.com/r/comics/comments/95an87/oc_smoke_signals/
• Slide 6: https://cdn11.bigcommerce.com/s-sp9oc95xrw/images/stencil/1280x1280/products/1375/9773/cable-
patch-cord-1m-ayoubcomputers__41253.1590582737.jpg?c=2
• Slide 7: Hustvedt, from https://commons.wikimedia.org/wiki/File:Fiber_optic_illuminated.jpg (CC BY-SA 3.0)
• Slide 8: CST – Computer Simulation Technology, from https://commons.wikimedia.org/wiki/
File:Printed_Inverted-F_Antenna_E- eld.gif (CC BY-SA 4.0)
• Slide 9: https://upload.wikimedia.org/wikipedia/commons/e/e0/Telephony_multiplexer_system.gif
• Slide 9: https://upload.wikimedia.org/wikipedia/commons/thumb/f/f1/Frequenzmultiplex001.svg/800px-
Frequenzmultiplex001.svg.png
• Slide 9: https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Multiplexing_diagram.svg/300px-
Multiplexing_diagram.svg.png
• Slide 9: https://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/WDM_operating_principle.svg/800px-
WDM_operating_principle.svg.png
• Slide 12: https://upload.wikimedia.org/wikipedia/commons/7/77/UMTS_Router_Surf@home_II,_o2-0017.jpg
• Slide 13: by Mikm, from https://commons.wikimedia.org/wiki/File:Ethernet_Type_II_Frame_format.svg (Public
domain)

17
fi
fl
fl
fi
fi
Computational techniques and computer systems
Module 1: Computer systems
1.7 High level networking

ENGSCI 233

1
Objectives of 1.7 High level networking

• Understand how the Internet works


(at a very basic level).
• Discuss ways to manage network
congestion.
• Discuss ways to manage network
reliability.

2
Computer networking is layered (like an onion)

Layer Purpose Activity

Group of applications requiring network Generates data and requests


Application
communications connections

Establishes the connection between applications Establishes connections with


Transport
on different devices remote host

Responsible for creating packets that move across Transfers packets with virtual (IP)
Network
the network addresses

Responsible for creating frames that move across Transfers frames with physical
Data link
the network (MAC) addresses

Physical Tranceiver that drives the signals on the network Transmits and receives bits

e.g. 5 layer TCP/IP model (7 layer OSI Model is more detailed)

3
Network layer

• The network layer implements the internet protocol (IP) for delivering
packets across the network from the source to destination based solely on
IP addresses in the packet headers.
• The IP acts as a bridge between networks:
• Assigns globally unique IP addresses;
• Allows packets to be split up and reassembled;
• Discards old or stuck packets.

• IPv4 and IPv6 are the two most widely used versions of IP.

4
IPv4

• IPv4 is the dominant internet protocol.


• 32 bit address gives ∼ 4.3 × 109 unique addresses.
• However, there are already more than 232 = 4,294,967,296 computers, so IPv6 was
created to accommodate more addresses.
• IPv4 addresses are usually written in dot-decimal or hexadecimal notation:
172 . 16 . 254 . 1 Dot-decimal
ac 10 fe 1 Hexadecimal
• 10101100 00010000 11111110 00000001 Binary

• IPv4 packet has 20 byte to 60 byte header:


• Version is always 4 for IPv4 packets;
• Total length is size of packet in bytes (≤ 65,535); 0 4 8 12 16 20 24 28 31 bit
Version IHL Type of service Total length
• Identi cation and fragment offset deal with Identification Flags Fragment offset
fragmentation of data into packets; TTL Protocol Header checksum
20 byte
Source address
• Time to live (TTL) is maximum router hops Destination address

before abandoning transmission; Options


0 byte to
40 byte
• 2 byte header checksum error checks header. Data
0 byte to
65515 byte

5
fi
IPv4

• Some IPv4 addresses are reserved for special use:


• for communications in local private network:
10.0.0.0 to 10.255.255.255 (16,777,216 addresses);
172.16.0.0 to 172.31.255.255 (1,048,576 addresses);
192.0.0.0 to 192.0.255.255 (65,536 addresses).
• for multicast to many devices:
224.0.0.0 to 239.255.255.255 (268,435,456 addresses).
• for loopback to local device:
127.0.0.0 to 127.255.255.255 (16,777,216 addresses).

6
IPv6

• IPv6 has increasing public deployment since 2006.


• 128 bit address gives ∼ 3.4 × 1038 unique addresses.
• IPv6 ddresses are usually written in 8 groups of 4 hexadecimal digits
separated by colons:
2001 : 0db8 : 0000 : 0000 : 0000 : ff00 : 0042 : 8329 8 groups of hexadectets
• 2001 : db8 : 0 : 0 : 0 : ff00 : 42 : 8329 Shortened form
• IPv6 packet has 40 byte header:
• Version is always 6 for IPv6 packets; 0 4 8 12 16 20 24 28 31 bit
Version Traffic class Flow label
• Flow label is used for special handling of Payload length Next header Hop limit
packets (e.g. real time requirements);
Source address
• Payload length is size of payload in bytes; 40 byte

• Hop limit is maximum router hops


Destination address
before being discarded;
• Addresses are all 128 bit IPv6. Payload 0 byte to
65535 byte

7
Routing

• A router is a device that forwards packets between devices on a network.


• Routers are connected to two or more data lines from different IP networks.
• Networks comprise many routers connected by data lines.
• Routers use address resolution protocol (ARP) to map network layer IP
addresses (IPv4 or IPv6) to data link layer physical addresses (MAC).
• IPv6 uses neighbor discovery protocol (NDP) to discover con gurations of local devices.

• When a packet arrives on one data line, the router uses information in its
routing table to send the packet to the next network
• Packets are forwarded from their source, through routers on the network,
until they reach their destination.
• High-level protocols discover the best route using methods such as
Dijkstra’s algorithm.

fi
Transport layer

• The transport layer handles the Port


Assignment
channels that applications use to number
exchange data. 20, 21 File transfer protocol (FTP)
22 Secure shell (SSH) secure login
• It provides the end-to-end message 23 Telnet remote login service
transfers between application ports 25 Simple mail transfer protocol (SMTP)
that are independent of the 53 Domain name system (DNS) service
underlying network, informing the 67, 68 Dynamic host con guration protocol
destination device how to combine 80 Hypertext transfer protocol (HTTP)
packets into useful data. 110 Post of ce protocol (POP3)
119 Network news transfer protocol
• Two major protocols are 123 (NNTP)
Network Time Protocol (NTP)
implemented in the transport layer: 143 Internet Message Access Protocol
• User datagram protocol (UDP); 161 (IMAP)
Simple Network Management Protocol
194 (SNMP)
Internet Relay Chat (IRC)
• Transmission control protocol (TCP).
443 HTTP Secure (HTTPS)
546, 547 DHCPv6 IPv6 version of DHCP

9
fi
fi
Transport layer user datagram protocol (UDP)

• UDP is a connectionless protocol where applications can interact with each


other by simple messaging of datagrams.
• It does not provide any guarantees of ordered and reliable delivery, error-
free communication (checksum is optional in IPv4), or ow control.
• UDP is typically used to transport small quantities of data (e.g. streaming
audio or video) because it has low overheads associated with establishing
and validating connections.
• A UDP datagram has an 8 byte header:
• Source port and destination port (each 16 bit);
• Length speci es the length of the header plus data (16 bit);
• Checksum for detecting errors in header and data (16 bit).
0 4 8 12 16 20 24 28 31 bit
Source port Destination port
Length Checksum 8 byte

8 byte to
Data
65507 byte

10
fi
fl
Transport layer transmission control protocol (TCP)

• TCP is a connection-oriented protocol where applications can interact with


each other as if they were physically connected.
• It provides a reliable, ordered, and error-checked delivery of a stream of
data between applications on devices communicating over an IP network.
• Data are transmitted as segments in a way that mimics character by
character transmission, rather than being seen by applications as separate
packets:
• Connection requests and acknowledgements are exchanged to establish a connection
between ports;
• Data are sent in byte order (disassembled into segments for transmission at the source, and
reassembled from segments at the destination);
• Once all data has been received, the connection is closed.

11
Transport layer transmission control protocol (TCP)

• TCP ensures that:


• Data arrives in order;
• Data is free of errors;
• Duplicate data is discarded;
• Lost or discarded packets are resent;
• Traf c congestion of packets is controlled.

• A TCP segment header is 20 byte to 60 byte comprising:


• Source port and destination port (each 16 bit);
• Data offset speci es the size of the TCP header in bytes;
0 4 8 12 16 20 24 28 31 bit
• Flags used to control how connections Source port Destination port
are handled (CRW, ECE, URG, ACK, Sequence number
Acknowledgement number (if ACK sent)
PSH, RST, SYN, FIN); Offset Flags Window size
20 byte

Checksum Urgent pointer (if URG set)


• Checksum for error checking the segment. Options (if Offset > 5)
0 byte to
40 byte
0 byte to
Data
65515 byte

12
fi
fi
Application layer

• The application layer enables applications or processes to communicate


data across a computer network to other applications on another or the
same host.
• It de nes standard internet services and network applications, including:
• Hypertext documents: Hypertext Transfer Protocol (HTTP) World Wide Web hypermedia.
• Remote login to hosts: Telnet, Secure Shell.
• File transfer: File Transfer Protocol (FTP), Trivial File Transfer Protocol (TFTP),
BitTorrent.
• Electronic mail transport: Simple Mail Transfer Protocol (SMTP), Internet Message Access
Protocol (IMAP), Post Of ce Protocol (POP)
• Remote host management: Simple Network Management Protocol (SNMP).
• Assigning IP addresses to network devices: Dynamic Host Con guration Protocol (DHCP).
• Naming system for network devices: Domain Name System (DNS).
• Clock synchronisation: Network Time Protocol (NTP).

13
fi
fi
fi
Tying the layers together

• The layered structure of computer networks speci es how data should be


packeted, addressed, transmitted, routed, and received for end-to-end data
communication.
• Each layer has its set of set of well-de ned protocols:

Layer Encapsulation Address Standard protocol examples

Application Protocol de ned Port HTTP, FTP, SMTP, DNS, NTP, …

Transport Datagram/Segment Port UDP, TCP, …

Network Packet IPv4, IPv6 IPv4, IPv6, …

Data link Frame MAC Ethernet, Wireless LAN, USB, …

Physical Protocol de ned Ethernet, Wireless LAN, USB, …

14
fi
fi
fi
fi
Tying the layers together

• Example of data ow and encapsulation in a simple network of two


devices connected by a bre link between their respective routers.
• Once the link is established, communication details are hidden from the
two processes.

Network topology

Device 1 Router 1 Router 2 Device 2

Data flow Data encapsulation


Application Process to process Application Data

Transport Host to host Transport UDP header UDP Data

Network Network Network Network IP header IP data

Data link Data link Data link Data link Frame header Frame data Frame footer

Physical (ethernet) Physical (fibre) Physical (ethernet)

15
fl
fi
Next section

• 1.8 Software systems

16
Image references

• Slide 2: Map of the Internet, by Randall Munroe, from https://xkcd.com/195/ (CC BY-NC 2.5)
• Slide 3: Adapted from Neil Drumm, https://www. ickr.com/photos/drumm/2989736147 (CC BY-SA 2.0)
• Slide 15: Adapted from https://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/IP_stack_connections.svg/
1024px-IP_stack_connections.svg.png
• Slide 15: Adapted from https://upload.wikimedia.org/wikipedia/commons/thumb/3/3b/UDP_encapsulation.svg/
700px-UDP_encapsulation.svg.png

17
fl
Computational techniques and computer systems
Module 1: Computer systems
1.8 Software systems

ENGSCI 233

1
Objectives of 1.8 Software systems

• Understand the software


components of a computer system.
• Describe the main forms of
memory management used in
computer systems.
• Understand how APIs are used to
access device drivers, operating
systems, and other people’s
applications.

2
Computer software is layered

Layer Purpose

Application Software that actually accomplishes your task

Operating system Software that manages the operation of other software in a computer system

Device drivers Software that provides a common interface to rmware/hardware

Firmware Software that runs on dedicated, low-performance processors

Hardware

e.g. 5 layer TCP/IP model (7 layer OSI Model is more detailed)

3
fi
System design is exible

• Not all computer systems need all of the levels.


• Simple embedded systems have only rmware and hardware.
• Computer systems can be made up of many other, smaller, computer
systems.
• e.g. “the cloud”.

• One set of hardware can run several computer systems at once.


• Virtualization.

4
fl
fi
How does software use hardware?

• Memory is layered according to speed and cost.


Memory

Registers Cache Primary Secondary


CPU instruction operable buffers primary accesses CPU directly accessible CPU indirectly accessible

Drive
RAM ROM hard disk or solid state
random access memory read only memory
CD / DVD
Static RAM PROM
faster, low power programmable ROM
USB flash drive
Dynamic RAM EPROM
slower, needs refreshing erasable PROM
Tape
EEPROM
electrically EPROM

• Random access memory (RAM) is typically used 3 ways:


• Static memory allocation;
• Stack-based memory allocation;
• Heap-based memory allocation.

5
Static memory allocation

• Static memory allocation is frozen,


xed throughout the execution of a
program.
• Memory addresses are set at
compile time.
• Cannot vary the size of variables.
• Must be planned in detail while
writing code.
• Typically used for global variables
in your program.
• In Python, no static allocations are
possible.

6
fi
Stack-based memory allocation

• Stack-based memory allocates data in a Stack origin


last-in- rst-out (LIFO) basis. Inactive frame N–3 Data

• Memory addresses are determined when Return to N–3


Inactive frame N–2 Data
a function begins executing.
Return to N–2
• The calling function puts its memory
address on stack to start new frame. Inactive frame N–1
Data
• Local variables are allocated at the start
Return to N–1
of the function in this frame.
Active frame N Data
• When the function ends, the active frame
Stack pointer
becomes free.
• Functions that call themselves (recursive)
may use a lot of stack memory. Free memory

• Stack over ow → crash!

7
fi
fl
Heap-based memory allocation

• Heap-based memory allocates data dynamically in


blocks: Stack

• Variables can be created or destroyed;


• Arrays can change size;
• Complex code objects can exist. Free memory
• Needs a memory manager for allocation/deallocation.
• In C, deallocation must be done by the programmer. If deallocation
is not done properly, you will get a memory leak - eventually using
up all of the free memory; Heap
• In Python, deallocation is automatic using garbage collection.
• Garbage collection: Data segment
• Memory allocations are checked for references to it;
• When there are no references, the block is deallocated;
Text segment
• Circular references can cause problems!

8
Device drivers

• Device drivers are software that enables


software to interact with hardware.
• Speci c to each hardware model, so must
be provided by the manufacturer.
• Common devices have generic drivers that
are distributed with operating systems.
• Operating systems require standard
interfaces, called application programming
interfaces (APIs).
• Such APIs also allow user software to
interoperate with hardware and software.

9
fi
Python imports device drivers

• Python imports are the way to


access APIs.
• Help les and contextual help
provide API documentation. from microbit import button_a
if button_a.was_pressed():
• APIs also are used to access other
software:
• Google maps; from microbit import display

• Financial systems; display.on()

• Cloud storage; display.clear()


• etc. display.show('A')

10
fi
Python can import many things

• Helpful libraries:
• import numpy as np
• import matplotlib.pyplot as plt

• Other software:
• from git import Repo
• from selenium.webdriver import
Firefox

• Device drivers:
• import serial
• import mouse

• Outside services:
• from bitbucket.client import Client
• from pydrive.drive import GoogleDrive

11
There are many useful APIs available

• You need to become familiar with


each API speci c to the tool that
you wish to use.
• Self-directed learning is key to
successful programming.
• Every API comes with
documentation, but they are not
always good.
• You may write software for others
to use, but you need to document
well for others to understand.

12
fi
APIs can make programming look easy

• This is how one can from github import Github


import csv
accept GitHub
invitations from an g = Github('access_token')
users = []
entire class… repos = []
ids = []
• The two imports
harness tens of for invite in g.get_user().get_invitations():
users += [invite.inviter.login]
thousands of lines of repos += [invite.repository.name]
other people’s code. ids += [invite.id]
for id_value in ids:
g.get_user().accept_invitation(id_value)

with open('invites.csv', 'a', newline='') as f:


write_out = csv.writer(f)
write_out.writerows(zip(users,repos))

13
Next section

• 1.9 Operating systems

14
Image references

• Slide 2: Old Days, by Randall Munroe, from https://xkcd.com/1755/ (CC BY-NC 2.5)
• Slide 4: (right) from https://commons.wikimedia.org/wiki/File:Cloud_computing_icon.svg (CC BY-SA 3.0)
• Slide 6: Steven Depolo, from https://www. ickr.com/photos/stevendepolo/3072821281 (CC BY-NC 2.0)
• Slide 9: from https://commons.wikimedia.org/wiki/File:SanDisk-Cruzer-USB-4GB-ThumbDrive.jpg (Public
domain)
• Slide 9: by Dmitry Nosachev, from https://commons.wikimedia.org/wiki/File:Supermicro_AOC-SGP-
I2_Gigabit_Ethernet_NIC,_PCI-Express_x4_card.jpg (CC BY-SA 4.0)
• Slide 11: Python, by Randall Munroe, from https://xkcd.com/353/ (CC BY-NC 2.5)
• Slide 12: from https://www.reddit.com/r/ProgrammerHumor/comments/69uof3/programming_stack_over ow/

15
fl
fl
Computational techniques and computer systems
Module 1: Computer systems
1.9 Operating systems

ENGSCI 233

1
Objectives of 1.9 Operating systems

• Understand the main functions of an operating system.


• Understand the major types of operating systems.

2
Computer software is layered

Layer Purpose

Application Software that actually accomplishes your task

Operating system Software that manages the operation of other software in a computer system

Device drivers Software that provides a common interface to rmware/hardware

Firmware Software that runs on dedicated, low-performance processors

Hardware

e.g. 5 layer TCP/IP model (7 layer OSI Model is more detailed)

3
fi
Operating system

• An operating system is system software that: User


• Manages hardware and software resources;
• Provides standard interfaces;
Application
• Provides common services for programs.

Operating system

Device drivers

Firmware

Hardware

4
Manages hardware and software resources

• How is a processor shared?


• Single-tasking:
• Only one program runs at a time;
• Nothing happens with other programs until the current program exits.

• Cooperative multi-tasking:
• Programs are written to take turns;
• What happens if one forgets?

• Preemptive multi-tasking:
• The OS gives out time slices and manages switching tasks between slices;
• Hardware can generate interrupts to wake up software when needed.

• Multi-threading
• Separate processes in one program that share memory;
• Uses the same multi-tasking approach as separate programs.

5
Manages hardware and software resources

• How is memory shared?


• Memory is organized into pages, assigned as needed.
• Pages are randomly distributed in the physical memory.
• Infrequently-used pages may be stored on the hard drive (virtual memory).

• Attempting to access the wrong page generates an error (segmentation


fault)
• Memory for data can be marked as non-executable.
• Failing to protect memory leads to security holes.

6
Manages hardware and software resources

• How are other resources shared?


• Mutual exclusion (mutex) – one program gets access, others need to wait
for it to be unlocked.
• Semaphore – variable used to track locks/mutexes.
• What happens if this fails?
• Never locked – race conditions, unexpected state changes.
• Never unlocked – deadlocks, resource unavailable to all.

7
Provides standard interfaces

• Operating systems provide standard interfaces using APIs.


• e.g. Windows 10 audio architecture:

https://learn.microsoft.com/en-us/windows-hardware/drivers/audio/windows-audio-architecture

8
Provides common services for programs

• Common services for programs include APIs for using and managing:
• File system;
• Memory allocation;
• Virtual memory;
• Network protocols;
• Security authentication;
• And many more…

9
Operating system examples (desktop)

• Operating systems are available for


many types of computers:
• Desktop;
• Mobile;
• Embedded/real time;
• Server/distributed.

• Desktop operating systems:


• Typically provide a graphical user interface
(GUI), and command line interaction;
• Incorporate advanced tools for memory
management, network security, graphics,
and interacting with hardware extensions;
• Support a wide variety of applications;
• Typically provide advanced support for
software development.

10
Operating system examples (mobile)

• Operating systems are available for


many types of computers:
• Desktop;
• Mobile;
• Embedded/real time;
• Server/distributed.

• Mobile operating systems are:


• Usually not multitasking;
• Have an emphasis on security;
• Have a touch-driven user interface.

11
Operating system examples (embedded/real time)

• Operating systems are available for many


types of computers:
• Desktop;
• Mobile;
• Embedded/real time;
• Server/distributed.

• Real time operating systems must provide


results by speci ed deadlines.
• Hard real time systems: used if relatively
bad consequences result from not meeting
deadlines (e.g. injury or loss of life).
• Soft real time systems: used if relatively
minor consequences result from not
meeting deadlines (e.g. some loss of
performance).

12
fi
Operating system examples (server/distributed)

• Operating systems are available for


many types of computers:
• Desktop;
• Mobile;
• Embedded/real time;
• Server/distributed.

• Server/distributed operating systems


typically:
• Have optional GUI;
• Can be recon gured without restart;
• Have advanced backup facilities;
• Have advanced networking facilities;
• Have tight security.

13
fi
Next section

• Julia Musgrave will present modules on software development and data


structures.

14
Image references

• Slide 2: Operating Systems, by Randall Munroe, from https://xkcd.com/1508/ (CC BY-NC 2.5)
• Slide 8: from https://learn.microsoft.com/en-us/windows-hardware/drivers/audio/windows-audio-architecture
• Slide 10: from https://commons.wikimedia.org/wiki/File:MacOS_logo.svg
• Slide 10: from https://commons.wikimedia.org/wiki/File:Windows_11_logo.svg
• Slide 10: from https://en.wikipedia.org/wiki/Linux#/media/File:Tux.svg
• Slide 11: from https://commons.wikimedia.org/wiki/File:IOS_wordmark_(2017).svg
• Slide 11: from https://commons.wikimedia.org/wiki/File:Android_logo_2019_(stacked).svg
• Slide 12: from https://en.wikipedia.org/wiki/VxWorks#/media/File:VxWorks_icon.svg
• Slide 12: from https://github.com/ARMmbed/mbed-os/blob/master/logo.png
• Slide 12: from https://www.lynx.com/hs-fs/hubfs/LynxOS® Logo white blue drop shadow.png?
width=3152&name=LynxOS® Logo white blue drop shadow.png
• Slide 12: from https://github.com/qnx
• Slide 13: from https://en.wikipedia.org/wiki/Linux#/media/File:Tux.svg
• Slide 13: from https://www.freebsd.org/
• Slide 13: from https://en.wikipedia.org/wiki/Windows_Server#/media/File:Windows_Server_logo.svg
• Slide 13: from https://logowik.com/ibm-zos-logo-vector-svg-pdf-ai-eps-cdr-free-download-19500.html

15

You might also like