0% found this document useful (0 votes)
1 views

Week1_BasicComputerOrganization

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Week1_BasicComputerOrganization

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

CENG 240

Programming with Pyton


for Engineers
Alper Tolga Kocataş
Software Engineer @
Course Introduction
To give brief introduction to understanding of a basic
computer organization, data representation,
Objective of the course: programming languages and algorithmic thinking.
It is designed as a first course of programming.
Supported by lab sessions.

https://ceng240.github.io
Web–site: https://ceng240.github.io

Textbook: https://pp4e- Programming with Python for


book.github.io/ Engineers, by S. Kalkan, O. T.
Şehitoğlu and G. Üçoluk.
Course Introduction

https://pp4e-workbook.github.io
Course conduct: Course workbook: https://pp4e-
https://pp4e-workbook.github.io
workbook.github.io
Weekly 2 hour courses,
Office hours with the assistants,
Lab exams,
Midterm exam and final exam
Grading
• A total of 8 lab exams
• You can ask any questions about grading and course conduct by sending
an email to: [email protected]
Basic Computer
Organization
CENG 240
Middle East Technical University
Outline
• Components of the von Neumann Architecture
• The Memory
• The CPU
• The Fetch-Decode-Execute Cycle
• The Stored Program Concept
• Pros and Cons of the von Neumann Architecture
• Peripherals of a computer
• The running of a computer
• The startup process
• The Operating System
• Important Concepts
The von Neumann Architecture
• The von Neumann Architecture defines the basic structure used
in most computers today
• Proposed in 1945 by von Neumann
• Consists of two distinct units:
• An addressable memory
• Central Processing Unit (CPU)
The von Neumann Architecture
• All the encoded actions and data are stored together in the
memory
• The CPU queries the instructions stored in memory and
executes them one by one, sequentially.

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

The von Neumann Architecture
• The CPU communicates with the memory using three sets of
wires:
• Address bus (e.g., 8 wires)
• Data bus (e.g., 8 wires)
• R/W wire (1 wire)
• Each wire contains either 0 or 1 as information
• 0 or +5 Volts

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

The Memory
• The memory can be imagined as
locations organized as rows.
• Each row has eight locations (bits)
• A bit can store 0 or 1,
• Each row consists of 8 bits
• Called a Byte
• 1 Byte = 8 bits
• Each byte of the memory has a
unique address
Reading a byte from the Memory
• When the CPU wants to read the byte at address 8700;
• The CPU sets the address bus to the binary value of 8700,
• It sets the R/W write to 0 to indicate a read operation,
• The byte value at address 8700 is then read using the data
bus

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

Writing a byte to the Memory
• When the CPU wants to write a value (e.g., 00110011)
at address 8700:
• The CPU sets the address bus to the binary value of 8700,
• It sets the R/W write to 1 to indicate a write operation,
• It provides the value 00110011 at the data bus
• The value 00110011 is then gets written at the byte at
address 8700

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

Memory
• The information stored in the memory stays until the power
is turned off.
• The memory in computers are called RAM
• RAM: Random Access Memory

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

The Memory
• Random access means that, accessing any
address of the memory takes a constant
amount of time.

• This time is usually very fast (e.g., 50


nanoseconds)
• 1 sec = 1000000000 nanoseconds (ns)

• The word “Random” means that:


• You can read any number of bytes in any random
order,
• and the time required to read any of the bytes will
not require more than 50 ns.

• For example, conventional hard disks which


stores the files in our computers are not
random access.
• Means: It requires more time to read particular
data
The Memory
• When we write some value at some
address, the value stored in that address
gets overwritten

• There is no way to undo this operation.

• You can’t bring back overwritten data


The CPU (Central Processing Unit)
• The CPU can be considered
as the brain of the computer

• It consists of the following


parts:
• Control Unit (CU)
• Arithmetic Logic Unit (ALU)
• Registers
The Control Unit (CU)
• The Control Unit:
• Fetches instructions from the memory,
• Interprets (decodes) the instructions,
• Executes the instructions
• We call this the Fetch-Decode-Execute Cycle

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

Arithmetic Logic Unit (ALU)
• The Arithmetic Logic Unit (ALU) performs operations such as:
• Addition
• Multiplication
• Division
• Logic (less than, greater than, equal to tests)
• Control Unit provides the data which ALU operates on (e.g.,
numbers to add, compare etc. )

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

Registers
• Registers are mainly storage units on the CPU for storing:
• The instruction being executed,
• The affected data,
• Outputs
• Temporary values
• Sizes and numbers of registers differ from CPU model to model.
• Most registers are 64 bits: a single register can store 64 bit values,
• Most CPU’s have between 10 and 20 of these registers

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

Registers
• The two special purpose registers are:
• Program counter
• Instruction Register
• Input/Output connections connect the CPU to other
components (keyboard, mouse, etc.)

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

The Fetch-Decode-Execute Cycle
• The CPU is a state machine
• A machine that has a representation of its current state
• The state of the CPU is what is stored in the registers
• Until it is powered off, it continues fetching, decoding and
executing instructions stored in the memory
• The Control Unit is responsible for functioning of this cycle.

Instruction1
Instruction1
Instruction2
Instruction2
Instruction3
Instruction3

Data1
Data2
Data3

The Fetch Phase
• The CPU has the address of the next instruction in the Program Counter
(PC) register
• The address bus is set to this address stored in the PC
• R/W wire is set to Read (0)
• The memory responds to this by providing the instruction stored in that
address via the data bus.
• The bytes which comprise the instruction are received into the instruction
register (IR).

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

The Decode Phase
• The content of the IR stores the instruction to be executed
• The first part of this content triggers some action
• Every CPU has an electronical circuit (instruction table) in
which every operation (e.g., addition multiplication)
corresponds to a binary code
• This is operation code is called the opcode.
• The instruction table differs from CPU brand to brand.

8 bit Instruction.
First 4 bits are the opcode, remaining 4 bits are
the data or address related to the operation
The Decode Phase

• For example, opcode 0001 can denote an


instruction for:
• Reading data from the memory or,
• Writing data to the memory or,
• Adding the contents of the two registers

• Remaining 4 bits then contain the parameters of the


instruction such as:
• The memory address to read/write the data from/to
• The registers which store the values to add
The Execute Phase
• In this phase, CPU caries out the operation denoted
by the opcode

• It uses the related data or address specified in the


instruction

• After the current instruction is executed, the


program counter (PC) is incremented by one

• However, some instructions change the flow of the


program and cause the PC to jump to some other
address.
• In that case, program counter (PC) is updated to the
specified address
The Execute Phase
• Some of the instructions take more time to
execute.
• For example, floating point division takes much
more time compared to addition.

• When does the CPU jump from one state to


another? (e.g., from Fetch to Decode?)
• Answer: The System Clock provides an external
pulse to the CPU. It is like a heart-beat.
The Execute Phase
• In every tick of the System Clock, CPU performs
an operation.
• The time between two clock ticks is called a
Clock Cycle or One Clock Period
The Execute Phase
• It would be good if the fetch phase, decode phase
and execute phase took one clock cycle, respectively.

• However, the fetch phase alone can take multiple


cycles, and some operations such as multiplication
can take more than one cycles.
The Length of a Clock Cycle
• CPUs are marked with their clock frequency.
• E.g., Intel’s i9 processor has a maximum clock frequency of
5GHz.
• This means 5 x 109 pulses per second
• Period = 1/frequency
• 1/ (5 x 109) seconds = 200 pico seconds
• Very short time: light travels only 6cm in this time.
The Stored Program
Concept
Storing programs and data in the memory
The Stored Program Concept
• We store the programs and related data in the memory
• The programs contain the instructions
• The CPU fetches and executes these instructions when
we run a program in our computer

Instruction1
Instruction2
Instruction3

Data1
Data2
Data3

The Stored Program Concept
• When you open the an application on your phone (e.g., Music app), the CPU
fetches the instructions of the application and executes them,

• The instructions operate on the data (e.g., songs) stored in the memory of your
phone,

• Eventually, your phone plays the music by using the instructions of the app and
the data (songs) stored in the memory of your phone

Instruction1
Instruction2
Instruction3

Song1
Song2
Song3

The Stored Program Concept
• Below is an example of a stored program
• This is the real extract from the memory
• This program multiplies two integer numbers in two
different locations of memory
• It stores the result of multiplication in another location of
the memory

Row 1 Row 2 Row 3


The Stored Program Concept
As displayed here, it is very hard to understand

All we see is zeros and ones

This is called the machine code

Machine code is not understandable by humans

Row 1 Row 2 Row 3


The Stored Program Concept:
Alternative notation
• This is the same program, in Assembly Language.
• Programs written in Assembly language are easier to
understand
Registers
Instructions
The Stored Program Concept:
Alternative notation
• A special program called Assembler converts this
program into machine code.
• CPU can only understand machine code.

Assembler
Pros and Cons of the von Neumann
Architecture
• Advantages:
• CPU retrieves the data and instructions from the
memory using the same way.
• This simplifies the design of the CPU

• Data from input/output (I/O) devices (e.g., keyboard,


mouse) and the data from memory is retrieved in the
same manner.
• This is achieved by mapping the I/O device to a specific region
of the memory

• The programmers have a considerable conrol of the


memory organization.
• They can better optimize the memory organization.
Pros and Cons of the von Neumann
Architecture
• Disadvantages:
• Sequential instruction processing makes parallel
implementations difficult.
• Any parallelization is actually a quick sequential change in tasks
• E.g., word and excel seems to be running at the same time,
however, at a given point in time, only one program can run on a
single core CPU.
• Ok, multi-core CPU’s are a bit different on this.
• An error in the program can cause an instruction to be
overwritten.
• Remember: if something in the memory gets overwritten, it is lost.

• There were other architectures such as the Harvard architecture, but


despite its disadvantages, the von Neumann architecture stood the test
of time, and is still being used extensively.
Peripherals of a Computer

Any device connected to the computer, which is Examples of Peripherals:


outside of the von Neumann Architecure is
called a Peripheral.
Keyboard,
Mouse,
Monitor,
Hard Disk
Printer
Peripherals of a Computer
CPU communicates with the devices by mapping them to specific Memory regions

For example, Keyboard can be mapped to memory address 8000,

When you press a key at the keyboard, the memory address at 8000 is set to the code for that key.

CPU can then read the byte value at 8000 to understand which key you pressed.

Program1
..
Program2
..
Data

Keyboard
Mouse
Printer
Peripherals of a Computer
• The CPU can either poll the address 8000 continuously to
detect your key presses,
• This is called polling.
• The CPU will check address 8000 even if you don’t press anything
on the keyboard.
• This slows down the CPU a lot.

Program1
..
Program2
..
Data

Keyboard
Mouse
Printer
Peripherals of a Computer
• Alternatively, the keyboard can send an interrupt to the CPU when you
press a key
• Using the interrupt mechanism, the keyboard somehow can tell the CPU
that a keyboard event has occurred.
• The CPU then stops its normal tasks and checks the address 8000 to see
which key the user has pressed.

Program1
..
Program2
..
Data

Keyboard
Mouse
Printer
The running of a
computer
• When you press the power button of a
computer:
• BIOS (Basic Input-Output System)
stores a special startup program, and
that program gets loaded into the
memory.
• Power-On Self Test (POST) is
performed
• CPU and RAM is checked for
errors,
• Peripherals are identified and
initialized

• CPU looks for an operating system (OS)


through the connected storage devices
• Master Boot Record (MBR)
region of the disk contains a table
and code piece for loading the
OS on that disk

• CPU starts the operating system.


The Operating System

Examples of Operating
Windows, Linux, MAC OS
Systems:

The OS is a set of programs that act as a layer between the


user and the computer hardware.

Memory management
Process management
Device management
It is responsible for: File management
Security
User Interface
Wrap up – Concepts to remember
• The von Neumann Architecture,
• The interaction between the CPU and the memory using:
• Address bus
• Data bus
• R/W line
• The components of the CPU:
• The control unit
• Arithmetic logic unit
• Registers
• The fetch-decode-execute cycle
• The stored program concept
• Operating system and its responsibilities
Further Reading

• For the curious: check the Further reading topics at the end of Chapter 1
in the book.

You might also like