Computer Organization with MIPS
Total Page:16
File Type:pdf, Size:1020Kb
Rowan University Rowan Digital Works Open Educational Resources University Libraries 7-24-2018 Computer Organization with MIPS Seth D. Bergmann Rowan University Follow this and additional works at: https://rdw.rowan.edu/oer Part of the Computer Sciences Commons DOI: 10.31986/issn.2689-0690_rdw.oer.1008 Let us know how access to this document benefits ouy - share your thoughts on our feedback form. Recommended Citation Bergmann, Seth D., "Computer Organization with MIPS" (2018). Open Educational Resources. 9. https://rdw.rowan.edu/oer/9 This Book is brought to you for free and open access by the University Libraries at Rowan Digital Works. It has been accepted for inclusion in Open Educational Resources by an authorized administrator of Rowan Digital Works. Computer Organization with MIPS Seth D. Bergmann August 9, 2019 2 Preface This book is intended to be used for a first course in computer organization, or computer architecture. It assumes that all digital components can be con- structed from fundamental logic gates. The book begins with number representation schemes and assembly language for the MIPS architecture, including assembler directives, pseudo-operations, and floating point instructions. It then describes the machine language instruc- tion formats, and shows the student how to translate an assembly language program to machine langauge. This is followed by a chapter which describes how to construct an assembler for MIPS. This chapter may be omitted without loss of continuity. There is then an introduction to boolean algebra and digital logic, followed by a design of the MIPS datapath. The book concludes with a description of the memory hierarchy, including cache memory, RAM, and virtual memory. Each section concludes with a list of exercises (solutions are available to instructors who have adopted this text in a course). This book is an open source book. That means that not only is the pdf version available (to potential students and teachers) for free download, but that the original (LaTeX) source files are also available (to potential authors and contributors). Based on the model of open source software, open source for textbooks is a relatively new paradigm in which many authors and contributors can cooperate to produce a high quality product, for no compensation. For details on the rationale of this new paradigm, and citations for other open source textbooks, see the journal Publishing Research Quarterly, Vol. 30, No. 1, March 2014. The source materials and pdf files of this book are licensed with the Creative Commons NonCommercial license, which means that they may be freely used, copied, or modified, but not for financial gain. The source files for this book are available at rdw.rowan.edu (search for Bergmann) and at cs.rowan.edu/∼bergmann/books Secondary Authors Contributors i Contents Preface i 1 ComputersandComputerPrograms 1 1.1 HardwareComponents........................ 2 1.1.1 CentralProcessingUnit . 2 1.1.2 Memory............................ 3 1.1.3 PeripheralDevices ...................... 4 1.2 MachineLanguage .......................... 5 1.3 AssemblyLanguage ......................... 5 1.4 ProgrammingLanguages.. .. .. .. .. .. .. .. .. .. 5 1.5 Exercises ............................... 6 2 Number Systems 8 2.1 BaseTwo-Binary .......................... 8 2.1.1 Binary Arithmetic . 8 2.1.2 Exercises ........................... 10 2.2 Base8-Octal............................. 10 2.2.1 Exercises ........................... 12 2.3 Base16-Hexadecimal........................ 12 2.3.1 Hexadecimal Values in the MIPS Architecture . 14 2.3.2 Exercises ........................... 15 2.4 TwosComplementRepresentation . 16 2.4.1 Exercises ........................... 19 2.5 PowersofTwo ............................ 20 2.5.1 Arithmetic With Powers of Two . 21 2.5.2 Exercises ........................... 22 3 Assembly Language for MIPS 23 3.1 RegistersandRegisterNames . 23 3.1.1 Exercises ........................... 24 3.2 AssemblyLanguageStatements . 24 3.2.1 Exercises ........................... 25 3.3 ArithmeticInstructions . 25 3.3.1 AddInstruction ....................... 26 ii CONTENTS iii 3.3.2 SubtractInstruction . 27 3.3.3 Examplesof Add andSubtract Instructions . 27 3.3.4 SetIfLessThan ....................... 28 3.3.5 Exercises ........................... 29 3.4 LogicalInstructions ......................... 30 3.4.1 LogicalOperations .. .. .. .. .. .. .. .. .. 30 3.4.2 MIPSLogicalInstructions. 32 3.4.3 Exercises ........................... 35 3.5 ShiftInstructions........................... 37 3.5.1 Logical Shift Instructions . 37 3.5.2 Arithmetic Shift Instructions . 38 3.5.3 Common Applications of Shift Instrucrtions . 38 3.5.4 Exercises ........................... 40 3.6 ImmediateInstructions. 41 3.6.1 Add,Load,Move,andPsuedoOps . 41 3.6.2 Logical Immediate Instructions . 44 3.6.3 LoadUpperImmediate . 45 3.6.4 Exercises ........................... 47 3.7 MemoryReferenceInstructions . 48 3.7.1 SymbolicMemoryAddresses . 49 3.7.2 Non-symbolicLoadandStore . 54 3.7.3 Exercises ........................... 56 3.8 TransferofControl.......................... 59 3.8.1 ConditionalTransferofControl: Branch . 59 3.8.2 UnconditionalTransferofControl: Jump . 61 3.8.3 SelectionStructures . 61 3.8.4 IterationStructures-Loops . 64 3.8.5 Exercises ........................... 69 3.9 MemoryArrays............................ 71 3.9.1 Exercises ........................... 72 3.10Functions ............................... 75 3.10.1 Function Calls - jal and jr . 75 3.10.2 FunctionParameters. 80 3.10.3 RegisterConventionsand the CallStack . 81 3.10.4 RecursiveFunctions . 91 3.10.5 Exercises ........................... 92 3.11 StringsandStringFunctions . 95 3.11.1 Initializing Memory with Strings . 95 3.11.2 Byte Instructions - lbu and sb ............... 96 3.11.3 StringProcessing. 97 3.11.4 Exercises ........................... 99 3.12 Multiplication of Whole Numbers . 102 3.12.1 Multiplication with Software . 102 3.12.2 Multiplication with a MIPS Instruction . 104 3.12.3 Exercises ........................... 107 3.13Division ................................110 iv CONTENTS 3.13.1 Division Implemented in Software . 111 3.13.2 Division with a MIPS Instruction . 115 3.13.3 Exercises ........................... 116 3.14 FloatingPointInstructions . 118 3.14.1 Floating Point Registers . 119 3.14.2 Floating Point Instructions . 119 3.14.3 Floating Point Data in Memory . 121 3.14.4 Loading and Storing Floating Point Registers . 122 3.14.5 Floating Point Comparisons . 122 3.14.6 Exercises ........................... 128 3.15 Input, Output, and Other System Calls With MARS . 132 3.15.1 NormalProgramTermination . 133 3.15.2 Inputwithsyscall . 134 3.15.3 Outputwithsyscall . 134 3.15.4 ExampleforInputandOutput . 135 3.15.5 Exercises ........................... 136 4 Machine Language for MIPS 138 4.1 InstructionFormats . 138 4.1.1 Introduction to the Instruction Formats . 139 4.1.2 Exercises ........................... 141 4.2 Showing Binary Fields . 142 4.2.1 Exercises ........................... 143 4.3 PseudoOperations .......................... 144 4.3.1 LoadImmediate ....................... 144 4.3.2 Move .............................144 4.3.3 Not ..............................145 4.3.4 LoadAddress......................... 145 4.3.5 OtherPseudoOperations . 145 4.3.6 Exercises ........................... 146 4.4 RFormatInstructions . 147 4.4.1 AddandSubtractInstructions . 147 4.4.2 LogicalInstructions . 149 4.4.3 ShiftInstructions. 150 4.4.4 Multiply and Divide Instructions . 151 4.4.5 JumpRegister ........................ 152 4.4.6 Exercises ........................... 153 4.5 IFormatInstructions. 154 4.5.1 Immediate instructions using constants . 154 4.5.2 MemoryReferenceInstructions . 155 4.5.3 MemoryReference-Symbolic. 156 4.5.4 ConditionalBranches . 158 4.5.5 Exercises ........................... 163 4.6 JFormatInstructions . 164 4.6.1 Exercises ........................... 165 4.7 FloatingPointDataRepresentation . 166 CONTENTS v 4.7.1 Fixed Point in Binary . 167 4.7.2 IEEE 754 Floating Point Data Representation . 168 4.7.3 Exercises ........................... 170 4.8 FloatingPointInstructions . 171 4.8.1 MIPS Arithmetic Floating Point Instruction Formats . 171 4.8.2 Floating Point Memory Reference Instruction Formats . 172 4.8.3 Floating Point Conditional Branch Instruction Formats . 173 4.8.4 Exercises ........................... 175 5 A MIPS Assembler 177 5.1 Version1-RFormatInstructionsOnly . 178 5.1.1 Version 1a - No Symbolic Registers . 178 5.1.2 Version 1b - Allow Symbolic Registers . 195 5.1.3 Include Directives . 195 5.1.4 Exercises ........................... 200 5.2 Version2-IncludeIandJ FormatInstructions . 201 5.2.1 Version2a-IandJFormatInstructions . 202 5.2.2 Version 2b - Explicit Memory Addresses . 202 5.2.3 Exercises ........................... 204 5.3 Version3-MorePseudoOperations . 204 6 BooleanAlgebraandDigitalLogic 205 6.1 NotationforBooleanFunctions . 205 6.1.1 BooleanExpressions . 206 6.1.2 Minimizing Boolean Expressions . 207 6.1.3 Exercises ........................... 213 6.2 BasicLogicGates........................... 215 6.2.1 ANDGates .......................... 215 6.2.2 ORGates...........................216 6.2.3 Inverters............................ 216 6.2.4 Composition of Logic Gates . 217 6.2.5 SumofProductsLogicDiagrams . 218 6.2.6 WiresandBuses ....................... 218 6.2.7 Exercises ........................... 220 6.3 Combinational Logic Circuits and Components . 222 6.3.1 SignExtend.........................