1/22
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
computer’s architecture - programmers perspective
computers = everywhere : embedded - washing machine, car fridge
‘ not embedded’ - normal laptops
enigma machine - used for encryption
can only perform encryptions - hence application specific
1st programmable computer
invented by Konrad Zuse - German + made Z3 (world’s 1st fully operational electromechanical programmable computer) in 1941
butttt - another earlier computing device: the Analytical Engine - by Charles Babbage in 1830s - cites as 1st conceptual programmable computer
x completed during Babbage’s lifetime but design laid out key principles of modern computers
stored program computers - John Von Neumann
stored program computers = became known as ‘VN architecture’ systems
a computer that stores program instructions (+data) in memory
✓ thing as
- absence of physical relays or structures in VN = faster output
wo mechanical relays, data can be sent to output devices via electronic signals = ↑ faster output speeds vs early mechanical systems
- flexibility - architecture = ↑ versatile + can handle wide range of tasks by simply changing program (instructions) stored in memory w/o need for hardware changes
- cost efficiency - single shared memory design = ↓ need for complex hardware = ↓ production + maintenance costs
- smaller, ↑ integrated systems - x bulky switches/relays = architecture allows for smaller + ↑ compact designs for output devices + their control systems
von Neumann architecture - in detail
3 components :
Central Processing Unit (CPU)
memory
input/output (I/O) interfaces
CPU - most important - has to execute a stream of instructions
heart of computing system
incl. 2 main components:
control unit (CU)
Arithmetic + Logic Unit (ALU) - performs mathematical or logical operations
memory
computers memory used to store both program instructions + data
input/output interfaces
used to receive or send info from/to connected devices
connected devices = peripheral devices
program execution of von Neumann computer
CU understands data needs to be provided by user
data is read from input device (e.g. keyboard) + brought to CPU
(3-4) similar steps followed
controller commands ALU to compute addition
controller copies data from ALU to memory
controller commands ALU to compute subtraction
controller sends data from ALU to display
organisation of a CPU - inside a CPU
.
Inside a CPU: Arithmetic and Logic Unit
Arithmetic and Logic Unit (ALU) is the union of the circuits for performing arithmetic and logical operations
Control Unit is responsible for step-by-step execution of instructions during a program execution.
registers
= small storage elements that are located v close to ALU
used as temp storage during computation
ALU can read from + write to registers v. fast
typically as wide as the machine bit width
small memory cells that operate at v high speeds
✓for speed
advantage of registers
1st example - x registers : no. memory read/write = 12
no. arithmetic = 6
hence total time = 12×4 + 6×1 = 54 milliseconds
2nd example- w registers : this computation can do w/o continuously touching memory - just plug in values in registers + do all computations there
total time requirement = 2×4 + 6×1 = 14 milliseconds
concl of registers
= registers improve processing speed
remember - cpu’s don’t need registers, they can just use memory to store
organisation of memory
programmer sees memory as a storage element
memory = addressable storage
memory access
during read + store operations, CPU generates memory addresses
memory management unit (MMU) reads or writes from/to requested memory location
example: memory access during program execution
steps:
CPU asks MMU to fetch data from #0
MMU reads location with address #0 + returns value of ‘a’
similar steps for ‘read b’
CPU computes ‘c’
CPU instructs MMU to store ‘c’ in location with address #2
MMU writes ‘c’ at #2
number systems
decimal system = base 10 system
digits = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
for example: 957 = 9 × 10² + 5 × 10^1 + 7 × 10^0
binary number system = base 2 system
digits = 0, 1 - called a ‘bit’
collection of 8 bits = byte, example : 0b11010100
0b1110 = binary number
value = 1 × 2³ + 1 × 2² + 1 × 2^1 + 0 × 2^0
= 14 in decimal
hexadecimal number system = base 16 system
digits = digits = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
example, 0xA3B is a hex number
value = A x 16² + 3 × 16^1 + B
= 10 × 16² + 3 × 16^1 + B
= 214 in decimal
a byte consists of 2 hex digits
example: let the byte be 1001 0100 - hex equivalent is 0xD4
general structure of a c program
data types
when dealing with +ve numbers only, can add unsigned before the type + get the range doubled
constants = declared as const
a constant is stored in the read-only segment of the memory
any effort to change a const variable will result in compilation error
values that once u declared them + given them a value, they never change
✓ practice = everything that can be constant should be constant
immediate initialisation of built-in variable
un-intialised variable contains a ‘garbage’ value initially
when you read from initialised you will get some value back - could be any value possible - 0, machine exploding etc
so ✓ practice = initialise a variable while declaring
more on data types
helpful types w defined bit width (convenient feature)
tells you how many bits there actually are - makes it explicit
use then when u know what ur dealing with
x have to thing abt long vs long long = ↑ easier to use
control flow
if statement - used for conditional computation
switch statement
expression is 1st evaluated
its compared with constant-expression1, constant-expression2,….
if it matches anyone, then all statements inside that case are executed
statements in the default case are executed if no match is found
if-else statements
used for multiple branching
better to not use in practice
for loop
more control flow
while loop
while the condition is true, execute the body of the loop
do while
continue statement
continue is used inside a loop
when continue is encountered in loop, control skips the statements inside the loop for the current iteration + jumps to beginning of next iteration
cont
break statement
= skips out of the loop instantly
after break control directly comes out of loop and the loop gets terminated
arrays
= data structure
stores a fixed size
sequential collection of elements
of the same type
arrays in C: common mistakes
C compiler/runtime doesn’t check array limits - C x make any efforts to ensure you stay within array - will happily go up to the end of the array + then go to ‘no where land’
if memory projection = violated, then the program crashes due to segmentation fault
program normally “crashes” - but in C = undefined behaviour + may x crash but instead go through happily + print out wtv in memory
need to do bounds checking manually as will happily go anywhere into memory when past bounds
simple solution = constant for length of array
undefined behaviour
a wide range of things in C cause UB, for example:
out of bounds access (above)
signed int overflow
divide by zero
…
when hitting UB, the program can (theoretically) do anything
functions
= a block of statements that together perform a task
function definition in C has:
a name
a list of arguments (optional)
type of value it returns (if any)
local variable declarations (if any), and
a sequence of statements (if any)
standard library (libc) I/O
printf() function - used to print outputs
It is defined in stdio.h library and prototype is:
printf ("%format", variable_name);
format is a place holder which depends on data-type
scan() function = used to receive inputs from keyboard
“porting” from Java
porting = process of adapting software or code to run on a diff platform/environment than the one it was originally designed for