Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

CENG240

Programming with Python for


Engineers
Lecture 1
M. ÇAĞRI KAYA
Ceng240
Website
https://ceng240.github.io/
Textbook
Programming with Python for Engineers, https://pp4e-book.github.io/
Grading
Midterm 30%, Labs 30% (starts after the 3rd week), Participation 5%, Final 35%
Workbook
https://pp4e-workbook.github.io/

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 2


How to practice Python
Using an interpreter accessible on the Web:
https://colab.research.google.com

Installing Python interpreter to your computer:


https://www.python.org/ (Download the latest version, for Windows: check Add python 3.X to the path)

Editor:
https://www.jetbrains.com/pycharm/ (Professional, Community)

Hello World:
New project -> (Make sure base interpreter is correct: python3.X) -> New python file ->
print('Hello World')

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 3


Outline for this lecture
BASIC COMPUTER
 What is computing?
 CPU & Memory
 Fetch, Decode, Execute
 Machine code + Assembler
 BIOS, OS

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 4


Computing
Computing is the process of inferring data from data (input -> output)

X
Task Z
Y

Multiplying two numbers: X * Y = Z

“status of my
delivery with id Chat bot “it’s on its way”
123456”

biometric authentication, image recognition, voice recognition…

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 5


Computer
The broader context: Any physical entity that can do ‘computation’.
The most common context: An electronic device that has a ‘microprocessor’ in it.
A computer,
- works on binary representation: 0/1, 0: no electric potential (voltage, signal),
1: some fixed electric potential (usually 5 Volts, a signal)
- consists of the central processing unit and memory, other electronic units (peripherals) for i/o
- performs a ‘task’ by executing a sequence of instructions, called a ‘program’
- is deterministic. (adding randomization from another unit is possible)

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 6


What is programming?
 The CPU (the microprocessor - 𝜇P) is able to perform several types of actions: arithmetic
operations, transfer to/from memory, communicating with the peripherals… each conducted by
‘instructions’, ‘machine code’
 Programming: a series of steps to be carried out or goals to be accomplished
Human readable (e.g. ‘print’) -> Machine readable (‘000100101001011’)

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 7


Basic Computer Organization
The von Neumann architecture: an addressable memory and a CPU

Querying the actions


All encoded actions and
(instructions), executes
data
them one by one

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 8


von Neumann Architecture

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 9


Memory
The memory is organized as a stack of rows such that each row has an
associated address.
Each row has 8 bits (each bit holds 0/1).
8 bit = 1 byte
W/R wire is set to WRITE (1) :
The binary content on the input data bus is copied into the 8-bit location
whose address is provided on the address bus, the former content
is overwritten.
W/R wire is set to READ (0) :
The data bus is set to a copy of the content of 8-bit location whose
address is provided on the address bus. The content of the accessed
byte is left intact.

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 10


Memory
The memory is also referred as
Random Access Memory (RAM).

8713

00001111
00001111

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 11


CPU
Central Processing Unit, a.k.a., the brain of a computer
 Control Unit (CU): fetching instructions from the memory, interpreting (‘decoding’) them and
executing them. -> next instruction, fetch-decode-execute cycle
 Arithmetic Logic Unit (ALU): performing arithmetic (addition, subtraction, multiplication,
division) and logic (less-than, greater-than, equal-to etc.) operations.
CU ALU
data, type of the operation
 Registers: storage units on the CPU that store the instruction being executed, the affected
data, the outputs, and temporary values.
 Two categories of registers: Special Purpose and General Purpose
 Two important special purpose registers are Program Counter (PC) and Instruction Register (IR)

 Input/Output Connections: connect the CPU to the other components in the computer

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 12


The Fetch-Decode-Execute cycle
The CPU is in fact a state machine, a machine that has a representation of its current state. The
machine, being in a state, reads the next instruction and executes the instruction according to its
current state. The state consists of what is stored in the registers. Until it is powered off, the CPU
follows the Fetch-Decode-Execute cycle where each step of the cycle is based on its state.
The control unit is responsible for the functioning of the cycle.

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 13


Fetch phase
The cycle starts with the fetch phase
At the beginning of this phase, the CPU has the address of the next instruction in the PC register.
During this phase, the address bus is set to this address in the PC register and the R/W wire is
set to Read (0). The memory responds to this by providing the memory content at the given
address on the data bus.

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 14


Decode phase
At the beginning of this phase, the IR is assumed to be holding the current instruction.
Instruction = Opcode + Effected data or address
Every CPU has an electronically built-in hard-
Opcode Effected data or address wired instruction table in which every
possible atomic operation that the CPU can
0001 0110
carry out has an associated binary code,
called operation code (opcode in short)
Add the contents
Register 01 Register 10
of two registers

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 15


Execute phase
The corresponding circuits carry out the instruction in this phase.
Depending on the instruction, the registers, the memory or other components are effected.
When the instruction completes, the PC is incremented (to the next instruction)
(exception: instruction changes the control flow, jumps to another instruction)
Not all instructions take the same amount of time to be carried out.
Floating point division, for example, takes much more time compared to others.

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 16


The stored program concept
Computer program: a set of instructions and data that perform a certain task.
The idea of storing a computer program into the memory to be executed is coined as the Stored
Program concept.
A program in the memory that multiplies two integer numbers sitting in two different locations
in the memory and stores the result in another memory location:

Which bytes are instructions/data?

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 17


Assembly program
A human invented denotation for instructions and data.
Each line of the assembler text corresponds to a single
instruction.
Binary code program: Machine Code Program (or simply
the machine code).
Assemblers: machine code programs that convert the
assembly text into machine code.
Even assembly languages are not efficient / not allowing fast
programming: Higher level languages are good… (C, C++,
Python…)

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 18


Pros and cons of the von Neumann
architecture
Advantages:
 Simple CPU design: CPU retrieves data and instructions in the same way from memory
 Retrieve data in the same way from I/O devices and memory
 The programmer can control/optimize memory organization
Disadvantages:
 Parallelization implementations difficult: Parallelization is actually a quick sequential change in
tasks.
 Von Neumann bottleneck: Instructions can only be carried out one at a time and sequentially.
 Unintentionally overwrite an instruction due to an error in the program.
Alternative: Harvard Architecture

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 19


Peripherals of a computer
Anything except from CPU and memory: keyboard, mouse, display, hard disk etc.
How peripherals are connected to the von Neumann architecture:
◦ When the CPU talks to the memory, i.e. it is busy, other devices keep quiet
◦ Otherwise, the CPU can talk to other devices through data bus

Polling
Inefficient, because it requires frequent asking to the device

Interrupts
CPU has a specific wire that is connected to the peripherals, when a device wants to talk, sends a signal
to the CPU

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 20


The running of a computer
The start-up process (booting), loads a program (Operating System - OS) from the disk
◦ Start-up
There must be a machine code that keeps its content even the power is off,
and CPU looks there for the first instruction: BIOS (Basic Input Output System)
When you power up a PC, BIOS do the following:
◦ Power-On Self Test (POST): checks CPU, memory, and other devices (keyboard, display, hard disk drive …)
◦ Look for an OS and load it (through master boot record - MBR)
◦ OS
After being loaded into memory, manages resources and services. It hides the details of the hardware…
OS does
◦ Memory Management: Managing the memory that is connected to the CPU, switches in the memory content
◦ Process Management: Scheduling of different tasks
◦ Device Management: External devices of a computer have a software plug-in: device drivers
◦ File Management: file formats and their storage are achieved through OS
◦ Security: is important for integrity, availability, and confidentiality of the computer
◦ User Interface: a mean of communication between human and computer

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 21


Conclusion
Important Concepts
◦ The von Neumann architecture
◦ The interaction between the CPU and the memory via address, R/W and data bus lines
◦ The crucial components on the CPU: The control unit, the arithmetic logic unit and the registers
◦ The fetch-decode-execute cycle
◦ The stored program concept
◦ Operating system and its responsibilities

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 22


Thank You!
QUESTIONS?

3/16/2021 M. ÇAĞRI KAYA - LECTURE 1 - BASIC COMPUTER 23

You might also like