Table Of Contentffirs.qxd 9/26/2005 2:42 PM Page iii
001010110001110010101000010101011010100100100100011011000001010001101010001
THE DEFINITIVE GUIDE TO
HOW
COMPUTERS
DO MATH
Featuring
The Virtual DIY Calculator
Clive “MAX” Maxfield
Alvin Brown
0
1
0
1
1
0
0
1 A JOHN WILEY & SONS, INC., PUBLICATION
0
001010110001 10010101000010101011010100100100100011011000001010001101010001
1
0
0
1
ffirs.qxd 9/26/2005 2:42 PM Page iv
Copyright © 2005 by John Wiley & Sons, Inc. All rights reserved.
Published by John Wiley & Sons, Inc., Hoboken, New Jersey.
Published simultaneously in Canada.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any
form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise,
except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without
either the prior written permission of the Publisher, or authorization through payment of the
appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 750-4470, or on the web at www.copyright.com. Requests to
the Publisher for permission should be addressed to the Permissions Department, John Wiley &
Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008.
Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best
efforts in preparing this book, they make no representations or warranties with respect to the
accuracy or completeness of the contents of this book and specifically disclaim any implied
warranties of merchantability or fitness for a particular purpose. No warranty may be created or
extended by sales representatives or written sales materials. The advice and strategies contained
herein may not be suitable for your situation. You should consult with a professional where
appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other
commercial damages, including but not limited to special, incidental, consequential, or other
damages.
For general information on our other products and services please contact our Customer Care
Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993 or fax 317-572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print,
however, may not be available in electronic format.
Library of Congress Cataloging-in-Publication Data is available.
ISBN-13 978-0471-73278-5
ISBN-10 0-471-73278-8
Printed in the United States of America.
10 9 8 7 6 5 4 3 2 1
ffirs.qxd 9/26/2005 2:42 PM Page v
To all our friends
who make the world
such a wonderful place!
ftoc.qxd 9/26/2005 2:44 PM Page vii
010101010001011101010000101010110101001001001000110110000010101101000110101
Contents
Laboratories ix
Do You Speak Martian? xi
Chapter 0 Why This Book Is So Cool 1
This is where we discover just why this book is so cool, and
also why this chapter is numbered “0.”
Chapter 1 Introducing Binary and Hexadecimal Numbers 7
In this chapter we introduce the concepts of the binary and
hexadecimal number systems (but in a much more interesting
manner than most computer books).
Chapter 2 Computers and Calculators 23
Here we rampage through the insides of a simple computer
and calculator, and we also meet our virtual DIY Calculator.
Chapter 3 Subroutines and Other Stuff 35
In this smorgasbord of a chapter, we first discuss logical, shift,
and rotate instructions; then we plunge headfirst into the
stack, subroutines, recursion, and the concept of self-
modifying code.
Chapter 4 Integer Arithmetic 69
This is where we learn the concepts of signed and unsigned
binary arithmetic, and multibyte data representations. In the
labs associated with this chapter we create some integer-based
math subroutines for use in Chapter 5.
Chapter 5 Creating an Integer Calculator 123
This is where things really start to get interesting because we
use the subroutines we developed in Chapter 4 to implement
a simple four-function integer calculator.
vviiii
ftoc.qxd 9/26/2005 2:44 PM Page viii
viii CONTENTS
Chapter 6 More Functions and Experiments 135
This may be the last chapter, but turn that frown upside down
into a smile because this is where we discover additional
projects you can undertake, and also how you can compete
with other readers to create better math routines for your
DIY Calculator.
Interactive Laboratories 155–412
This is where you will find the interactive laboratories
associated with Chapters 2 through 5 (see more details
on page ix).
Appendix A Installing Your DIY Calculator 413
In this appendix, we learn how to install the virtual DIY
Calculator presented on the CD-ROM accompanying
this book.
Appendix B Addressing Modes 417
Here we learn about the various addressing modes supported
by the virtual microprocessor powering the DIY Calculator.
Appendix C Instruction Set Summary 429
The tables in this appendix are useful for quickly determining
which instructions are available with which addressing modes.
Appendix D Additional Resources 433
There is a wealth of additional resources for individual
readers and educators on the CD-ROM accompanying the
book and from the DIY Calculator’s website. Also, there are
lots of other really interesting books to read. This appendix
explains it all.
About the Authors 441
Acknowledgments 443
Index 445
flast.qxd 9/26/2005 2:47 PM Page ix
010101010001011101010000101010110101001001001000110110000010101101000110101
Laboratories
LABS FOR CHAPTER 2: CREATING AND RUNNING PROGRAMS 155
Lab 2a: Creating a Simple Program 157
Lab 2b: Constant Labels and .EQU Directives 169
Lab 2c: Driving the Calculator’s Main Display 173
Lab 2d: Reading from the Calculator’s Keypad 185
Lab 2e: Writing to the Calculator’s Six LEDs 193
Lab 2f: Using the Memory Walker and Other 201
Diagnostic Displays
LABS FOR CHAPTER 3: SUBROUTINES AND OTHER STUFF 219
Lab 3a: Using Logical Instructions, Shifts, and Rotates 221
Lab 3b: Understanding the Program Counter (PC) 237
Lab 3c: Using the Index Register (X) 245
Lab 3d: Using the Stack and Stack Pointer (SP) 255
Lab 3e: Using Subroutines 261
Lab 3f: Using Recursion 267
LABS FOR CHAPTER 4: INTEGER ARITHMETIC 275
Lab 4a: Creating a Testbench Program 277
Lab 4b: Creating a 16-bit ADD Subroutine 299
Lab 4c: Creating a 16-bit SUBTRACT Subroutine 311
Lab 4d: Creating a 16-bit NEGATE Subroutine 317
Lab 4e: Checking for Overflow in the ADD and 323
SUBTRACT Routines
Lab 4f: Creating a 16-bit MULTIPLY Subroutine 333
Lab 4g: Creating a 16-bit DIVIDE Subroutine 351
LABS FOR CHAPTER 5: CREATING AN INTEGER CALCULATOR 373
Lab 5a: Creating the Calculator Framework 375
Lab 5b: Adding Some Low-Level Utility Routines 383
iixx
flast.qxd 9/26/2005 2:47 PM Page x
x LABORATORIES
Lab 5c: Creating a Decimal GETNUM (“get number”) 389
Subroutine
Lab 5d: Creating a Decimal DISPNUM (“display number”) 395
Subroutine
Lab 5e: Implementing a Four-Function Integer Calculator 405
flast.qxd 9/26/2005 2:47 PM Page xi
010101010001011101010000101010110101001001001000110110000010101101000110101
Do You Speak Martian?
There are an abundance of books on computer architectures, computer
logic, and computer mathematics, and most of these works discuss vari-
ous techniques for representing and manipulating numbers inside com-
puters. Sadly, however, it appears that the majority of these tomes are
written by visitors from the planet Mars, whose keen understanding of
higher mathematics is somewhat offset by their limited grasp of the
English language.
“Say it’s not so!” you cry, but the proof is irrefutable. When was the
last time you waded through a book on computer mathematics without
your brain overheating? Much like reading Being and Nothingness by the
famous French philosopher Jean Paul Sartre, one could mull over many
of these cryptic masterpieces until the end of time without gaining so
much as the faintest clue as to what was in their authors’ minds.
This is why we have been moved to write this modest attempt at in-
troducing the basics of computer arithmetic in words we can all under-
stand. An impossible task, some may say, but we dare to fly in the face of
conventional wisdom. Now, read on. ...
iixx
c00.qxd 7/20/2005 8:21 AM Page 1
The Definitive Guide to How Computers Do Math: Featuring the Virtual DIY Calculator.
Clive “MAX” Maxfield and Alvin Brown. Copyright 2005 John Wiley & Sons, Inc.
100001001010111111001010100001010101101010010010010001101100000101000110101
CHAPTER
0 WHY THIS BOOK
IS SO COOL
“
The simplest schoolboy is now familiar with
facts for which Archimedes would have
”
sacrificed his life.
ERNESTRENAN(1823–1892) in Souvenirs
D’enfance et de Jeunesse(1887)
100001001010111111001010100001010101101010010010010001101100000101000110101
10100010101010001110010101000010101011010100100
1010I1n0 t0hi0s 1ch1a0p1te1r w00e w00ill0 le1a0rn100011010110000101010100011
0
1001010100001010101101010010010010001101100000
0 ●● The sock color of choice for the discerning
101100011010V1ik1in0g0 w0a0rr1io0r101010001110010101000010101
01010101001001001000110110000010100011010110000
●● Why this book is so cool
1
1010101000111001010100001010101101010010010010
●● That there are jobs awaiting time-travelers
0
0011011000001010001101011000010101010001110010
0 ●● Some “stuff” about calculators
1011000010●●10W1h0y1 th1i0s c1h0ap1t0er0 i1s n0u0m1b0e0re1d0 “00”0110110000010100
0110101100001010101000111001010100001010101101
0100100100100011011000001010001101011000010101
0100011100101010000101010110101001001001000110
1100000101000110101100001010101000111001010100
0010101011010100100100100011011000001010001101
0110000101010100011100101010000101010110101001
0010010001101100000101000110101100001010101000
1110010101000010101011010100100100100011011000
0010100011010110000101010100011100101010000101
0101101010010010010001101100000101000110101000
0011011000001010001101011000010101010001110010
1010000101010110101001001001000110110000010100
01010101010101010010100
0110101100001010101000111001010100001010101101
0100100100100011011000001010001101011000010101
0100011100101010000101010110101001001001000110 1
1100000101000110101100001010101000111001010100
0110000101010100011100101010000101010110101001