1 Data Representation 1 1.1 Representing Integers . 1 1.1.1 Unsigned Binary Numbers 1 1.1.1.1 Conversion of Binary to Decimal . 2 1.1.1.2 Conversion of Decimal to Binary . 4 1.1.1.3 Addition of Unsigned Binary Numbers 4 1.1.2 Signed Binary Numbers . 6 1.1.2.1 Addition and Subtraction of Signed Binary Numbers 8 1.1.2.2 Shifting Signed Binary Numbers . 9 1.1.2.3 Hexadecimal Notation . 9 1.2 Representing Characters . 10 1.3 Representing Programs 11 1.4 Memory Organization 12 1.4.1 Units of Memory . 13 1.4.1.1 Historical Perspective . 13 1.4.2 Addresses and Pointers 13 1.4.3 Summary . 14 1.5 Exercises 15 1.5.1 . 15 1.5.2 . 15 1.5.3 . 15 2 MIPS Tutorial 17 2.1 What is Assembly Language? 17 2.2 Getting Started: add.asm 18 2.2.1 Commenting 18 2.2.2 Finding the Right Instructions 19 2.2.3 Completing the Program . 20 2.2.3.1 Labels and main 20 2.2.3.2 Syscalls . 22 2.3 Using SPIM 23 2.4 Using syscall: add2.asm 24 2.4.1 Reading and Printing Integers 25 2.5 Strings: the hello Program . 26 2.6 Conditional Execution: the larger Program 28 2.7 Looping: the multiples Program 31 2.8 Loads: the palindrome.asm Program 33 2.9 The atoi Program 36 2.9.1 atoi-1 . 36 2.9.2 atoi-2 . 38 2.9.3 atoi-3 . 39 2.9.4 atoi-4 . 39 2.10 Exercises 42 2.10.1 . 42 2.10.2 . 42 2.10.3 . 42 3 Advanced MIPS Tutorial 43 3.1 Function Environments and Linkage . 43 3.1.1 Computing Fibonacci Numbers . 45 3.1.1.1 Using Saved Registers: fib-s.asm 45 3.1.1.2 Using Temporary Registers: fib-t.asm . 47 3.1.1.3 Optimization: fib-o.asm . 48 3.2 Structures and sbrk: the treesort Program 50 3.2.1 Representing Structures . 51 3.2.2 The sbrk syscall . 52 3.3 Exercises 53 3.3.1 . 53 3.3.2 . 53 3.3.3 . 53 3.3.4 . 53 3.3.5 . 54 4 The MIPS R2000 Instruction Set 55 4.1 A Brief History of RISC . 55 4.2 MIPS Instruction Set Overview . 56 4.3 The MIPS Register Set 57 4.4 The MIPS Instruction Set 57 4.4.1 Arithmetic Instructions 59 4.4.2 Comparison Instructions . 60 4.4.3 Branch and Jump Instructions 60 4.4.3.1 Branch . 60 4.4.3.2 Jump 61 4.4.4 Load, Store, and Data Movement 61 4.4.4.1 Load . 61 4.4.4.2 Store . 62 4.4.4.3 Data Movement . 63 4.4.5 Exception Handling 63 4.5 The SPIM Assembler . 64 4.5.1 Segment and Linker Directives 64 4.5.2 Data Directives 65 4.6 The SPIM Environment . 65 4.6.1 SPIM syscalls . 65 4.7 The Native MIPS Instruction Set 65 4.8 Exercises 67 4.8.1 . 67 5 MIPS Assembly Code Examples 69 5.1 add2.asm . 70 5.2 hello.asm . 71 5.3 multiples.asm 72 5.4 palindrome.asm . 74 5.5 atoi-1.asm 76 5.6 atoi-4.asm 78 5.7 printf.asm 80 5.8 fib-o.asm . 84 5.9 treesort.asm . 86