Fundamentals of Computer Science
Fundamentals of Computer Science
Course introduction Course introduction
Prof.
Iscrizione
Iscrizione al
al corso
corso
E’ E’ statastata abilitataabilitata l’iscrizionel’iscrizione al al corsocorso
Non Non èè obbligatoriaobbligatoria, la , la richiedorichiedo per per finifini statisticistatistici e per e per megliomeglio programmare
programmare ilil corsocorso
Key Concept: Encodings
We can interpret the 0’s and 1’s in computer memory any way we want.
n We can treat them as
numbers.
n We can encode information in
those numbers
3
Even the notion that the computer understands numbers is an
interpretation
n We encode the voltages on
wires as 0’s and 1’s,
eight of these defining a byte
n Which we can, in turn, interpret
as a decimal number
BTW: why do we interpret this string of 0’s an 1’s as 74 ?
Layer the encodings as deep as you want
ASCII encoding for characters
n “A” coded as 65 n “B” coded as 66 n …
n If there’s a byte with a 65 in it, and we decide that it’s a
character, POOF! It’s an “A”! character, POOF! It’s an “A”!
We can string together lots of these numbers together to make usable text
n “77, 97, 114, 107” stands for “Mark”
n “60, 97, 32, 104, 114, 101, 102, 61” stands for
Layered encodings
A number is just a number
If you have to treat it as a letter, there’s a piece of software that does it
n For example, that associates 65 with the graphical
representation for “A”
If you have to treat it as part of an HTML document, there’s a
If you have to treat it as part of an HTML document, there’s a piece of software that does it
n That understands that “<A HREF=“ is the beginning of a link
That part that knows HTML communicates with the part that knows that 65 is an “A”
Multimedia is unimedia
But that same byte with a 65 in it might be interpreted as…
n A very small piece of sound (e.g., 1/44100-th of a second) n The amount of redness in a single dot in a larger picture
n The amount of redness in a single dot in a larger picture which
is a single frame in a full-length motion picture
We use software to manage all these layers
We use software to manage all these layers
n How do you decide what a number should mean, and how you
should organize your numbers to represent all the data you want?
n That’s data structures
If that sounds like a lot of data, it is
n To represent all the dots on your screen probably takes more
than 3.145.728 bytes
Interactions between Hardware and
Interactions between Hardware and
Software
Software
“
talking” with computers
We need a language to exchange information with computers
n data, recipes, …
Different programming languages are different ways (encodings) to tell computers same things
Programming languages:
• First generation, or machine languages • Second Generation, assembly language • Third generation. C,C++, fortran, Basic • Forth generation: Java, Matlab
Programming languages and layers of abstraction
Programming languages and layers of abstraction
DifferentDifferent languageslanguages atat differentdifferent layerslayers
Machine Machine languagelanguage
0100 0000 0000 1000 0100 0000 0000 1000 0100 0000 0000 1001 0100 0000 0000 1001 0000 0000 0000 1000 0000 0000 0000 1000
Sequence of binary instructions, directly executable by CPU
9 AssemblerAssembler LOAD X LOAD X ADD Y ADD Y STORE STORE ZZ
high high levellevel languagelanguage
def
def funfun():(): a = 0; a = 0;
print a+5 print a+5
Instructions in 1-to-1 correspondence with binary instructions, but
expressed with symbolic (human understandable) names
Machine independent. Data abstraction
defined bydefined by
n
n
a grammar (syntax)
a grammar (syntax)
nn
a semantic
a semantic
nn
a vocabulary
a vocabulary
nn key wordskey words
Language programming
n
n key wordskey words n
n variablesvariables
A A naturalnatural languagelanguage sentencesentence can be
can be syntacticallysyntactically correctcorrect, , butbut with no
with no meaningmeaning atat allall ! !
n
n the the grassgrass readsreads the the househouse
The The samesame for for programmingprogramming languages
Program Development Sequence
Program Development Sequence
1-11
Compiler
+
translation from layer to layer
translation from layer to layer
swap: muli $2, $5, 4 add $2, $4, $2 lw $15, 0($2) lw $16, 4($2) sw $16, 0($2) sw $15, 4($2) jr $31 def swap(v, k) : temp = v[k] v[k] = v[k+1] v[k+1] = temp compiler
Program in a high level language Program in assembler language Introduzione 12 assembler 00000000101000010000000000011000 00000000100011100001100000100001 10001100011000100000000000000000 10001100111100100000000000000100 10101100111100100000000000000000 10101100011000100000000000000100 00000011111000000000000000001000 Programm in binary machine language
Program execution
Program execution
Processor Control Memory Devices Input 000000 00000 00101 0001000010000000 000000 00100 00010 0001000000100000 100011 00010 01111 0000000000000000 100011 00010 10000 0000000000000100 101011 00010 10000 0000000000000000 101011 00010 01111 0000000000000100 000000 11111 00000 0000000000001000 Input: Code/DataComputer Skills - Lesson 2 - L.Ingrosso
Control Datapath Memory Input Output 10/03 /14
Program execution
Program execution
Processor Control Memory Devices Input 000000 00000 00101 0001000010000000 000000 00100 00010 0001000000100000 100011 00010 01111 0000000000000000 Code/Data stored in memoryComputer Skills - Lesson 2 - L.Ingrosso
Control Datapath Input Output 100011 00010 01111 0000000000000000 100011 00010 10000 0000000000000100 101011 00010 10000 0000000000000000 101011 00010 01111 0000000000000100 000000 11111 00000 0000000000001000 10/03 /14
Program execution
Program execution
Processor Control Memory Devices Input 000000 00000 00101 0001000010000000 000000 00100 00010 0001000000100000 100011 00010 01111 0000000000000000The processor fetches an instruction
Computer Skills - Lesson 2 - L.Ingrosso
Control Datapath Input Output 100011 00010 01111 0000000000000000 100011 00010 10000 0000000000000100 101011 00010 10000 0000000000000000 101011 00010 01111 0000000000000100 000000 11111 00000 0000000000001000 10/03 /14
Program execution
Program execution
Processor Control Memory Devices Input 000000 00100 00010 0001000000100000The CPU decodes the instruction What I have to do?
Computer Skills - Lesson 2 - L.Ingrosso
Datapath Memory Input Output 000000 00100 00010 0001000000100000 10/03 /14
Program execution
Program execution
Processor Control Memory Devices Input 000000 00100 00010 0001000000100000The processing unit executes the instruction (eventually
interacting with the memory loading operands and saving results)
Computer Skills - Lesson 2 - L.Ingrosso
Datapath
Memory Input
Output
contents Reg #4 ADD contents Reg #2
results put in Reg #2
000000 00100 00010 0001000000100000
10/03 /14
Program execution
Program execution
Processor Control Datapath Memory Devices Input Output 000000 00000 00101 0001000010000000 000000 00100 00010 0001000000100000 100011 00010 01111 0000000000000000 100011 00010 10000 0000000000000100 101011 00010 10000 0000000000000000 101011 00010 01111 0000000000000100 000000 11111 00000 0000000000001000Computer Skills - Lesson 2 - L.Ingrosso
Datapath Output Fetch Decode Exec 10/03 /14
Program execution
Program execution
Processor Control Memory Devices InputWhen the program terminates, the results are sent to the output device
Computer Skills - Lesson 2 - L.Ingrosso
Control Datapath Input Output 00000100010100000000000000000000 00000000010011110000000000000100 00000011111000000000000000001000 00000100010100000000000000000000 00000000010011110000000000000100 00000011111000000000000000001000 10/03 /14
Why should you need to study computer science? or “recipes”?
To understand better the “recipe-way” of thinking
n It’s influencing everything, from computational science to bioinformatics n Eventually, it’s going to become part of everyone’s notion of a liberal
education
n That’s the process argument
n BTW, to work with and manage computer scientists
AND … to communicate!
n Writers, marketers, producers communicate through computation
We’ll somehow take these in opposite order
Summary
Summary
EncodingEncoding
Computing Computing llayersayers
Programming Programming languageslanguages n
n