1、Computer Organization&DesignThe Hardware/Software Interface,2024/2/16,1,2024/2/16,2,2.1 Introduction2.2 Operations of the Computer Hardware2.3 Operands of the Computer Hardware2.4 Signed and Unsigned Numbers2.5 Representing Instructions in the Computer2.6 Logical Operations2.7 Instructions for Makin
2、g Decisions2.8 Supporting Procedures in Computer Hargware2.9 Communicating with People2.10 MIPS Addressing for 32-Bit Immediates and Addresses2.11 Parallelism and Instructions:Synchronization2.12 Translating and Starting a Program2.13 A C Sort Example to Put It All Together2.14 Arrays versus Pointer
3、s,Critical Interface between hardware and softwareAn ISA includes the following Instructions and Instruction Formats格式Data Types,Encodings,and RepresentationsProgrammable Storage:Registers and MemoryAddressing Modes:to address Instructions and DataHandling Exceptional Conditions(like division by zer
4、o)Examples(Versions)First Introduced inIntel(8086,80386,Pentium,.)1978 MIPS(MIPS I,II,III,IV,V)1986PowerPC(601,604,)1993,Instruction Set Architecture(ISA),Instructions,Instructions are the language of the machineWe will study the MIPS instruction set architectureKnown as Reduced Instruction Set Comp
5、uter(RISC)Elegant and relatively simple designSimilar to RISC architectures developed in mid-1980s and 90sVery popular,used in many productsSilicon Graphics,ATI,Cisco,Sony,etc.Comes next in sales after Intel IA-32 processorsAlmost 100 million MIPS processors sold in 2002(and increasing)Alternative d
6、esign:Intel IA-32Known as Complex Instruction Set Computer(CISC),MIPS:Microprocessor without Interlocked Piped Stages无内部互锁流水级的微处理器,2024/2/16,5,Basics of RISC Design,All instructions are typically of one sizeFew instruction formatsArithmetic instructions are register to registerOperands are read from
7、 registersResult is stored in a registerGeneral purpose integer and floating point registersTypically,32 integer and 32 floating-point registersMemory access only via load and store instructionsLoad and store:bytes,half words,words,and double words Few simple addressing modes,Instruction Set,The rep
8、ertoire of instructions of a computerDifferent computers have different instruction setsBut with many aspects in commonEarly computers had very simple instruction setsSimplified implementationMany modern computers also have simple instruction sets,The MIPS Instruction Set,Used as the example through
9、out the courseStanford MIPS commercialized by MIPS Technologies()Large share of embedded core marketApplications in smartphones,tablets,consumer electronics,network/storage equipment,cameras,printers,Logical View of the MIPS Processor,32 General Purpose Registers(GPRs)32-bit registers are used in MI
10、PS32Register 0 is always zeroAny value written to R0 is discardedSpecial-purpose registers LO and HIHold results of integer multiply and divideSpecial-purpose program counter PC32 Floating Point Registers(FPRs)Floating Point registers can be either 32-bit or 64-bitA pair of registers is used for dou
11、ble-precision floating-point,Overview of the MIPS Registers,GPRs$0$31,FPRs$F0$F31,MIPS General-Purpose Registers,32 General Purpose Registers(GPRs)Assembler uses the dollar notation to name registers$0 is register 0,$1 is register 1,and$31 is register 31All registers are 32-bit wide in MIPS32Registe
12、r$0 is always zeroAny value written to$0 is discardedSoftware conventionsSoftware defines names to all registersTo standardize their use in programs$8-$15 are called$t0-$t7Used for temporary values$16-$23 are called$s0-$s7,MIPS Register Conventions,Assembler can refer to registers by name or by numb
13、erIt is easier for you to remember registers by nameAssembler converts register name to its corresponding number,Instruction Formats,All instructions are 32-bit wide,Three instruction formats:Register(R-Type)Register-to-register instructionsOp:operation code specifies the format of the instructionIm
14、mediate(I-Type)16-bit immediate constant is part in the instructionJump(J-Type)Used by jump instructions,R-Type Format,Op:operation code(opcode)Specifies the operation of the instructionAlso specifies the format of the instructionfunct:function code extends the opcodeUp to 26=64 functions can be def
15、ined for the same opcodeMIPS uses opcode 0 to define R-type instructionsThree Register Operands(common to many instructions)Rs,Rt:first and second source operandsRd:destination operandsa:the shift amount used by shift instructions,R-Type Format,Op:operation code(opcode)Specifies the operation of the
16、 instructionAlso specifies the format of the instructionfunct:function code extends the opcodeUp to 26=64 functions can be defined for the same opcodeMIPS uses opcode 0 to define R-type instructionsThree Register Operands(common to many instructions)Rs,Rt:first and second source operandsRd:destinati
17、on operandsa:the shift amount used by shift instructions,I-Type Format,Constants are used quite frequently in programsThe R-type shift instructions have a 5-bit shift amount constant What about other instructions that need a constant?I-Type:Instructions with Immediate Operands16-bit immediate consta
18、nt is stored inside the instructionRs is the source register numberRt is now the destination register number(for R-type it was Rd)Examples of I-Type ALU Instructions:Add immediate:addi$s1,$s2,5#$s1=$s2+5OR immediate:ori$s1,$s2,5#$s1=$s2|5,J-Type Format,J-type format is used for unconditional jump in
19、struction:j label#jump to label.label:26-bit immediate value is stored in the instructionImmediate constant specifies address of target instructionProgram Counter(PC)is modified as follows:Next PC=Upper 4 most significant bits of PC are unchanged,Instruction Categories,Integer ArithmeticArithmetic,l
20、ogical,and shift instructionsData TransferLoad and store instructions that access memoryData movement and conversionsJump and BranchFlow-control instructions that alter the sequential sequenceFloating Point ArithmeticInstructions that operate on floating-point registersMiscellaneousInstructions that
21、 transfer control to/from exception handlersMemory management instructions,2024/2/16,19,2.1 Introduction2.2 Operations of the Computer Hardware2.3 Operands of the Computer Hardware2.4 Signed and Unsigned Numbers2.5 Representing Instructions in the Computer2.6 Logical Operations2.7 Instructions for M
22、aking Decisions2.8Supporting Procedures in Computer Hargware2.9 Communicating with People2.10 MIPS Addressing for 32-Bit Immediates and Addresses2.11 Parallelism and Instructions:Synchronization2.12 Translating and Starting a Program2.13 A C Sort Example to Put It All Together2.14 Arrays versus Poin
23、ters,Arithmetic Operations,Add and subtract,three operandsTwo sources and one destinationadd a,b,c#a gets b+cAll arithmetic operations have this formDesign Principle 1:Simplicity favours regularityRegularity makes implementation simplerSimplicity enables higher performance at lower cost,Arithmetic E
24、xample,C code:f=(g+h)-(i+j);Compiled MIPS code:add t0,g,h#temp t0=g+hadd t1,i,j#temp t1=i+jsub f,t0,t1#f=t0-t1,2024/2/16,22,2.1 Introduction2.2 Operations of the Computer Hardware2.3 Operands of the Computer Hardware2.4 Signed and Unsigned Numbers2.5 Representing Instructions in the Computer2.6 Logi
25、cal Operations2.7 Instructions for Making Decisions2.8Supporting Procedures in Computer Hargware2.9 Communicating with People2.10 MIPS Addressing for 32-Bit Immediates and Addresses2.11 Parallelism and Instructions:Synchronization2.12 Translating and Starting a Program2.13 A C Sort Example to Put It
26、 All Together2.14 Arrays versus Pointers,Register Operands,Arithmetic instructions use register operandsMIPS has a 32 32-bit register fileUse for frequently accessed dataNumbered 0 to 3132-bit data called a“word”Assembler names$t0,$t1,$t9 for temporary values$s0,$s1,$s7 for saved variablesDesign Pri
27、nciple 2:Smaller is fasterReason for small register filec.f.main memory:millions of locations,Register Operand Example,C code:f=(g+h)-(i+j);f,j in$s0,$s4Compiled MIPS code:add$t0,$s1,$s2add$t1,$s3,$s4sub$s0,$t0,$t1,Memory Operands,Main memory used for composite dataArrays,structures,dynamic data,Onl
28、y a small amount of data can fit in registersTo apply arithmetic operationsLoad values from memory into registersStore result from register to memoryMemory is byte addressedEach address identifies an 8-bit byteWords are aligned in memoryAddress must be a multiple of 4MIPS is Big EndianMost-significa
29、nt byte at least address of a wordc.f.Little Endian:least-significant byte at least address,Big vs.Little Endian,Big Endian:leftmost byte is word address IBM 360/370,Motorola 68k,MIPS,Sparc,HP PALittle Endian:rightmost byte is word addressIntel 80 x86,DEC Vax,DEC Alpha(Windows NT),Memory Operand Exa
30、mple 1,C code:g=h+A8;g in$s1,h in$s2,base address of A in$s3Compiled MIPS code:Index 8 requires offset of 324 bytes per wordlw$t0,32($s3)#load wordadd$s1,$s2,$t0,offset,base register,Memory Operand Example 2,C code:A12=h+A8;h in$s2,base address of A in$s3Compiled MIPS code:Index 8 requires offset of
31、 32lw$t0,32($s3)#load wordadd$t0,$s2,$t0sw$t0,48($s3)#store word,Registers vs.Memory,Registers are faster to access than memoryOperating on memory data requires loads and storesMore instructions to be executedCompilers use registers for variables as much as possibleOnly“spill”to memory for less freq
32、uently used variablesRegister optimization is important!,Immediate Operands,Constant data specified in an instructionaddi$s3,$s3,4No subtract immediate instructionJust use a negative constantaddi$s2,$s1,-1Design Principle 3:Make the common case fastSmall constants are commonImmediate operand avoids
33、a load instruction,The Constant Zero,MIPS register 0($zero)is the constant 0Cannot be overwrittenUseful for common operationsE.g.,move between registersadd$t2,$s1,$zero,2024/2/16,32,2.1 Introduction2.2 Operations of the Computer Hardware2.3 Operands of the Computer Hardware2.4 Signed and Unsigned Nu
34、mbers2.5 Representing Instructions in the Computer2.6 Logical Operations2.7 Instructions for Making Decisions2.8Supporting Procedures in Computer Hargware2.9 Communicating with People2.10 MIPS Addressing for 32-Bit Immediates and Addresses2.11 Parallelism and Instructions:Synchronization2.12 Transla
35、ting and Starting a Program2.13 A C Sort Example to Put It All Together2.14 Arrays versus Pointers,Unsigned Binary Integers,Given an n-bit number,Range:0 to+2n 1Example0000 0000 0000 0000 0000 0000 0000 10112=0+123+022+121+120=0+8+0+2+1=1110Using 32 bits0 to+4,294,967,295,Twos Complement Signed Inte
36、gers,Given an n-bit number,Range:2n 1 to+2n 1 1Example1111 1111 1111 1111 1111 1111 1111 11002=1231+1230+122+021+020=2,147,483,648+2,147,483,644=410Using 32 bits2,147,483,648 to+2,147,483,647,Twos Complement Signed Integers,Bit 31 is sign bit1 for negative numbers0 for non-negative numbers(2n 1)cant
37、 be representedNon-negative numbers have the same unsigned and twos complement representationSome specific numbers 0:0000 0000 00001:1111 1111 1111Most-negative:1000 0000 0000Most-positive:0111 1111 1111By default,all data in MIPS is a signed integeradd vs.addu,Signed Negation,Complement and add 1Co
38、mplement means 1 0,0 1,Example:negate+2+2=0000 0000 001022=1111 1111 11012+1=1111 1111 11102,Sign Extension,How to represent a number using more bits?e.g.16 bit number as a 32 bit numberMust preserve the numeric(数字的)valueReplicate(复制)the sign bit to the leftc.f.unsigned values:extend with 0sExamples
39、:8-bit to 16-bit+2:0000 0010=0000 0000 0000 00102:1111 1110=1111 1111 1111 1110In MIPS instruction setaddi:extend immediate valuelb,lh:extend loaded byte/halfwordbeq,bne:extend the displacement,2024/2/16,38,2.1 Introduction2.2 Operations of the Computer Hardware2.3 Operands of the Computer Hardware2
40、.4 Signed and Unsigned Numbers2.5 Representing Instructions in the Computer2.6 Logical Operations2.7 Instructions for Making Decisions2.8Supporting Procedures in Computer Hargware2.9 Communicating with People2.10 MIPS Addressing for 32-Bit Immediates and Addresses2.11 Parallelism and Instructions:Sy
41、nchronization2.12 Translating and Starting a Program2.13 A C Sort Example to Put It All Together2.14 Arrays versus Pointers,Representing Instructions,Instructions are encoded in binaryCalled machine codeMIPS instructionsEncoded as 32-bit instruction wordsSmall number of formats encoding operation co
42、de(opcode),register numbers,Regularity!Register numbers$t0$t7 are regs 8 15$t8$t9 are regs 24 25$s0$s7 are regs 16 23,MIPS-32 ISA,Instruction CategoriesComputational Load/StoreJump and BranchFloating PointcoprocessorMemory ManagementSpecial,R0-R31,PC,HI,LO,Registers,MIPS R-format Instructions,Instru
43、ction fieldsop:operation code(opcode)rs:first source register numberrt:second source register numberrd:destination register numbershamt:shift amount(00000 for now)funct:function code(extends opcode),R-format Example,add$t0,$s1,$s2,special,$s1,$s2,$t0,0,add,0,17,18,8,0,32,000000,10001,10010,01000,000
44、00,100000,0000 0010 0011 0010 0100 0000 0010 00002=0232402016,Hexadecimal,Base 16Compact representation of bit strings4 bits per hex digit,Example:eca8 64201110 1100 1010 1000 0110 0100 0010 0000,MIPS I-format Instructions,Immediate arithmetic and load/store instructionsrt:destination or source regi
45、ster numberConstant:215 to+215 1Address:offset added to base address in rsDesign Principle 4:Good design demands good compromises(折中)Different formats complicate decoding,but allow 32-bit instructions uniformlyKeep formats as similar as possible,Two Key Principles of Computer Design,The BIG Picture,
46、Instructions are represented as numbers and,as such,are indistinguishable from dataPrograms are stored in alterable memory(that can be read or written to)just like data,Stored-program conceptPrograms can be shipped as files of binary numbers binary compatibility(兼容性)Computers can inherit(继承)ready-ma
47、de software provided they are compatible with an existing ISA leads industry to align(匹配)around a small number of ISAs,2024/2/16,46,2.1 Introduction2.2 Operations of the Computer Hardware2.3 Operands of the Computer Hardware2.4 Signed and Unsigned Numbers2.5 Representing Instructions in the Computer
48、2.6 Logical Operations2.7 Instructions for Making Decisions2.8Supporting Procedures in Computer Hargware2.9 Communicating with People2.10 MIPS Addressing for 32-Bit Immediates and Addresses2.11 Parallelism and Instructions:Synchronization2.12 Translating and Starting a Program2.13 A C Sort Example t
49、o Put It All Together2.14 Arrays versus Pointers,Logical Operations,Instructions for bitwise manipulation,Useful for extracting and inserting groups of bits in a word,Shift Operations,shamt:how many positions to shift Shift left logicalShift left and fill with 0 bitssll by i bits multiplies by 2iShi
50、ft right logicalShift right and fill with 0 bitssrl by i bits divides by 2i(unsigned only),AND Operations,Useful to mask bits in a wordSelect some bits,clear others to 0and$t0,$t1,$t2,0000 0000 0000 0000 0000 1101 1100 0000,0000 0000 0000 0000 0011 1100 0000 0000,$t2,$t1,0000 0000 0000 0000 0000 110