Discovering Computers 2009 Chapter 8 Operating Systems and Utility Programs.
Computers and Programs Machine Codeweb.eecs.utk.edu/~mbeck/classes/cs160/lectures/10_isa.pdf ·...
Transcript of Computers and Programs Machine Codeweb.eecs.utk.edu/~mbeck/classes/cs160/lectures/10_isa.pdf ·...
Ward 1CS 160
Instruction Set Architecture
Ward 2CS 160
Background & Introduction
Ward 3CS 160
Computers and Programs
• A simplified model of a computer consists of a processing unit (CPU) and a memory.
• CPU can understand simple instructions:– read/write a memory location– add two numbers– compare numbers– etc.
Ward 4CS 160
Machine Code
• An executable program is a sequence of these simple instructions.
• The sequence is stored in memory (Von Neumann architecture).
• The CPU processes the simple instructions sequentially.– Some instructions can tell the CPU to jump
to a new place in memory to get the next instruction.
Ward 5CS 160
Instructions
• Each instruction is stored in memory as a bunch of bits.
• The CPU decodes the bits to determine what should happen.
• For example, the instruction to add 2 numbers might look like this:
10100110
Ward 6CS 160
Sample Program in Words
# Instruction1 Set memory[801] to hold 000000012 Set memory[802] to hold 000000003 If memory[802] = 10 jump to instruction #84 increment memory[802]5 set memory[803] to 2 times memory[801]6 put memory[803] in to memory[801]7 jump to instruction #38 print memory[801]
Ward 7CS 160
A Picture
CPUCPU
Instruction #1Instruction #2Instruction #3Instruction #4Instruction #5Instruction #6...
...
MEMORY
012345
801802803
Address
Ward 8CS 160
Human vs Machine Programs
• The computer can only understand the bits (the encoded program)
• Humans don't like to deal with bits, so they developed English-like abbreviations for programs.
• Typically, one-to-one relationship between the two
Machine Language
Assembly Language
Ward 9CS 160
Assembly & Machine Language
ST 1,[801]ST 0,[802]
TOP: BEQ [802],10,BOTINCR [802]MUL [801],2,[803]ST [803],[801]JMP TOP
BOT: LD A,[801]CALL PRINT
Assembly Language
00100101 1101001100100100 1101010010001010 01001001 1111000001000100 0101010001001000 10100111 1010001111100101 10101011 00000010001010011101010111010100 1010100010010001 01000100
Machine Language
Our Sample Program
Ward 10CS 160
An Assembler
AssemblyLanguageProgram
ST 1,[801]. . .
AssemblyLanguageProgram
ST 1,[801]. . .
MachineLanguageProgram
0100100110010100
MachineLanguageProgram
0100100110010100
AssemblerAssembler
Ward 11CS 160
Higher-Level Languages
• Assembly Language is much easier to deal with than Machine Language, but you still need to know about all the instructions.
• People/Companies have developed languages that were independent of the specific machine language (i.e., portable).– More abstract representation of
instructions.
Ward 12CS 160
High-Level Languages
• Many high-level languages have been developed.– Different ways of representing
computations.– Different languages for different needs:
• symbolic vs. numeric computation• human efficiency vs. program efficiency• portability• extensibility
Ward 13CS 160
Examples of High Level Languages
• C• C++• Fortran• Java• Ada• Basic• Algo
Some High-Level Languages are “higher” than others.
Ward 14CS 160
Sample Program In C
set memory[801] to hold 00000001set memory[802] to hold 00000000if memory[802] = 10 jump to instruction #8increment memory[802]set memory[803] to 2 times memory[801]put memory[803] in to memory[801]jump to instruction #3print memory[801]
x=1;i=0;while (i!=10) {
i++;x=x*2;
}
printf("%d",x);
}
Ward 15CS 160
Compiler
C Program
int main() {int i=1;. . .
C Program
int main() {int i=1;. . .
MachineLanguageProgram
0100100110010100
MachineLanguageProgram
0100100110010100
C CompilerC Compiler
Created with text editor or development environment
Ward 16CS 160
Many Different Compilers
• Sample C Compilers:– GNU gcc– Portland pgcc– Keil C– Intel Win32 C – Sun C– Fujitsu C– IBM AIX C– . . .
Ward 17CS 160
Instruction Set Characteristics & Functions
Ward 18CS 160
Elements of an Instruction
• Operation code (Op code)– Do this
• Source Operand(s) reference– To this
• Result Operand reference– Put the answer here
• Next Instruction Reference (rare)– When you have done that, do this...
Ward 19CS 160
Instruction Cycle State Diagram(without Interrupts)
Ward 20CS 160
Simple Instruction Format
• How many instructions (Opcodes) can we have in our Instruction Set?
• How much memory (or virtual memory) can we directly access in our system?
24 = 16
26 = 64
Ward 21CS 160
Number of Addresses [1]
• 3 addresses– Operand 1, Operand 2, Result– a = b + c;– May be a fourth - next instruction (rare -
usually implicit)– Needs very long words to hold everything.
Why?– Typically indirect addressing using registers
Ward 22CS 160
Number of Addresses [2]
• 2 addresses– One address doubles as operand and result– a = a + b– Reduces length of instruction– Requires some extra work
• Temporary storage to hold some results
Ward 23CS 160
Number of Addresses [3]
• 1 address– Implicit second address– Usually a register (accumulator)– Common on early machines
Ward 24CS 160
Number of Addresses [4]
• 0 (zero) addresses– All addresses implicit– Uses a stack– e.g. push a– push b– add– pop c
– c = a + b
Ward 25CS 160
How Many Addresses
• More addresses– More complex (powerful?) instructions– More registers
• Inter-register operations are quicker
– Fewer instructions per program
• Fewer addresses– Less complex (powerful?) instructions– More instructions per program– Faster fetch/execution of instructions
Ward 26CS 160
Types of Operand
• Addresses• Numbers
– Integer/floating point
• Characters– ASCII, etc.
• Logical Data– Bits or flags
Ward 27CS 160
Operations
Ward 28CS 160
Instructions: Types of Operation
• Data Transfer• Arithmetic• Logical• Conversion• I/O• Transfer of Control• System Control
• Data Access & Transfer • Arithmetic• Logical• Floating-Point • Conditional &
Unconditional Branch• Processor Control
Two different classifications
Ward 29CS 160
Common Data Transfer Operations
• Specify– Source– Destination– Amount of data
• May be different instructions for different movements– e.g. IBM 370
• Or one instruction and different addresses– e.g. VAX
Ward 30CS 160
Common Arithmetic Operations
• Add, Subtract, Multiply, Divide• Signed Integer• Floating point ?• May include
– Increment (a++)– Decrement (a--)– Negate (-a)
Ward 31CS 160
Common Shift and Rotate Operations
Ward 32CS 160
Common Logical Operations
• Bitwise operations• AND, OR, NOT
Ward 33CS 160
Common Conversion Operations
• E.g. Binary to Decimal
Ward 34CS 160
Common Input/Output Operations
• May be specific instructions• May be done using data movement
instructions (memory mapped)• May be done by a separate controller
(DMA)
Ward 35CS 160
Common Transfer of Control Operations
• Branch– e.g. branch to x if result is zero
• Skip– e.g. increment and skip if zero
ISZ Register1Branch xxxxADD A
• Subroutine call– c.f. interrupt call
Ward 36CS 160
Branch Instruction
Ward 37CS 160
Nested Procedure Calls
Ward 38CS 160
Use of Stack
Ward 39CS 160
Common Systems Control Operations
• Privileged instructions• CPU needs to be in specific state
– Ring 0 on 80386+– Kernel mode
• For operating systems use
Ward 40CS 160
Example Instruction Set
MIPS Instruction Set
• Early RISC Design• Design goals:
– Speed• Ensure one instruction can complete every
clock cycle
– Minimalistic• Contain the fewest possible instructions
necessary
Ward 41CS 160
MIPS Instruction Set (Part I)
Ward 42CS 160
MIPS Instruction Set (Part II)
Ward 43CS 160
MIPS Floating-Point Instructions
Ward 44CS 160
Aesthetics of Instruction Sets
• Elegance– Balanced– No frivolous or useless instructions
• Orthogonality– No unnecessary duplication– No overlap among instructions
Ward 45CS 160
Prinicple of Orthogonality
The principle of orthogonality specifies that each instruction should perform a unique task without duplicating or overlapping the function of other instructions
Ward 46CS 160
Typical Simple RISC Design
• Instructions designed to– Complete in one clock cycle
• Performs a basic computation
– Support restricted set of operations• Register operations• Load/Store operations
– Fixed instruction length
Ward 47CS 160
Addressing Modes
Ward 48CS 160
Addressing Modes
• Immediate• Direct• Register• Indirect
– Memory– Register
• Displacement/Indexed – various types • Stack
Ward 49CS 160
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
Ward 50CS 160
Immediate Addressing Diagram
OperandOpcode
Instruction
Ward 51CS 160
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
Ward 52CS 160
Direct Addressing Diagram
Address AOpcode
Instruction
Memory
Operand
Ward 53CS 160
Register Addressing (1)
• 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
• Very limited address space– Number of registers much smaller than
memory
Ward 54CS 160
Register Addressing (2)
• No memory access• Very fast execution• Large number of registers helps
performance– Requires good assembly programming or
compiler writing• Good (multiple) use of operands after in registers
– C programming • register int a;
• Compare: direct addressing
Ward 55CS 160
Register Addressing Diagram
Register Address ROpcode
Instruction
Registers
Operand
Ward 56CS 160
Memory Indirect Addressing [1]
• 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
Ward 57CS 160
Memory Indirect Addressing [2]
• 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
Ward 58CS 160
Memory Indirect Addressing Diagram
Address AOpcode
Instruction
Memory
Operand
Pointer to operand
Ward 59CS 160
Register Indirect Addressing
• Compare: 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
Ward 60CS 160
Register Indirect Addressing Diagram
Register Address ROpcode
Instruction
Memory
OperandPointer to Operand
Registers
Ward 61CS 160
Displacement Addressing
• EA = A + (R)• Address field hold two values
– A = base value– R = register that holds displacement– or vice versa
Ward 62CS 160
Displacement Addressing Diagram
Register ROpcode
Instruction
Memory
OperandPointer to Operand
Registers
Address A
+
Ward 63CS 160
Relative Addressing
• A version of displacement addressing• R = Program counter, PC• EA = A + (PC)• i.e. get operand from A cells from
current location pointed to by PC• Compare: locality of reference & cache
usage
Ward 64CS 160
Base-Register Addressing
• Version of displacement addressing• A holds displacement• R holds pointer to base address• R may be explicit or implicit• Useful for virtual memory and paging
Ward 65CS 160
Indexed Addressing
• A = base• R = displacement• EA = A + R• Good for accessing arrays
– EA = A + R– R++
Ward 66CS 160
Stack Addressing
• Operand is (implicitly) on top of stack• e.g.
– ADD Pop top two items from stackand add
Ward 67CS 160
Types of Operands
Ward 68CS 160
Implicit Encoding
• For a given opcode, the type of each operand is fixed
• More opcodes required
• Example– add_signed_immediate_to_register
Ward 69CS 160
Examples of Implicit
Ward 70CS 160
Explicit Encoding
• Operand specifies type and value
• Fewer opcodes required
• Example– Opcode is add, operands specify register
and immediate
Ward 71CS 160
Examples of Explicit
R1 = R1 + R2
R1 = R1 + -93
Ward 72CS 160
Combination
• Some processors provide hardware that can compute an operand value from multiple sources
• Typically, a sum
• Example– register-offset specifies register and
immediate value– Processor adds immediate value to
contents of register
Ward 73CS 160
Example
R2 = R2-17 + R4+76
Useful when referencing a data aggregate such as C language struct
Ward 74CS 160
Tradeoffs for Operand Types
• No single style of operands optimal for all purposes– All styles discussed have been implemented
• Tradeoff among– Ease of programming– Fewer instructions– Smaller instructions– Larger range of immediate values– Faster operand fetch and decode– Decreased hardware size
Ward 75CS 160
Some Common ISAs• Alpha AXP (DEC Alpha) • ARM (Acorn RISC Machine) (Advanced RISC Machine now ARM
Ltd) • IA-64 (Itanium) • MIPS• Motorola 68k• PA-RISC (HP Precision Architecture) • IBM POWER• PowerPC• SPARC• SuperH• System/360• Tricore (Infineon) • Transputer (STMicroelectronics) • VAX (Digital Equipment Corporation) • x86 (IA-32, Pentium, Athlon) (AMD64, EM64T)
Source: http://www.answers.com/topic/instruction-set