Lecture 4
Lecture 4
◼ Components of a processor
◼ Instruction format
◼ Instruction cycle
◼ Addressing modes
◼ Nested interrupts
John von Neumann was a consultant on the ENIAC project, the first general purpose computer (operating
in base 10!) developed from 1943-45 by US military to create firing tables with ballistic equations.
Instruction register +
Program counter
Execution of 3
A+B Main memory
0
Operands are in memory
2 types of operations:
✓ Register memory: e.g.,
1) load memory words into
registers or 2) store back
results into memory 2
✓ Register register : ex.
ADD like here Once available, result is
copied into memory
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
+Notes on previous slide 9
About registers
◼ 2 types of registers:
◼ General-purpose registers: rapid access to frequently-used data
such as local variables and calculation results
◼ Special-purpose registers: program counter, instruction register,
FLAGS register, etc (others on next slides)
Some bits of FLAGS are set by the ALU and reflect the status of the most
recent operation:
C — Set when the result caused a Carry out of the leftmost bit.
….
The exact name and the size of this register vary depending on the CPU:
▪ Intel: FLAGS (16 bits), EFLAGS (32 bits), RFLAGS (64 bits)
▪ Known as Program Status Word (PSW) on older IBM CPUs
Branches – value of PC
The flow of control is
the order in which the
instructions
of a program are
executed (also called
“the sequence/flow of
execution”)
Does the for() loop
lead to “jumps” in
Program counter value as a function the PC values?
of time, here no branches in the code.
Von Neumann: “Execution occurs in
a sequential fashion (unless explicitly e.g., for (i=0; i<10;i++) {
modified) from one instruction to the next.” /* do D */
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
}
Sequential Flow of Control and 15
Branches – value of PC
Control flow is
altered by
Program counter value
control
instructions,
such as jump to
another address
Processor- Processor-
memory I/O
Data
Control
processing
Processor- Processor-
memory I/O
Data
Control
processing
◼ Conditional branch:
Whether last instruction’s
result is zero or not can be
read from the FLAGS register
Could you imagine a code snippet that could be translated into the
assembly code located between address 202 and 211?
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Common Instruction Set operations 21
Data-
transfer
instructions
Data-
processing
instructions
continued
Control-flow
instructions
Data-
transfer
(to/from)
IO
of IBM EAS/390
details
◼ If both source and destination are registers, then this is a simple
CPU internal operation
◼ If one or both operands are in memory, then the CPU (i.e., the CU):
1. Calculate the memory address based on addressing mode →
see later in the lecture
2. If the address refers to virtual memory, translate from virtual to
real-memory address →outside the scope of the course
3. Determine whether the addressed data is in cache → see later
in the lecture
4. If not, issue a command to memory module
CPU
CPU
operands
Eg: NOP, RET Eg: JUMP (branch
Eg: ADD with result in one operand Eg: ADD with result in third operand
operands
ADD with result stored in first operand ADD with result stored in first operand
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. What do we observe wrt code size ?
+Number of addresses per instruction 35
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved. What do we observe wrt code size ?
+Number of addresses per instruction 36
◼ Principle: total instruction size remains fixed, but opcode size and
operands number vary:
◼ when the opcode is short, like below, a lot of bits are left to hold operands,
◼ If an instruction has no operands (such as Halt) or few, all/most of the bits can
be used for the opcode.
Keep
Keep 1111 1111 1111 1111
for 8bit opcodes for 16bit opcodes
Memory Models
Memory Models
Instruction execution
execution: fine-
grained view
CIR : current instruction register
= instruction register This figure is
simplified in the
sense that it does
not consider that
instructions can
CU be in cache
= Control Unit, see slide 7
directs the operations of the processor
and other resources such as buses
by providing control signals
machine
Fetch Execute
All numbers in
Hexadecimal Instruction #1
2 at address 300
3 4 Instruction #2
5 6
Instruction #3
Fetch Execute
All numbers in
Hexadecimal Instruction #1
3 4 Instruction #2
5 6
Instruction #3
Fetch Execute
All numbers in
Hexadecimal Instruction #1
4 Instruction #2
5 6
Instruction #3
Fetch Execute
All numbers in
Hexadecimal Instruction #1
Instruction #2
5 6
Instruction #3
Fetch Execute
All numbers in
Hexadecimal Instruction #1
Instruction #2
6
Instruction #3
Fetch Execute
All numbers in
Hexadecimal Instruction #1
Instruction #2
The program
fragment shown adds
the contents of the
memory word at
address 940 to the
contents of the
memory
word at address 941 Instruction #3
and stores the result
in the latter location
Nb:
- not all machines support all addressing modes
- there are other addressing modes not covered here
- up to know we assumed operands are memory addresses or
registers, we will discover they can be values as well
Nb:
- not all machines support all addressing modes
- there are other addressing modes not covered here
- up to know we assumed operands are memory addresses or
registers, we will learn they can be values as well
ADD R1 R2
ADD R1 ADDR
MOV ADDR R1
Direct Addressing: address contains the location
of the operand in memory
✓ the instruction will always access exactly the
same memory location → only for global
variables of a program
✓ Address space limited by operand size!
EAX is a general-purpose
register on Intel platforms
Constants
are
indicated
by the #
sign
BLT ?
An assembly program that computes the sum of the elements of an
array of 1024 integers of 4 bytes. Here constants are prefixed by #
and (R2) means the address contained in register 2. Note that ADD is
used with 2 different addressing modes here.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Memory Indirect Addressing 61
Memory indirect
Interrupts:
other modules (I/O, memory)
and software may interrupt the
normal processing of the processor
Classes of interrupts
Please refer to slide #24 of lecture #3, which is more detailed
Program Generated by some condition that occurs as a result of an instruction
execution, such as arithmetic overflow, division by zero, attempt to
execute an illegal machine instruction, or reference outside a user's
allowed memory space.
Timer Generated by a timer within the processor. This allows the operating
system to perform certain functions on a regular basis.
I /O Generated by an I/O controller, to signal normal completion of an
operation, request service from the processor, or to signal a variety of
error conditions.
Hardware failure Generated by a failure such as power failure or memory parity error.
i
Interrupt
occurs here i+1
The program does not have to do
anything special - the processor and
the operating system are responsible
M for suspending the user program and
then resuming it at the same point.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Instruction cycle with interrupts 66
during X - even if Y is
urgent it will have to
Solution A: Sequential wait until X finishes
Interrupt
interrupt processing handler Y
Interrupt
User program handler X
Nested
interrupt processing:
Interrupt
interrupts have priorities, handler Y
processed.
Interrupt
User program handler X
Solution B: Nested
interrupt processing.
Interrupts have priorities, Interrupt
handler Y
the most urgent can
preempt the less urgent and
get executed first
(b) Nested interrupt processing
pipelining
◼ CPU have abandoned the simple model of fetching, decoding and
executing one instruction at a time
◼ Pipelining: CPU might have separate fetch, decode, and execute
units, so that while it is executing instruction n, it could also be
decoding instruction n + 1 and fetching instruction n + 2 (see (a)).
Instr. N+2 Instr. N+1 Instr. N
A superscalar CPU.
Figure 1-7. (a) A three-stage pipeline. (b) A superscalar CPU.