Title: CSC: 345 Computer Architecture
1CSC 345 Computer Architecture
- Jane Huang
- Lecture 6
- Instruction Sets
- Addressing Modes
2http//fizbin.eecs.lehigh.edu/mschulte/ece401-01/
lect/my-lec03-p2.pdf
3Instruction Set
- The operation of the CPU is determined by the
instructions it executes ie. Its Instruction Set - An instruction must contain all the information
needed in the instruction cycle.
4Elements of an Instruction
- Operation Code Specifying the operation to be
performed. For example ADD, JUMP, STORE - Source operand reference.
- Result operand reference.
- Next instruction reference.
- An instruction is represented as a series of bits
but as this is not very understandable by a
user, we can also use symbolic notation (ie ADD
instead of 01011101 etc).
4 bits
6 bits
6 bits
Opcode
OperandReference
OperandReference
16 bits
5Instruction Types
- Consider a basic instruction
- x x yLocation 512 stores x, location 513
stores y - With one register (AC)Load AC from Memory
location 512,Add to AC from Memory location
513Store AC back to Memory location 512 - Therefore a single high level instruction might
require multiple instructions at the machine
level. - Machine instructions needed for
- Data processing (Arithmetic, logic)
- Data storage (Memory instructions)
- Data movement (I/O instructions)
- Control (Test and branch instructions)
6http//fizbin.eecs.lehigh.edu/mschulte/ece401-01/
lect/my-lec03-p2.pdf
7Number of Instructions
- Maximum number of addresses that could be needed
4. - 2 source operands
- 1 result operand
- Address of next instruction
8Stacks
- A stack is a pushdown list.
- Access is always from the top ? LIFO list.
- Basic stack operations are PUSH (to put on the
stack) and POP (to get off the stack) - Simple calculation a ab
- Push aPush bAdd
- If stack is accessible to programmers need for
explicit stack oriented instructions. - Stack related addresses often stored in registers
for Stack pointer, Stack base, Stack limit.
(Optional TopElement, SecondElement)
9Expression Evaluation
- Normally mathematical formulas are evaluated
using infix notation in which a binary operation
appears between two operands (eg ab) - In reverse polish (postfix) notation the operator
follows its operands - a b ? ab
- a (b c) ? abc
- (ab) c ? abc
- Postfix expressions can be easily evaluated using
a stack. - Scan from left to right.
- If the element is a variable, push it onto the
stack. - If the element is an operator, pop the top two
items, perform the operation, and push the result
back onto the stack. - For example abc
10Converting from infix ? postfix
- Dijkstra defined an algorithm that uses a stack
to convert from infix to postfix. - Scan infix expression from left to right
performing the following steps - Examine the next element in the input.
- If it is an operand, output it.
- If it is an opening parenthesis, push it onto
the stack. - If it is an operator then
- If the top of the stack is an opening
parenthesis, push the operator. - If it has higher priority than the top of the
stack, push the operator. - Else pop operation from stack to output and
repeat step 4. - Example A (B C) ( D E) / F
Edsger Wybe Dijkstra   1930-2002
11Instruction Set Design
- Many fundamental issues related to instruction
sets is still in dispute - Operation repertoireHow many and which
operations to provide, how complex the operations
should be. - Data typesWhat types of data should be included.
- Instruction formatInstruction length, number of
addresses, size of fields, etc. - RegistersHow many registers should be referenced
by instructions. - AddressingThe mode by which addresses should be
specified.
12Types of Operands
- Major categories of data include
- Addresses
- Numbers
- Characters
- Logical data
Numbers
- Three common types of numerical data
- Integer or fixed point
- Floating point
- Decimal
13Integer Representation
- Binary number system can represent all numbers
using 0 and 1 digits, minus sign (-) and a
decimal point. - - 1101.01012 -13.312510
- Converting from decimal to binary Example
355 - 355/2 177 remainder 1 ?
- 177/2 88 remainder 1 ? 88/2 44
remainder 0 ? 44/2 22 remainder 0 ?
22/2 11 remainder 0 ? - 11/2 5 remainder 1 ?5/2 2 remainder
1 ?2/2 1 ?
111011001100011100011110001111100011
14Integer Representation
- Converting a decimal fraction to binary0.8110
- 0.81 X 2 1.620.62 X 2 1.240.24 X 2
0.480.48 X 2 0.960.96 X 2 1.920.92 X 2
1.84 - Computer storage has no minus signs and periods!
- Sign-magnitude representationLeftmost big holds
the sign bit (1 negative, 0 positive) - Example 310 0112 -310 1112
- Drawbacks include
- Two representations for zero (000,100)
- Addition and subtraction need to consider the
sign of the number and their relative magnitude.
111110110011001110011
15- Twos Complement Representation
-
- Also uses the most significant bit as a sign
bit.Differs from signed magnitude in how the
other bits are interpreted. - To convert from binary ( 0011010) to 2s
complement. - Take the boolean complement of each bit of the
corresponding positive number - 1100101
- Add 1 to the resulting bit pattern viewed as an
unsigned integer. 1100101 1
1100110 - Quick method starting at least significant bit
leave all bits the same until after the first
1. From that point on, flip the bits.
16Twos Complement Arithmetic
- Add 7 4 11
- 7 00111, 4 00100
- 00111 00100 01011
-
-
- Subtract 7 - 4 3
- 7 00111, -4 11100
- 00111 11100 00011
-
-
17Floating-Point Representation
- Fixed point notation supports the representation
of a range of positive and negative numbers
centered around 0. - Range is limited by the number of bits.
- In decimal notation, we used scientific notation.
- 976,000,000,000,000 can be represented as 9.76 X
1014 - 0.0000000000000976 ? 9.76 X 10-14
- Binary numbers can also be represented in the
form - S X B E
-
23 bits
8 bits
Sign of signific-and
Biased exponent
Significand
1.101001 X 210100 0 10010011
101000100000000000000000 1.638125 X
220 -1.101001 X 210100 1 10010011
101000100000000000000000 1.638125 X 220
1.101001 X 2-10100 0 01101011
101000100000000000000000 1.638125 X
220 -1.101001 X 2-10100 1 01101011
101000100000000000000000 1.638125 X 220
18- Decimal Numbers
- For applications that have comparatively little
computation but lots of I/O, it is sometimes
better to keep numbers in decimal format. - Packed Decimal1834 ? 0001 1010 0011 0100
- Characters
- ASCII
- EBCDIC
- Logical Data
- An n-bit data unit is considered as n 1-bit items
- Array of boolean or binary data items.
19Examples of IBM S/390 Data Transfer Operations L
Load (32 bits) Mem ? RegLH Load
halfword (16 bits) Mem ? RegLR Load (32
bits) Reg ? Reg
- Types of Operations
- Data transfer
- Arithmetic
- Logical
- Conversion
- I/O
- System Control
- Transfer of Control
- Data Transfer
- Move, Store, Load, Set, Clear, Push, Pop
- Location of source and destination operands
- Location could be memory, register, stack.
- Length of data to transfer
- Addressing mode (to be discussed later)
- Tradeoffs - Where to indicate the type of
location (opcode spec or operand?)
20- Arithmetic
- Basic arithmetic operations of add, subtract,
multiply, divide. - Orthogonality means these should be provided for
all supported number types. Sometimes certain
operations only provided for fixed point numbers. - Arithmetic occurs in the ALU.
- Data transfer operations may be required to
position operands and to deliver output. - Logical
- Operations for manipulating individual bits of a
word (bit twiddling) - Masking registers (When did we see this???)
- NOT, AND, OR, XOR.
- How could we use XOR to test for equality of two
numbers? - Logical shift (used to isolate fields within a
word).
21- Logical left/right shift (0 shifted in on)Useful
for isolating single fields within a word. - Arithmetic shift treats word as signed integer
and does not shift the bit sign. In 2s
complement a shift to the right ? division by 2,
shift to the left ? multiplication by 2. - Rotate (cyclic shift) brings each bit in turn
into rightmost bit, for testing.
22- Conversion
- Instructions that change the format or operate on
the format of data. - Example convert from decimal to binary (ie
packed decimal to signed integer, or EBCDIC to
ASCII) - System Control
- Can only be executed from a privileged state
- Used by the operating system.
- Example reading or altering a control register
( stack base etc)
23- Transfer of Control
- Many instructions change the sequence of
instruction execution - Operation of these instruction causes the PC to
be updated with a new address. - Program loops
- Decision making
- Procedure calls
- Operation of these instruction causes the PC to
be updated with a new address - Most common transfer-of-control operations
include branch, skip, procedure call. - Branch Instructions One operand is the address
of the next instruction to be executed.BRE R1,
R2, X (Branch to X if R1 is equal to R2.
24Status register bits
Status register enables quick comparison for
branching.
- Bit C (Carry) set to 1 if the end carry C8 is 1,
it is cleared if C8 0. - Bit S (sign) set to 1 if highest order bit F7 is
1 (visa versa for 0) - Bit Z (zero) set to 1 if output of ALU contains
all 0s. - Bit V (overflow) set to 1 if exclusive-OR of last
two carries is equal to 1 (ie condition for
overflow)
25- Procedure Call Instructions
- Procedures are self-contained computer program
that can be called (invoked) from anywhere in the
larger program. - Allows same code to be used multiple times.
(economy and modularity) - Involves two basic instructions
- A call instruction with a branch to the
procedure. - Return instruction (also a branch)
- Note
- A procedure can be called from more than one
location - One procedure may call another procedure
(nesting) - Each procedure call is matched by a return to the
called program. - Where should the return address be stored for
CALL X? - Register (Reg ? PC ?, PC ? X) - where ?
instruction length. - Start of called procedure (X ? PC ?, PC ? X
1) - (Both of these approaches prevent re-entrant
procedures) - Top of stack (more general and powerful
approach)Return address placed on the stack.
26http//fizbin.eecs.lehigh.edu/mschulte/ece401-01/
lect/my-lec03-p2.pdf
27Instruction SetsChapter 11
- The address field in a typical instruction format
are relatively small. - In order to reference a large range of locations
in main memory a variety of addressing
techniques are employed. - Each one involves a trade-off between
- Address range and/or address flexibility
- Number of memory references needed to access the
data - Complexity of the address calculation.
- Common addressing techniques
- Immediate
- Direct
- Indirect
- Register
- Register Indirect
- Displacement
- Stack
28Immediate Addressing
- Operand present in the instruction.
- OPERAND A
- No memory reference other than instruction fetch
is needed. - Limited operand magnitude.
29Direct Addressing
- Address field contains the effective address of
the operand. - EA A
- Common in early computers
- Not common on todays computers.
- Requires only one memory reference and no
special calculation. - Provides only a limited address space of 2K
where k the length of the address field.
30Indirect Addressing
- The address field refers to the the address of a
word in memory. - That word contains a full-length address of the
operand. - EA (A) where (A) means the contents of A
- Address space now increasesto 2N, where N the
word length. - BUT the number of different effective addresses
is limited to 2K. - This works well in a virtual memory environment
in which alleffective address locations can
beconfined to page 0 of a process.
31Register Addressing
- Similar to direct addressing.
- The address field refers to a register rather
than main memory address. - EA R
- Small address field 3-5 bits.
- No memory references required.
- BUT address space is limited.
- Limited number of registers only worthwhile if
the operandwill be used repeatedly. - Interesting register coloring problem.
32Register Indirect Addressing
- Analogous to indirect addressing.
- EA (R)
- Address field refers to a word in memory
containingan address.
33Displacement Addressing
- Very powerful addressing mode.
- Combines capabilities of directaddressing and
register indirectaddressing. - EA A (R)
- Instruction must have two address fields, at
leastone of which is explicit A. - 3 common uses
- Relative addressing in which the PC is the
implicitly referenced register. Therefore EA
displacement relative to the address of the
instruction. - Base-Register Addressing referenced register
contains a memory address, address field contains
a displacement. - Indexing Address field references a main memory
address referenced register contains a positive
displacement from that address.
34Instruction Formats
- Defines the layout of bits in an instruction, in
terms of its constituent parts. - Must include an opcode and explicitly or
explicitly 0 or more operands. - Each explicit operand is referenced using an
addressing mode.
Instruction Length
- Critical decision impacted by memory size, memory
organization, bus structure, CPU complexity, and
CPU speed. - Major trade off powerful instruction repertoire
vs. need to save space. - Programmers want more opcodes, more operands,
more addressing modes, and greater address range.
(shorter programs, easier to write) - Instruction length should be equal to
memory-transfer length (bus) or one should be a
multiple of the other. - Should be a multiple of character length (8
bits), and fixed point numbers.
35Allocation of bits
- How should bits be allocated within the
instruction? - Major trade-off between number of opcodes and the
power of the addressing capability. - Primary issues include
- Number of addressing modesAre these modes
implicit or explicit? - Number of operandsFewer addresses ? longer
more awkward programs. - Register vs. memoryIf certain operations operate
on registers rather than memory.Number of
registers - Number of register setsGeneral purpose registers
vs. specialized set in which the set to be used
is implicit in the operation. - Address rangeRange of memory to address,
addressing modes supported. - Address granularityWord or byte?
36http//fizbin.eecs.lehigh.edu/mschulte/ece401-01/
lect/my-lec03-p2.pdf
37http//fizbin.eecs.lehigh.edu/mschulte/ece401-01/
lect/my-lec03-p2.pdf
38http//fizbin.eecs.lehigh.edu/mschulte/ece401-01/
lect/my-lec03-p2.pdf
39Instruction Set Design(Homework 6 will
partially build on this)
- Design an Instruction Set for a small
programmable embedded device that is used to
control the temperature of a vat used for
manufacturing chemicals. The temperature must
follow a specific pattern during the production
process. The device has a single temperature
sensor. - Consider
- Instruction size
- Instruction format
- Addressing modes you will support
- Opcodes you will support