Introduction to MIPS Assembly Language Programming (Introduction to MIPS Assembly Language Programming)
Minimum price
Suggested price

Introduction to MIPS Assembly Language Programming

About the Book

Given the effort of writing a book, the first question an author has to answer is “why bother?”  The answer to that question is what frames the book, and what I will describe here.

Why was this book written?  First because I do not believe that there is any book currently available which meets the needs I had for a text book.  The first is that the can be obtained for a minimal price, or can even be downloaded for free at a number of sites on the web.  I am very tired of asking students to pay over $100 per book for classes.  I personally have been blessed in so many ways in this world, and I have reached a point in my career where I can take the time to produce this text as a small pay back for all I have been given.  I hope that this example will help students who use the book to order their priorities as they go through life, to seek a good outside of personal monetary gain.

I realize that this text could very much use a good editing, unless I can find someone willing to donate the time to do so, I think the basic information in the book is well enough organized to make it useful, and well worth the cost of a free download.

The second reason I wrote this book is that I could not find an assembly programming book that met the need I had in teaching assembly programming.  I believe that learning assembly programming is important to every Computer Science student because the principals in assembly affect how high level languages and programs in those languages are implemented.  I have purposefully structured the topics in this text to illustrate how concepts such as memory organization (static, heap, and stack) affect variable allocation in high level languages.  The chapter on program control is intended to make the student aware of structured programming, which is the basis for control structures in all modern high level languages.  Even arrays make more sense in a high level language once one understands how they are implemented, and why.  This text is intended to be more than a book about assembly language programming, but to extend assembly language into the principals on which the higher level languages are built.

Finally writing a book is the best way to organize my own thoughts.  Much of the material in this text existed for years as a jumble in my own mind.  Producing slides and programs for class helped clarify the concepts, but it was when I had to provide a larger organization of the ideas that many of them finally gelled for me.  Forcing yourself to explain a concept, particularly in the brutal detail of writing it out, is the best way to organize and learn things.

There are other details about this book that need to be mentioned.  Because this book is electronic, it can be released in phases.  This text should be looked at in the same way as a beta software release.  I know there are mistakes, but I have the ability to correct them and rerelease the text.  So comments are welcome.

There is a separate set of appendices which should be available by mid-summer, 2015.  I will update this forward with the URL address of those appendices once they are posted.  If anyone is in real need of those appendices, I will send them in their current, incomplete, format.  I can be contacted at ckann(at)

I will also release an answer guide when it is completed, hopefully in the same mid-summer 2015 time frame.  To request an answer guide will require a request from a professor or lecturer at a school, and that the requestor be listed on the department web site for that school.  Requests for this document can be made to me at the same address as for the appendices.

I hope the readers find this text useful.  I hope it is at least worth the price...

About the Author

Charles Kann
Charles Kann

Dr. Kann has worked in the data processing industry and has taught Computer Science for over 20 years.  He is most interested in helping students learn about Computer Science, and to be ready for live.  He believes that if you do what you love, you will never work in your life.  He has retired and now teaches as an adjunct at John Hopkins University and Gettysburg College.

Table of Contents


Chapter 1        Introduction. 13

Chapter 1. 1        Binary Numbers. 13

Chapter 1.1. 1     Values for Binary Numbers. 13

Chapter 1.1. 2     Binary Whole Numbers. 14

Chapter 1. 2        Converting Binary, Decimal, and Hex Numbers. 16

Chapter 1.2. 1     Converting Binary to Decimal 16

Chapter 1.2. 2     Converting  Decimal to Binary using Binary Powers. 16

Chapter 1.2. 3     Converting  Decimal to Binary using Division. 17

Chapter 1.2. 4     Converting  between binary and hexadecimal 18

Chapter 1. 3        Character Representation. 19

Chapter 1. 4        Adding Binary Whole Numbers. 21

Chapter 1. 5        Integer Numbers (2's Complement) 21

Chapter 1.5. 1     What is an Integer 21

Chapter 1.5. 2     2's complement operation and 2's complement format 22

Chapter 1.5. 3     The 2's Complement Operation. 22

Chapter 1.5. 4     The 2's Complement (or Integer) Type. 23

Chapter 1. 6        Integer Arithmetic. 24

Chapter 1.6. 1     Integer Addition. 24

Chapter 1.6. 2     Overflow of Integer Addition. 25

Chapter 1.6. 3     Integer multiplication using bit shift operations. 26

Chapter 1.6. 4     Integer division using bit shift operations. 27

Chapter 1. 7        Boolean Logical and Bitwise Operators. 28

Chapter 1.7. 1     Boolean Operators. 28

Chapter 1.7. 2     Logical and Bitwise Boolean Operators. 29

Chapter 1. 8        Context 30

Chapter 1. 9        Summary. 30

Chapter 1. 10     Exercises. 30

Chapter 2        First Programs in MIPS assembly. 34

Chapter 2. 1        The MARS IDE.. 34

Chapter 2. 2        MIPS and memory. 35

Chapter 2.2. 1     Types of memory. 36

Chapter 2.2. 2     Overview of a MIPS CPU.. 36

Chapter 2.2. 3     Registers. 38

Chapter 2.2. 4     Types of memory. 40

Chapter 2. 3        First program in MIPS assembly. 41

Chapter 2.3. 1     Program 2-1 Commentary. 43

Chapter 2. 4        Program to prompt and read an integer from a user 45

Chapter 2.4. 1     Program 2-2 Commentary. 46

Chapter 2. 5        Program to prompt and read a string  from a user 46

Chapter 2.5. 1     Program 2-3 Commentary. 47

Chapter 2. 6        Summary. 51

Chapter 2. 7        Java program for call by value and reference. 51

Chapter 2. 8        Exercises. 52

Chapter 3        MIPS arithmetic and Logical Operators. 53

Chapter 3. 1        3-Address machines. 53

Chapter 3. 2        Addition in MIPS assembly. 55

Chapter 3.2. 1     Addition operators. 55

Chapter 3.2. 2     Addition Examples. 56

Chapter 3.2. 3     Introduction to pseudo code. 58

Chapter 3.2. 4     Assembly language addition program.. 59

Chapter 3.2. 5     Assembly language addition program commentary. 60

Chapter 3. 3        Subtraction in MIPS assembly. 61

Chapter 3. 4        Multiplication in MIPS assembly. 62

Chapter 3. 5        Division in MIPS Assembly. 64

Chapter 3.5. 1     Remainder operator, even/odd number checker 66

Chapter 3.5. 2     Remainder operator, even/odd number checker 66

Chapter 3. 6        Solving arithmetic expressions in MIPS assembly. 67

Chapter 3. 7        Division and accuracy of an equation. 68

Chapter 3. 8        Logical operators. 69

Chapter 3. 9        Using logical operators. 72

Chapter 3.9. 1     Storing immediate values in registers. 72

Chapter 3.9. 2     Converting a character from upper case to lower case. 72

Chapter 3.9. 3     Reversible operations with XOR.. 73

Chapter 3. 10     Shift Operations. 74

Chapter 3.10. 1     Program illustrating shift operations. 77

Chapter 3. 11     Summary. 78

Chapter 3. 12     Exercises. 78

Chapter 4        Translating Assembly Language into Machine Code. 82

Chapter 4. 1        Instruction formats. 82

Chapter 4. 2        Machine code for the add instruction. 84

Chapter 4. 3        Machine code for the sub instruction. 85

Chapter 4. 4        Machine code for the addi instruction. 85

Chapter 4. 5        Machine code for the sll instruction. 86

Chapter 4. 6        Exercises. 86

Chapter 5        Simple MIPS subprograms. 89

Chapter 5. 1        Exit Subprogram.. 89

Chapter 5.1. 1     Commentary on Exit subprogram.. 90

Chapter 5. 2        PrintNewLine subprogram.. 91

Chapter 5.2. 1     Commentary on Exit subprogram.. 92

Chapter 5. 3        The Program Counter ($pc)  register and calling a subprogram.. 93

Chapter 5. 4        Returning from a subprogram and the $ra register 96

Chapter 5. 5        Input parameter with PrintString subprogram.. 97

Chapter 5. 6        Multiple input parameters with PrintInt subprogram.. 99

Chapter 5. 7        Return values with PromptInt subprogram.. 101

Chapter 5. 8        Create a utils.asm file. 103

Chapter 5. 9        Final program to prompt, read, and print an integer 105

Chapter 5. 10     Summary. 106

Chapter 5. 11     Exercises. 106

Chapter 6        MIPS memory - the data segment 109

Chapter 6. 1        Flat memory model 109

Chapter 6. 2        Static data. 111

Chapter 6. 3        Accessing memory. 113

Chapter 6. 4        Methods of accessing memory. 114

Chapter 6.4. 1     Addressing by label 115

Chapter 6.4. 2     Register direct access. 116

Chapter 6.4. 3     Register indirect access. 117

Chapter 6.4. 4     Register offset access. 118

Chapter 6. 5        Exercises. 120

Chapter 7        Assembly language program control structures. 122

Chapter 7. 1        Use of goto statements. 123

Chapter 7. 2        Simple if statements. 124

Chapter 7.2. 1     Simple if statements in pseudo code. 124

Chapter 7.2. 2     Simple if statement translated to assembly. 124

Chapter 7.2. 3     Simple if statement with complex logical conditions. 125

Chapter 7. 3        if-else  statements. 126

Chapter 7. 4        if-elseif-else statements. 128

Chapter 7. 5        Loops. 130

Chapter 7.5. 1     Sentinel control loop. 131

Chapter 7.5. 2     Counter control loop. 132

Chapter 7. 6        Nested code blocks. 134

Chapter 7. 7        A full assembly language program.. 136

Chapter 7. 8        How to calculate branch amounts in machine code. 140

Chapter 7.8. 1     Instruction Addresses. 140

Chapter 7.8. 2     Value in the $pc register 141

Chapter 7.8. 3     How the word boundary effects branching. 142

Chapter 7.8. 4     Translating branch instructions to machine code. 143

Chapter 7.8. 5     PC relative addressing. 145

Chapter 7. 9        Exercises. 146

Chapter 8        Reentrant Subprograms. 149

Chapter 8. 1        Stacks. 149

Chapter 8.1. 1     Stack data structure: definition. 149

Chapter 8.1. 2     Another stack implementation. 151

Chapter 8. 2        The program stack. 152

Chapter 8.2. 1     The non-reentrant subprogram problem.. 152

Chapter 8.2. 2     Making subprograms re-entrant 155

Chapter 8. 3        Recursion. 157

Chapter 8.3. 1     Recursive multiply in a HLL.. 158

Chapter 8. 4        Exercises. 160

Chapter 9        Arrays. 161

Chapter 9. 1        Heap dynamic memory. 161

Chapter 9.1. 1     What is heap memory. 161

Chapter 9.1. 2     Allocating heap memory example – PromptString subprogram.. 162

Chapter 9.1. 3     Commentary on PromptString Subprogram.. 163

Chapter 9. 2        Array Definition and creation in Assembly. 164

Chapter 9.2. 1     Allocating arrays in memory. 166

Chapter 9. 3        Printing an Array. 167

Chapter 9. 4        Bubble Sort 169

Chapter 9.3. 1     Bubble Sort in MIPS assembly. 170

Chapter 9. 5        Summary. 173

Chapter 9. 6        Exercises. 174


The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

80% Royalties. Earn $16 on a $20 book.

We pay 80% royalties. That's not a typo: you earn $16 on a $20 sale. If we sell 5000 non-refunded copies of your book or course for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $12 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub