EC04-Computer Organization
EC04-Computer Organization
Ing. Aeroespacial
Computers
2
Computer organization Informática
Introduction
Computer architecture
Memory organization
CPU organization
3
Introduction Informática
Computers
A computer is a device that performs processes, calculations and operations
based on instructions provided by a software or hardware program. It is
designed to execute applications and provides a variety of solutions by
combining integrated hardware and software components.
- History of computers à see https://en.wikipedia.org/wiki/Computer
All modern computers are based on digital systems (0/1)
and the idea of “programmed logic”
Program
Machine
Data
(hardware)
(software)
Programmable machine
4
Introduction Informática
Real computer
A computer is a machine designed to execute machine language
instructions
01001000
00110101
10010101
01111010
00101110
Introduction
Computer architecture
Memory organization
CPU organization
6
Computer architecture Informática
Peripherals
Disk Video
Main Controller Controller
CPU Memory
I/O I/O
Bus
7
Computer architecture Informática
Bus
set of electrical lines for the interconnection of all the functional units of a
computer
there are single bus and multiple bus architectures
8
Computer architecture Informática
9
Computer architecture Informática
PC Motherboard
10
Computer organization Informática
Introduction
Computer architecture
Memory organization
CPU organization
11
Memory organization Informática
Memory
Memory: abstract device for storing information with two main operations:
- Write: storing, updating some information on the memory.
- Read: retrieving some previously stored information on the memory.
Memory property: a read operation always returns the value of the last write
operation.
Levels of storage
- Main memory: short-term memory. It stores the program(s) that the CPU is
currently executing and their associated data. In general, it is a volatile
memory: it loses its contents upon disconnection from the power supply.
- Secondary memory: includes disks, tapes, pen-drives, CD’s, and others. It
is a long-term storage for the programs and data that the computer may use.
It is a non-volatile memory.
12
Memory organization Informática
Memory capacity
Volume of information it can store. Usually measured in bytes, KB, MB, GB, TB.
- It is the number of rows (memory words) x number of bytes per row.
‣ Example: 6 MB memory → 6 × 220 bytes ⋍ 6 million bytes.
Memory accesses
Read: The CPU provides Address, RD/WR=1, CS=1 and waits to get Data
Write: The CPU provides Address, Data, RD/WR=0, CS=1
Data Data
15
Memory organization Informática
Access times
- SRAM: 10 ns
- DRAM: 50-150 ns.
16
Memory organization Informática
Memory hierarchy
Levels:
- Processor registers - internal to capacity speed
the processor. - +
- Cache memory - internal to the CPU
processor. regs.
- Main memory - stores programs cache
in execution and their associated memory
data.
main
- Virtual memory - simulates a memory
larger memory.
virtual
- Secondary memory - Disks and memory
similar devices.
Capacity increases top-down secondary
memory
while access speed increases + -
bottom-up.
17
Computer organization Informática
Introduction
Computer architecture
Memory organization
CPU organization
18
CPU organization Informática
19
CPU organization Informática
CPU subsystems
Register bank: a very fast low capacity memory to keep the operands of
machine language instructions.
Arithmetic Logic Unit (ALU): is a combinational circuit that performs
arithmetic (like addition, subtraction, increment, ...) and logic operations
(AND, OR, NOT, ...).
Control Unit (CU): it fetches instructions in memory, decodes (identifies)
them and coordinates the rest of CPU subsystems to execute them.
20
CPU organization Informática
21
CPU organization Informática
Register bank
1011011011101101
0x0001 R0
R1 Equivalent mnemonic
R2
0xFFFF ADD R2,R0
...
Description:
R14
R15/SP R2 ← R2 + R0
23
CPU organization Informática
0x0000 Stack
0x7FFD
26
CPU organization Informática
27
CPU organization Informática
if Z == 0
PC = 0x2000;
end
Cache memory
Current CPU technology allows
to access memory much faster
than main memory, specially Main
dynamic RAM (DRAM). Processor memory
- Due to this, memory can
significantly slow down the CPU
and reduce system
performance. CPU
The cache memory is a fast
memory whose goal is to Cache
improve memory average access memory ...
times.
Cache memory stores copies of
the most frequently used main
memory locations
- Its contents is a subset of the
main memory contents. 29
CPU organization Informática
Cache memory
The operation of cache memory is as follows: whenever the processor needs
to read or write a location in main memory it first checks whether that
memory location is in the cache.
- If the memory location is cached, we say that a cache hit has occurred.
‣ The processor immediately reads or writes the data in the cache line.
- Otherwise, we speak of a cache miss.
‣ The referenced memory location has to be cached. In order to make room for
the new entry, the cache has to evict one of the existing entries and replace it
with the required main memory location.
- The “victim” entry is selected using a replacement policy.
‣ Optimal policy: replacing the entry which is least likely to be used in the future.
However, predicting the future is difficult.
‣ LRU: replaces the Least Recently Used entry.
The performance of the cache memory depends on the percentage of
accesses that result in a cache hit is known as the hit rate.
30
CPU organization Informática
Cache memory
Modern desktop and server CPUs have separated instruction and data
caches to increase performance.
- Instruction caches are read-only so they do not need to be refreshed on main
memory upon a miss
Data caches are usually multi-level, i.e. they are organized as a hierarchy of
more cache levels (L1, L2, etc.)
31
CPU organization Informática
Microprocessors
Microprocessor: single chip CPU.
- Old CPUs were made of separated chips for registers, ALU, CU, etc.
‣ See again Megaprocessor: http://www.megaprocessor.com
- Actual CPUs are single chip
- They can include multiple cores (multicore) that basically are CPUs that
share memory and cache…
32
CPU organization Informática
Microprocessors
The instruction set is considered as the
interface definition of a given
microprocessor
- The Intel Pentium has its own instruction
set different to, for example, the ARM
Cortex
- Two microprocessors with the same
instruction set are considered
compatibles
‣ Examples: Intel Pentium clones
manufactured by AMD or Cyrix, like AMD
K5 or K6.
33
CPU organization Informática
Microprocessors
Evolution of the Intel family
35
CPU organization Informática
36
Informática
Ing. Aeroespacial
Computers
Machine language
Machine language instructions
The stack
Translating into Assembly
Compilers and interpreters
38
Machine language Informática
Machine language
Remember: the CPU only executes machine language instructions
• Then, your computer only runs machine instructions,
• but every processor architecture has its own instruction set
Instruction classification:
- Data movement
- Arithmetic and logic instructions
- Control of flow
- Stack handling
39
Machine language Informática
Instruction encoding
Instruction codes consist of the following fields:
- Opcode: in the SEP16 it is a fixed number of bits for all instructions.
‣ Encodes not only the type of instruction (MOV, INC, etc.) but also the addressing mode
‣ 6 bits: it allows up to 26=64 different instructions and addressing modes.
- Operands: each operand is reserved a number of bits that depends on the operand type
and addressing mode.
- Number of operands: instructions may have different number of operands:
‣ two operands: ADD R2,R0
‣ one operand: JNE 0x2000
‣ no operands: NOP
- Addressing modes: the way in which the operands of the instructions are obtained:
‣ Register addressing requires 4 bits (16 registers)
‣ Direct addressing requires 16 bits (64K word memory address space)
‣ Register indirect addressing requires 4 bits (16 registers)
‣ Immediate addressing requires 16 bits (16 bit data)
‣ Implicit addressing has no operands
42
Machine language Informática
Addressing modes
Register addressing: the operand is a register.
- Examples: MOV R1,R2; ADD R3,R2; XOR R1,R2
Direct addressing: the operand is a memory location that is specified in the
instruction itself.
- Examples: MOV R0,0x4002; JNE 0x2002; CALL 0x7FE
Register indirect addressing: the operand is a memory location whose
address is given by (the contents of) a bracketed register identifier.
- Examples: MOV R0,[R2]; JEQ [R2]; CAL [R2]
Instruction: MOV R0,[R2]
0x4000 R2
...
43
Machine language Informática
Addressing modes
Immediate addressing: the operand is an immediate data specified in the
instruction itself.
- Examples: LDR R0,0xFF01; ADD R2,0xAB01
Implicit addressing: the operand is a predefined register which is not
specified as an operand.
- Examples: PUSHSR, POPSR
44
Computer organization Informática
Machine language
Machine language instructions
The stack
Translating into Assembly
Compilers and interpreters
45
Machine language instructions Informática
Arithmetic instructions
ADD - add SUB - subtract
- ADD reg1,reg2 - add register to register - SUB reg1,reg2
‣ RTL: reg1←(reg1+reg2), Ex: ADD R1, R2 ‣ RTL: reg1←(reg1-reg2)
Logic instructions
CMP - compare and affect flags
- CMP reg1,reg2 - compare register to register.
‣ RTL: PSR←(reg1 ? reg2) Ex: CMP R1,R2
- CMP reg,data - compare register to immediate data.
‣ RTL: PSR←(reg ? data) Ex: CMP R1,0xFFFF
AND - logical and OR - logical or XOR - logical xor
- AND reg1,reg2 - and register to register.
‣ RTL: reg←(reg1 & reg2) Ex: AND R1,R2
- AND reg,data - compare register to immediate data.
‣ RTL: reg←(reg & data) Ex: AND R1,0xFF00
BIC - bit clear
- BIC reg1,reg2 - bit clear register to register.
‣ RTL: reg1←(reg1 & ∼ reg2) Ex: BIC R1,R2
- BIC reg,data - bit clear register to immediate data
‣ RTL: reg←(reg & ∼ data) Ex: BIC R1,0xFF00
48
Machine language instructions Informática
Shift instructions
SHR - shift right SHL - shift left
- SHR reg,data4
‣ RTL:(C←reg31 ←reg30 ←···←reg1 ←reg0 ←0)*data4 times. Ex: SHR R1,0x0A
49
Machine language instructions Informática
Conditions
AL: always
EQ / NE: equal / not equal
MI, PL: minus (negative) /
plus (positive or zero)
Unsigned:
HS / LO: ≥ / <
HI / LS: >/≤
Signed:
GE / LT: ≥/<
GT / LE: >/≤
51
Computer organization Informática
Machine language
Machine language instructions
The stack
Translating into Assembly
Compilers and interpreters
52
The stack Informática
The stack
A stack is a Last In, First Out (LIFO) data structure.
It has two associated operations:
- push: puts a new item onto the top of the stack
- pop: removes an item from the top-most position of the stack.
53
The stack Informática
Other instructions
NOP - no operation
STOP - stop the processor
54
The stack Informática
CAL
[SP] ← PC;
SP ← SP-1;
PC ← 0x240A;
RET
SP ← SP+1;
PC ← [SP] ;
55
The stack Informática
56
The stack Informática
Global
Local
57
Computer organization Informática
Machine language
Machine language instructions
The stack
Translating into Assembly
Compilers and interpreters
58
Translating from high-level into assembly Informática
Assembly language
//DATA segment
ORG 0x2000 SYMBOL TABLE
X: DWORD X ≡ 0x2000
Y: DBYTE
//STACK segment Y ≡ 0x2001
ORG 0x7000 STACK ≡ 0x7200
STACK: DWORD[512] START ≡ 0x4000
//CODE segment CASE2 ≡ 0x400A
ORG 0x4000
CASE1 ≡ 0x400E
START: LDR SP, STACK+512 //SP←Stack base - 0x4000
LDR R0, 200 //R0 ← 200 - 0x4002 END ≡ 0x4011
MOV X, R0 //0x2000 ← R0 - 0x4004
CMP R0, 100 //R0 > 100? - 0x4006
JGT CASE1 //Jump greater - 0x4008
//than (signed)
CASE2: LDR R1, 0 //R1←0 - 0x400A
JAL END //Jump always - 0x400C
CASE1: LDR R1, 1 //R1←1 - 0x400E
SHL R1, 1 //Shift left R1 - 0x4010
END: NOP //Do nothing - 0x4011 NOT TO STUDY-
MOV Y, R1 //0x2001 ← R1 - 0x4012 ONLY GIVEN AS EXAMPLES
OF ASSEMBLY CODE
59
Translating from high-level into assembly Informática
Translating if-statements
ORG 0x2000 // DATA segment
x: DWORD
y: DWORD
ORG 0x4000 // CODE segment
START: LDR R0, 200 // R0 ← 200
MOV x, R0 // 0x2000 ← R0
LDR R1, 0 // R1 ← 0
MOV y, R1 // 0x2000 ← R1
CMP R0,100 // if R0>100?
JGT case1 //Jump signed GT case1
CMP R0,10 //if R0>10
JGT case2 //Jump signed GT case2
case3: MOV R2, 3 //else R2 ← 3
JAL END //Jump always END
case2: MOV R2, 2 //R2 ← 2
JAL END //Jump always END
case1: MOV R2, 1 //R2 ← 1
NOT TO STUDY-
END: MOV y, R2 //0x2001 ← R2 ONLY GIVEN AS EXAMPLES
OF ASSEMBLY CODE
60
Translating from high-level into assembly Informática
Translating for-statements
ORG 0x2000 // DATA segment
y: DWORD[100] // y[0]...y[99]
// i assigned to R0
ORG 0x4000 // CODE segment
START: LDR R0, 0 // R0 ← 0
LDR R1, y // R1 points @ y[0]
loop: CMP R0,100 // R0>=100?
JGT END // if >= exit loop
ADD R1, 2 // R1 points @ y[i]
MOV [R1], RO // y[i] ← R0
INC R0 // i ← i+1
JAL loop // loop again
END: ...
NOT TO STUDY-
ONLY GIVEN AS EXAMPLES
OF ASSEMBLY CODE
61
Translating from high-level into assembly Informática
Translating while-statements
ORG 0x2000 // DATA segment
x: DWORD //var x
ORG 0x4000 // CODE segment
START: MOV R0, x //R0 ← [x]
loop: CMP R0, 0 //x<0?
JLT END //exit loop
CMP R0,100 //x>=100?
JGE END //exit loop
INC R0 //x ← x+1
JAL loop //loop again
END: MOV x, R0 //[x] ← R0
62
Translating from high-level into assembly Informática
NOT TO STUDY-
ONLY GIVEN AS EXAMPLES
OF ASSEMBLY CODE
64
Computer organization Informática
Machine language
Machine language instructions
The stack
Translating into Assembly
Compilers and interpreters
65
Languages and compilers Informática
Languages
High-level programming language: it is machine independent; it can be used for any
computer and may be ported across computers
- It makes abstraction of the computer architecture like the CPU registers, memory
addresses, stacks and, mostly, a particular machine language instruction set
Language components:
- Syntax (form): describes the possible combinations of symbols that form a syntactically
correct program
- Semantics: meaning
Programming language paradigms
- Structured programming:
‣ A program is a limited number of structures (sequences, selections, and repetitions).
The most important structuring elements are functions, which allow to decompose a
program into a set of well defined building blocks. It is used to build higher level
abstractions and hiding their details
‣ C, Matlab, ...
- Object oriented:
‣ Java, C++, ...
- Others...
66
Languages and compilers Informática
Compilers
Compiler: it is a computer program that transforms source code written in a
pro gramming language (source) into another language (target).
- Source: high level language (C, C++, Fortran...)
- Target: usually machine code
67
Languages and compilers Informática
Linkers
Large program consist of many source files and make use of external libraries
Library: file that contains code and data that can be used from independent
programs. They usually encapsulate a set of related functions useful in some
application domain
- Example: a matrix library, a graphic library, an I/O library,...
Generating an executable program consists of two steps:
- 1. Compiling: each source file separately. In this case the compiler does not
produce the executable file, but an intermediate file called an object file
- 2. Linking all the object files and libraries that the program comprises
together, in order to generate a single executable file
Linking process:
- The compiler generates a symbol table for each object file with symbols and
the location of this symbols into the object file
- Solving cross-references consists of associating each unsolved reference
with an external reference
68
Languages and compilers Informática
Linkers
69
Languages and compilers Informática
Linkers
70
Languages and compilers Informática
Interpreters
An interpreter is a computer program that directly executes instructions
written in a programming language. No previous compiling or linking
process
- Examples: Matlab, Perl, Python
In compiled languages there are two important milestones in their life-cycle:
- Compile time
- Execution time
In interpreted language there is no compile time; everything is done at
execution time: program analysis, solving external references, translating
and performing instructions, etc.
- More inefficient
- Easily portable
71
Languages and compilers Informática
72
Languages and compilers Informática
73