Unit 2
Unit 2
R f(R, R)
f: shift, load, clear,
increment, add,
subtract, complement,
Registe ALU 1 clock cycle
rs (f)
(R)
and, or, xor, …
INTERNAL HARDWAREORGANIZATION OF A DIGITAL SYSTEM
• Definition of the internal hardware organization of a computer
- Set of registers it contains and their function
- The sequence of microoperations performed on the binary
information stored in the registers
- Control signals that initiate the sequence of
microoperations (to perform the functions)
R2 R1
R1 7 6 5 4 3 2 1
0
Register R
Showing individual bits
15 8 7 0
15 0
R2 PC(H) PC(L)
Load
Transfer occurs here
Bus lines
x
select
y
4-line bus
Bus and Memory Transfers
Bus lines
Load
Reg. R0 Reg. R1 Reg. R2 Reg. R3
D0 D1 D2 D3
Select z E (enable)
w 2x4
Decoder
S0 0
Select 1
S1 2
Enable 3
Bus and Memory Transfers
MEMORY (RAM)
• Memory (RAM) can be thought as a sequential circuits
containing some number of registers
• These registers hold the words of memory
• Each of the r registers is indicated by an address
• These addresses range from 0 to r-1
• Each register (word) can hold n bits of data
• Assume the RAM contains r = 2k words. It needs the following
– n data input lines
data input lines
– n data output lines
– k address lines n
MEMORY WRITE
• To write a value from a register to a location in memory looks like this in
register transfer language:
M[MAR] R1
This causes the following to occur
– The contents of the MAR get sent to the memory address lines
– A Write (= 1) gets sent to the memory unit
– The values in register R1 get sent over the bus to the data input lines of the
memory
– The values get loaded into the specified address in the memory
Bus and Memory Transfers
B3 A3 B2 A2 B1 A1 B0 A0
Binary Adder
FA C3 FA C2 FA C1 FA C0
C4 S3 S2 S1 S0
Binary Adder-Subtractor
B3 A3 B2 A2 B1 A1 B0 A0
C3 C2 C1 C0
FA FA FA FA
C4 S3 S2 S1 S0
A2 A1 A0 1
Binary Incrementer A3
x y x y x y x y
H A H A H A H A
C S C S C S C S
C 4 S 3 S 2 S 1 S 0
ARITHMETIC CIRCUIT
S1
S0
Cin
A0 X0 C0
S1 D0
S0 FA
B0 0 Y0 C1
1
2
3 4x1
MUX
A1 X1 C1
S1 FA D1
S0 Y1 C2
B1 0 4x1
1 MUX
2
3
A2 X2 C2
S1 FA D2
S0
B2 0 4x1 Y2 C3
1 MUX
2
3
A3 X3 C3
S1 D3
S0 FA
B3 0 4x1 Y3 C4
1 MUX
2
3 Cout
0 1
0 0 0 0 0 … 1 1 1
0 1 0 0 0 … 1 1 1
1 0 0 0 1 … 0 1 1
1 1 0 1 0 … 1 0 1
• There are, in principle, 16 different logic functions that can be
defined over two binary input variables
• However, most systems only implement four of these
– AND (), OR (), XOR ( ), Complement/NOT
• The others can be created from combination of these
Logic Microoperations
LIST OF LOGIC MICROOPERATIONS
• List of Logic Microoperations
- 16 different logic operations with 2 binary vars.
- n binary vars →2 2 n functions
Bi
1
4X1
MUX Fi
2
3 Select
S1
S0
Function table
S1 Output -operation
S0
0 F=AB AND
0 F=AB OR
0 F=A XOR
1 B Comple
1 F = A’ ment
0
1
APPLICATIONS OF LOGIC MICROOPERATIONS
1100 At
1010 B
1110 (A A + B)
At+1
• If a bit in B is set to 1, that same position in A
gets set to 1, otherwise that bit in A keeps its
previous value
SELECTIVE COMPLEMENT
1100 At
1010 B
0110 (A A B)
At+1
• If a bit in B is set to 1, that same position in A
gets complemented from its original value,
otherwise it is unchanged
SELECTIVE CLEAR
1100 At
1010 B
0100 (A A
At+1
B’)
• If a bit in B is set to 1, that same position in A
gets set to 0, otherwise it is unchanged
MASK OPERATION
• In a mask operation, the bit pattern in B is
used to clear certain bits in A
1100 At
1010 B
1000 At+1 (A A
B)
• If a bit in B is set to 0, that same position in A
gets set to 0, otherwise it is unchanged
CLEAR OPERATION
1100 At
1010 B
0110 At+1 (A A B)
INSERT OPERATION
• An insert operation is used to introduce a specific bit pattern into A
register, leaving the other bit positions unchanged
• This is done as
– A mask operation to clear the desired bit positions, followed by
– An OR operation to introduce the new bits into the desired
positions
Example
Suppose 1101
you wanted
1000 1011
to introduce
0001 1010
A (Original)
into the low order four bits
of A: 1101 1000 1011 1010 A (Desired)
• A left
shift
operatio
n
Serial
input
LOGICAL SHIFT
• In a logical shift the serial input to the shift is a 0.
• A right logical shift operation:
0
S
MUX H0
0
1
A0
A1 S
MUX H1
0
A2 1
A3
S
MUX H2
0
1
S
MUX H3
0
1
Serial
input (IL)
ARITHMETIC LOGIC SHIFT UNIT
S3
S2
S1 Ci
S0
Arithmetic Di
Circuit
Select
0 4x1
Ci+1 1 MUX Fi
2
3
Bi Logic Ei
Ai Circuit
shr
Ai-1
Ai+1 shl
RAM
15 0
4095
Instruction codes
INSTRUCTIONS
• Program
– A sequence of (machine) instructions
• (Machine) Instruction- are in the form of binary codes known
as instruction codes.
– A group of bits that tell the computer to perform a specific
operation (a sequence of micro-operation)
• The instructions of a program, along with any needed data are
stored in memory
• Each instruction can be divided into different fields.
- Operation code
- Source/destination Operand
- Source Operand address
- Destination Operand address
- Next Instruction address
• Types of operands
- Addresses, Numbers, characters, logical data
Instruction codes
INSTRUCTION FORMAT
• A computer instruction is often divided into two parts
– An opcode (Operation Code) that specifies the operation for that
instruction
– An address that specifies the registers and/or locations in
memory to use for that operation
• In the Basic Computer, since the memory contains 4096 (= 212) words,
we needs 12 bit to specify which memory address this instruction will
use
• In the Basic Computer, bit 15 of the instruction specifies the
addressing mode (0: direct addressing, 1: indirect addressing)
• Since the memory words, and hence the instructions, are 16 bits
long, that leaves 3 bits for the instruction’s opcode
Instruction Format for immediate Instruction Format for direct and indirect
operand address
15 14 12 11
15 12 11 0 I Opcode Address 0
Opcode Address
Addressing
Addressing mode
mode
Instruction codes
ADDRESSING MODES
• The address field of an instruction can represent either
– Direct address: the address in memory of the data to use
(the address of the operand), or
– Indirect address: the address in memory of the address in
of the data to use
memory Direct addressing Indirect addressing
300 1350
457 Operand
1350 Operand
+ +
AC AC
• Immediate
• Direct
• Indirect
• Register
• Register Indirect
• Displacement (Indexed)
• Stack
Immediate Addressing
• Operand is part of instruction
• Operand = address field
• e.g. ADD 5
– Add 5 to contents of accumulator
– 5 is operand
• No memory reference to fetch data
• Fast
• Limited range
Instruction
Opcode Operand
Direct Addressing
• Address field contains address of operand
• Effective address (EA) = address field (A)
• e.g. ADD A
– Add contents of cell A to accumulator
– Look in memory at address A for operand
• Single memory reference to access data
• No additional calculations to work out effective address
• Limited address space
Instruction
Opcode Address A
Memory
Operand
Indirect Addressing
• Memory cell pointed to by address field contains the
address of (pointer to) the operand
• EA = (A)
– Look in A, find address (A) and look there for operand
• e.g. ADD (A)
– Add contents of cell pointed to by contents of A to
accumulator
• Large address space
• 2n where n = word length
• May be nested, multilevel, cascaded
– e.g. EA = (((A)))
• Draw the diagram yourself
• Multiple memory accesses to find operand
• Hence slower
Indirect Addressing Diagram
Instruction
Opcode Address A
Memory
Pointer to operand
Operand
Register Addressing
• Operand is held in register named in address filed
• EA = R
• Limited number of registers
• Very small address field needed
– Shorter instructions
– Faster instruction fetch
• No memory access
• Very fast execution
• Very limited address space
• Multiple registers helps performance
– Requires good assembly programming or compiler
writing
– N.B. C programming
• register int a;
• c.f. Direct addressing
Register Addressing Diagram
Instruction
Opcode Register Address R
Registers
Operand
Register Indirect Addressing
• C.f. indirect addressing
• EA = (R)
• Operand is in memory cell pointed to by contents of register R
• Large address space (2n)
• One fewer memory access than indirect addressing
Instruction
Opcode Register Address R
Memory
Registers
Registers
Base-Register Addressing
• A holds displacement
• R holds pointer to base address
• R may be explicit or implicit
• e.g. segment registers in 80x86
Indexed Addressing
• A = base
• R = displacement
• EA = A + (R)
• Good for accessing arrays
– EA = A + (R)
– R++
y Combinations
• Postindex
EA = (A) + (R)
• Preindex
EA = (A+(R))
• (Draw the diagrams)
Stack Addressing
• Operand is (implicitly) on top of stack
• e.g.
– ADD Pop top two items from stack and add
Pentium Addressing Modes
• Virtual or effective address is offset into segment
– Starting address plus offset gives linear address
– This goes through page translation if paging enabled
• 12 addressing modes available
– Immediate
– Register operand
– Displacement
– Base
– Base with displacement
– Scaled index with displacement
– Base with index and displacement
– Base scaled index with displacement
– Relative
Instruction codes
PROCESSOR REGISTERS
• A processor has many registers to hold instructions,
addresses, data, etc
• The processor has a register, the Program Counter (PC) that holds
the memory address of the next instruction to get
– Since the memory in the Basic Computer only has 4096
locations, the PC only needs 12 bits
• In a direct or indirect addressing, the processor needs to keep
track of what locations in memory it is addressing: The Address
Register (AR) is used for this
– The AR is a 12 bit register in the Basic Computer
• When an operand is found, using either direct or indirect
addressing, it is placed in the Data Register (DR). The
processor then uses this value as data for its operation
• The Basic Computer has a single general purpose register – the
Accumulator (AC)
Instruction codes
PROCESSOR REGISTERS
• The significance of a general purpose register is that it can be
referred to in instructions
– e.g. load AC with the contents of a specific memory location; store
the contents of AC into a specified memory location
• Often a processor will need a scratch register to store
intermediate results or other temporary data; in the Basic
Computer this is the Temporary Register (TR)
• The Basic Computer uses a very simple model of input/output
(I/O) operations
– Input devices are considered to send 8 bits of character data to
the processor
– The processor can send 8 bits of character data to output devices
• The Input Register (INPR) holds an 8 bit character gotten from an
input device
• The Output Register (OUTR) holds an 8 bit character to be send to
an output device
Registers
COMPUTER REGISTERS
Registers in the Basic Computer
11 0
PC
Memory
11 0
4096 x 16
AR
15 0
IR CPU
15 0 15 0
TR DR
7 0 7 0 15 0
OUTR INPR AC
List of BC Registers
DR or MDR 16 Data Register Holds memory operand
AR or MAR 12 Address Register Holds address for memory
AC 16 Accumulator Processor register
IR 16 Instruction Register Holds instruction code
PC 12 Program Counter Holds address of instruction
TR 16 Temporary Register Holds temporary data
INPR 8 Input Register Holds the data read from i/p device
OUTR 8 Output Register Holds the data to be sent to the o/p device
Registers
COMMON BUS
SYSTEM
• The registers in the Basic Computer are
connected using a bus
• This gives a savings in circuitry over
complete connections between registers
Registers
COMMON BUS SYSTEM
S2
S1 Bus
S0
Memory unit 7
4096 x 16
Address
Write
Read AR 1
LD INR CLR
PC 2 S2 S1 S0 Register
LD INR CLR 0 0 0 x
0 0 1 AR
DR 3 0 1 0 PC
0 1 1 DR
LD INR CLR
1 0 0 AC
E 1 0 1 IR
ALU AC 4 1 1 0 TR
1 1 1 Memory
LD INR CLR
INPR
IR 5
LD
TR 6
LD INR CLR
OUTR
Clock
LD
16-bit common bus
Registers
COMMON BUS
SYSTEM
Read
INPR
Memory Write
4096 x 16 S2 S1 S0 Register
E ALU 0 0 0 x
Address
0 0 1 AR
0 1 0 PC
0 1 1 DR
AC 1 0 0 AC
1 0 1 IR
L I C 1 1 0 TR
1 1 1 Memory
L I C L
L I C DR IR L I C
PC TR
AR OUTR LD
L I C
7 1 2 3 4 5 6
• Either one of the registers will have its load signal activated,
or the memory will have its read signal activated
– Will determine where the data from the bus gets
loaded
• The 12-bit registers, AR and PC, have 0’s loaded onto the
bus in the high order 4 bit positions
• When the 8-bit register OUTR is loaded from the bus, the
data comes from the low order 8 bits on the bus
Instructions
COMPUTER INSTRUCTIONS
3x8
decoder
7 6543
210
D0
I Combinational
D7 Control Control
signals
logic
T15
T0
15 14 . . . . 2 1 0
4 x 16
decoder
D3T4: SC
C l oTc0k
0 T1 T2 T3 T4 T0
T0
T1
T2
T3
T4
D3
CLR
SC
INSTRUCTION CYCLE
• In Basic Computer, a machine instruction is executed in the
following cycle:
1. Fetch an instruction from memory
2. Decode the instruction
3. Read the effective address from memory if the
instruction has an indirect address
4. Execute the instruction
• After an instruction is executed, the cycle starts
again at
step 1, for the next instruction
T1 S2
T0 S1 Bus
S0
Memory
7
unit
Address
Read
AR 1
LD
PC 2
INR
IR 5
LD Clock
Common bus
Instrction Cycle
DETERMINE THE TYPE OF INSTRUCTION
Start
SC
T0
AR PC
T1
IR M[AR], PC PC + 1
T2
Decode Opcode in IR(12-14),
AR IR(0-11), I
IR(15)
T3 T3 T3 T3
Execute Execute AR M[AR] Nothing
input-output register-reference
instruction instructi
SC 0 on Execute T4
SC memory-reference
0 instruction
SC 0
D'7IT3: AR M[AR]
D'7I'T3: Nothing
D7I'T3: Execute a register-reference instr.
D7IT3: Execute an input-output instr.
MR Instructions
MEMORY REFERENCE INSTRUCTIONS
AR = 135 135 21
136 Subroutine PC = 136 Subroutine
BSA:
D5T4: M[AR] PC, AR AR + 1
D5T5: PC AR, SC 0
D6T4: DR
M[AR] D6T5: DR
DR + 1
D6T4:
M[AR] DR, if (DR
= 0) then (PC PC
+ 1), SC 0
MR Instructions
D5T5 D6T5
PC AR DR DR + 1
SC 0
D6T6
M[AR] DR
If (DR = 0)
then (PC
PC + 1)
SC 0
I/O and Interrupt
INPUT-OUTPUT AND INTERRUPT
AC
Transmitter
Keyboard interface INPR FGI
INPR Input register - 8 bits
OUTR Output register - 8 bits Serial Communications Path
FGI Input flag - 1 bit Parallel Communications Path
FGO Output flag - 1 bit
IEN Interrupt enable - 1
bit
- The terminal sends and receives serial information
- The serial info. from the keyboard is shifted into INPR
- The serial info. for the printer is stored in the OUTR
- INPR and OUTR communicate with the terminal
serially and with the AC in parallel.
- The flags are needed to synchronize the timing
difference between I/O device and the
computer
I/O and Interrupt
FGI=0 FGO=1
Start Input Start Output
FGI 0
AC Data
yes yes
FGI=0 FGO=0
no no
AC INPR OUTR AC
FGO 0
yes More
Character yes More
Character
n
o no
END
END
INPUT-OUTPUT INSTRUCTIONS
D7IT3 = p
IR(i) = Bi, i = 6, …, 11
p: SC 0 Clear SC
INP pB11: AC(0-7) INPR, FGI 0 Input char. to AC
OUT pB10: OUTR AC(0-7), FGO 0 Output char. from AC
SKI pB9: if(FGI = 1) then (PC PC + 1) Skip on input flag
SKO pB8: if(FGO = 1) then (PC PC + 1) Skip on output flag
ION pB7: IEN 1 Interrupt enable on
IOF pB6: IEN 0 Interrupt enable off
I/O and Interrupt
PROGRAM-CONTROLLED INPUT/OUTPUT
• Program-controlled I/O
- Continuous CPU involvement
I/O takes valuable CPU time
- CPU slowed down to I/O speed
- Simple
- Least hardware
Input
LOOP, SKI
DEV BUN
LOOP INP
Output DEV
- The I/O interface, instead of the CPU, monitors the I/O device.
- When the interface founds that the I/O device is ready for data
transfer, it generates an interrupt request to the CPU
Execute =0
IEN
instructions
=1 Branch to location 1
PC 1
=1
FGI
=0
=1 IEN 0
FGO R0
=0
R
1
1 BUN 0 1 BUN 0
85
Eight addressing modes for the load
instruction
Assembly
Mode Convention Register transfer
86
Typical arithmetic instructions
Name Mnemonic
Increment INC
Decrement DEC
Add ADD
Subtract SUB
Multiply MUL
Divide DIV
Add with carry ADDC
Subtract with borrow SUBB
Negate (2’s NEG
complement)
87
Typical logical and bit manipulation
instructions
Name Mnemonic
Clear CLR
Complement COM
AND AND
OR OR
Exclusive-OR XOR
Clear carry CLRC
Set carry SETC
Complement carry COMC
Enable interrupt EI
Disable interrupt DI
88
Typical shift instructions
Name Mnemonic
Logical shift right SHR
Logical shift left SHL
Arithmetic shift right SHRA
Arithmetic shift left SHLA
Rotate right ROR
Rotate left ROL
Rotate right through carry
RORC Rotate left through carry
ROLC
89
Stack Organization
(Example)
infix 3 * 4 + 5 * 6 = 42
postfix 3 4 * 5 6 * +
12 5 6 * +
12 30 +
• Reverse Polish notation evaluation with a stack.
Stack is the most efficient way for evaluating arithmetic
expressions.
stack evaluation:
Get value
If value is data: push data
Else if value is operation: pop, pop
evaluate and push.
Stack-organized computer does not use an address field for the instructions ADD, and MUL
Instruction: POP X
Evaluate X = ( A + B ) * ( C + D )
PUSH, and POP instructions need an address field to specify the operand
PUSH A TOS A
PUSH B TOS B
ADD TOS ( A B)
PUSH C TOS C
PUSH D TOS D
ADD TOS (C D)
MUL TOS (C D) ( A B)
POP X X TOS
LOAD A AC M [ A]
ADD B AC AC M [B]
STORE T
M [T ] AC
• All operations are done between the AC register and memory operand.
• Advantages: fewer bits are needed to specify the
address. Disadvantages: results in writing long programs.
Two address instructions
• Three address instructions: Memory addresses for the two operands and one
destination need to be specified.
100
CISC characteristics
101
RISC characteristics
• The major characteristics of a RISC
processor are:
– Relatively few instructions
– Relatively few addressing modes
– Memory access limited to load and store
instructions
– All operations done within the registers of the
CPU
– Fixed-length, easily decoded instruction
format
– Single-cycle instruction execution
– Hardwired rather than microprogrammed 102
RISC characteristics(cont.)
• Other characteristics attributed to RISC architecture are:
– A relatively large number of registers in the processor unit
– Use of overlapped register windows to speed-up procedure call
and
return
– Efficient instruction pipeline
– Compiler support for efficient translation of high-level language
programs into machine language programs
• Studies that show improved performance for RISC
architecture do not differentiate between the effects of the
reduced instruction set and the effects of a large register
file.
– A large number of registers in the processing unit are sometimes
associated with RISC processors.
103
Overlapped register windows
• Some computers provide multiple-register banks, and each
procedure is allocated its own bank of registers. This eliminates
the need for saving and restoring register values.
• Some computers use the memory stack to store the parameters
that are needed by the procedure, but this required a memory
access every time the stack is accessed.
• A characteristics of some RISC processors is their use of
overlapped register windows to provide the passing of
parameters and avoid the need for saving and restoring register
values.
• The concept of overlapped register windows is illustrated in Fig.
8-9.
104
Overlapped register
windows(cont.)
• In general, the organization of register windows will have the
following relationships:
number of global registers = G
number of local registers in each window = L
number of registers common to two windows = C
number of windows = W
• The number of registers available for
each window is
calculated as followed:
window size = L + 2C + G
• The total number of registers needed in
the processor is
register file = (L + C)W + G
• The example of Fig. 8-9:
105
Overlapped register windows
R15
Common to D and A
R10
R73
Local to D
R64
R63
Common to C and D
R58
Proc D R57
Local to C
R48
R47
Common to B and C
R42
Proc C R41
Local to B
R32
R31
R9 Common to A and
Common to all R26
R25
B
procedures Proc B Local to A
R0 R16
R15
Global Common to A and
registers R10
D
Proc A 10
6
Berkeley RISC I
• The Berkeley RISC I is a 32-bit integrated circuit CPU.
– It supports 32-bit address and either 8-, 16-, or 32-bit
data.
– It has a 32-bit instruction format and a total of 31
instructions.
– There are three basic addressing modes:
• Register addressing, immediate operand, and relative to
PC addressing for branch instructions.
– It has a register file of 138 registers
• 10 global register and 8 windows of 32registers in each
– The 32 registers in each window have an
organization
similar to the one shown in Fig. 8-9.
107
Berkeley RISC I(cont.)
• Fig. 8-10 shows the 32-bit instruction formats used for
register-to-register instructions and memory access
instructions.
• Seven of the bits in the operation code specify an operation,
and the eighth bit indicates whether to update the status bits
after an ALU operation.
• For register-to-register instructions :
– The 5-bit Rd field select one of the 32 rgisters as a destination for
the result of the operation
– The operation is performed with the data specified in fields Rs
and S2.
– Thus the instruction has a three-address format, but the second
source may be either a register or an immediate operand.
108
Berkeley RISC I(cont.)
• For memory access instructions:
– Rs to specify a 32-bit address in a register
– S2 to specify an offset
– Register R0 contains all 0’s, so it can be used in any
field to specify a zero quantity
• The third instruction format combines the last three
fields to form a 19-bit relative address Y and is
used primarily with the jump and call instructions.
– The COND field replaces the Rd field for jump
instructions and is used to specify one of 16 possible
branch conditions.
109
Berkeley RISC I instruction formats
31 24 23 19 18 14 13 12 5 4 0
8 5 5 8 5
1
8 5 5 1
13
8 5 19
(c) PC relative mode:
110
Instruction set of berkeley RISC I
• The 31 instructions of RISC I are listed in
Table8-12.
• They have been grouped into three
categories:
– The data manipulation instructions:
• Perform arithmetic, logic, and shift operations.
• An immediate operand is symbolized by the
number sign #.
113
Instruction set of berkeley RISC I(cont.)
114
Instruction set of berkeley RISC I(cont.)
Opcode Register transfer Description
Operands
Program
JMP control instructions
COND,S2(Rs) PC Rs + S2 Conditional jump