week 1 - Computer Architecture and Principles of C

0.0(0)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/22

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

23 Terms

1
New cards

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

2
New cards

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

3
New cards

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

4
New cards

von Neumann architecture - in detail

3 components :

  1. Central Processing Unit (CPU)

  2. memory

  3. input/output (I/O) interfaces

CPU - most important - has to execute a stream of instructions

heart of computing system

incl. 2 main components:

  1. control unit (CU)

  2. 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

5
New cards

program execution of von Neumann computer

  1. CU understands data needs to be provided by user

  2. data is read from input device (e.g. keyboard) + brought to CPU

  3. (3-4) similar steps followed

  1. controller commands ALU to compute addition

  2. controller copies data from ALU to memory

  3. controller commands ALU to compute subtraction

  4. controller sends data from ALU to display

<ol><li><p>CU understands data needs to be provided by user</p></li><li><p>data is read from input device (e.g. keyboard) + brought to CPU</p></li><li><p>(3-4) similar steps followed</p></li></ol><ol start="5"><li><p>controller commands ALU to compute addition</p></li><li><p>controller copies data from ALU to memory</p></li><li><p>controller commands ALU to compute subtraction </p></li><li><p>controller sends data from ALU to display</p></li></ol><p></p>
6
New cards

organisation of a CPU - inside a CPU

.

7
New cards

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.

8
New cards
<p>registers</p>

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

<p>= small storage elements that are located v close to ALU</p><ul><li><p>used as temp storage during computation</p></li><li><p>ALU can read from + write to registers v. fast</p></li></ul><p></p><p>typically as wide as the machine bit width</p><p></p><p>small memory cells that operate at v high speeds</p><p>✓for speed</p><p></p>
9
New cards
<p>advantage of registers</p>

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

<p>1st example - x registers : no. memory read/write = 12</p><p>no. arithmetic = 6</p><ul><li><p>hence total time = 12×4 + 6×1 = 54 milliseconds</p></li></ul><p></p><p></p><p>2nd example- w registers : this computation can do w/o continuously touching memory - just plug in values in registers + do all computations there</p><ul><li><p>total time requirement = 2×4 + 6×1 = 14 milliseconds</p></li></ul><p></p>
10
New cards
<p>concl of registers</p>

concl of registers

= registers improve processing speed

remember - cpu’s don’t need registers, they can just use memory to store

<p>= registers improve processing speed</p><p></p><p>remember - cpu’s don’t <strong>need</strong> registers, they can just use memory to store</p>
11
New cards

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

12
New cards
<p>example: memory access during program execution</p>

example: memory access during program execution

steps:

  1. CPU asks MMU to fetch data from #0

  2. MMU reads location with address #0 + returns value of ‘a’

  3. similar steps for ‘read b’

  4. CPU computes ‘c’

  5. CPU instructs MMU to store ‘c’ in location with address #2

  6. MMU writes ‘c’ at #2

<p>steps:</p><ol><li><p>CPU asks MMU to fetch data from #0</p></li><li><p>MMU reads location with address #0 + returns value of ‘a’</p></li><li><p>similar steps for ‘read b’</p></li><li><p>CPU computes ‘c’</p></li><li><p>CPU instructs MMU to store ‘c’ in location with address #2</p></li><li><p>MMU writes ‘c’ at #2</p></li></ol><p></p>
13
New cards
<p>number systems</p>

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

<p>decimal system = base 10 system</p><p>digits = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9</p><ul><li><p>for example: 957 = 9 × 10² + 5 × 10^1 + 7 × 10^0 </p></li></ul><p></p><p>binary number system = base 2 system</p><p>digits = 0, 1 - called a ‘bit’</p><p>collection of 8 bits = byte, example : 0b11010100</p><ul><li><p>0b1110 = binary number</p></li><li><p>value = 1 × 2³ + 1 × 2² + 1 × 2^1 + 0 × 2^0 </p><p>= 14 in decimal</p></li></ul><p></p><p>hexadecimal number system = base 16 system</p><p>digits = digits = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F</p><ul><li><p>example, 0xA3B is a hex number</p><p>value = A x 16² + 3 × 16^1 + B</p><p>= 10 × 16² + 3 × 16^1 + B</p><p>= 214 in decimal</p></li></ul><p>a byte consists of 2 hex digits</p><p>example: let the byte be 1001 0100 - hex equivalent is 0xD4</p><p></p>
14
New cards

general structure of a c program

knowt flashcard image
15
New cards
<p>data types</p>

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

<p>when dealing with +ve numbers only, can add unsigned before the type + get the range doubled</p><p></p><p>constants = declared as const</p><p>a constant is stored in the read-only segment of the memory</p><ul><li><p>any effort to change a const variable will result in compilation error</p><ul><li><p>values that once u declared them + given them a value, they never change</p></li><li><p><strong>✓ </strong>practice = everything that <u>can</u> be constant should be <u>constant</u></p></li></ul></li></ul><p></p><p>immediate initialisation of built-in variable</p><ul><li><p>un-intialised variable contains a ‘garbage’ value initially</p></li><li><p>when you read from initialised you will get some value back - could be any value possible - 0, machine exploding etc</p><ul><li><p>so <strong>✓ </strong>practice = initialise a variable while declaring</p></li></ul></li></ul><p></p>
16
New cards
<p>more on data types</p>

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

<p>helpful types w defined bit width (convenient feature)</p><p>tells you how many bits there actually are - makes it explicit</p><ul><li><p>use then when u know what ur dealing with</p><ul><li><p>x have to thing abt long vs long long = ↑ easier to use</p></li></ul></li></ul><p></p><p></p><p>control flow </p><p>if statement - used for conditional computation</p><p></p><p>switch statement</p><ul><li><p>expression is 1st evaluated</p></li><li><p>its compared with constant-expression1, constant-expression2,….</p></li><li><p>if it matches anyone, then all statements inside that case are executed</p></li><li><p>statements in the default case are executed if no match is found</p></li></ul><p></p><p>if-else statements</p><ul><li><p>used for multiple branching</p><ul><li><p>better to not use in practice</p></li></ul></li></ul><p></p><p>for loop</p><p></p>
17
New cards
<p>more control flow </p>

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

<p>while loop </p><ul><li><p>while the condition is true, execute the body of the loop</p></li></ul><p></p><p>do while</p><p></p><p>continue statement</p><ul><li><p>continue is used inside a loop</p><ul><li><p>when continue is encountered in loop, control skips the statements inside the loop for the current iteration + jumps to beginning of next iteration</p></li></ul></li></ul><p></p>
18
New cards

cont

break statement

= skips out of the loop instantly

  • after break control directly comes out of loop and the loop gets terminated

<p>break statement </p><p>= skips out of the loop instantly</p><ul><li><p>after break control directly comes out of loop and the loop gets terminated</p></li></ul><p></p>
19
New cards
<p>arrays</p>

arrays

= data structure

  1. stores a fixed size

  2. sequential collection of elements

  3. 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

<p>= data structure</p><ol><li><p>stores a fixed size</p></li><li><p>sequential collection of elements</p></li><li><p>of the same type</p></li></ol><p></p><p></p><p>arrays in C: common mistakes</p><ul><li><p>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’</p></li><li><p>if memory projection = violated, then the program crashes due to segmentation fault</p></li><li><p>program normally “crashes” - but in C = undefined behaviour + may x crash but instead go through happily + print out wtv in memory</p></li><li><p>need to do bounds checking manually as will happily go anywhere into memory when past bounds</p><ul><li><p>simple solution = constant for length of array</p></li></ul></li></ul><p></p>
20
New cards

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

<p>a wide range of things in C cause UB, for example:</p><ul><li><p>out of bounds access (above)</p></li><li><p>signed int overflow</p></li><li><p>divide by zero</p></li><li><p>…</p></li></ul><p></p><p>when hitting UB, the program can (theoretically) do anything</p><p></p><p></p>
21
New cards

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)

<p>= a block of statements that together perform a task</p><p></p><p>function definition in C has:</p><ul><li><p>a name</p></li><li><p>a list of arguments (optional)</p></li><li><p>type of value it returns (if any)</p></li><li><p>local variable declarations (if any), and</p></li><li><p>a sequence of statements (if any)</p></li></ul><p></p>
22
New cards

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

<p>printf() function - used to print outputs</p><ul><li><p>It is defined in stdio.h library and prototype is:</p><p>printf ("%format", variable_name);</p><p>format is a place holder which depends on data-type</p></li></ul><p></p><p>scan() function = used to receive inputs from keyboard</p>
23
New cards

“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

<p>porting = process of adapting software or code to run on a diff platform/environment than the one it was originally designed for</p>