You are on page 1of 20

www.GetPedia.

com
More than 500,000 articles about almost
EVERYTHING !!
Click on your interest section for more information :
q

Acne
q
q

Fitness Equipment
q

P
olitical Advertising
q
q
q

Forums
q

P
ositive Attitude Tips Aerobics & Cardio
q
q

Game
q

P
ay-Per-Click Advertising Affiliate Revenue
q
q

Goal Setting
q

P
ublic Relations Alternative Medicine
q

Golf
q

P
regnancy Attraction
q
q

Dealing with Grief & Loss


q

P
resentation Online Auction
q
q

Hair Loss
q

P
sychology Streaming Audio & Online Music
q
q

Finding Happiness
q

P
ublic Speaking Aviation & Flying
q
q

Computer Hardware
q

R
eal Estate Babies & Toddler
q
q

Holiday
q

R
ecipes & Food and Drink Beauty
q
q

Home Improvement
q

R
elationship Blogging, RSS & Feeds
q
q

Home Security
q

R
eligion Book Marketing
q
q

Humanities
q

S
ales Book Reviews
q
q

Humor & Entertainment


q

S
ales Management Branding
q
q

Innovation
q

S
ales Telemarketing Breast Cancer
q
q

Inspirational
q

S
ales Training Broadband Internet
q
q

Insurance
q

S
atellite TV Muscle Building & Bodybuilding
q
q

Interior Design & Decorating


q

S
cience Articles Careers, Jobs & Employment
q
q

Internet Marketing
q

I
nternet Security Casino & Gambling
q
q

Investing
q

S
earch Engine Optimization Coaching
Landscaping & Gardening
(SEO)
q

Coffee
q
q

Language
q

S
exuality College & University
q
q

Leadership
q

W
eb Site Promotion Cooking Tips
q
q

Leases & Leasing


q

S
mall Business Copywriting
q
q

Loan
q

S
oftware Crafts & Hobbies
q
q

Mesothelioma & Asbestos Cancer


q

S
pam Blocking Creativity
q
q

S
pirituality Credit
q
q

Business Management
q

S
tocks & Mutual Fund Cruising & Sailing
q
q

Marketing
q

S
trategic Planning Currency Trading
q
q

Marriage & Wedding


q

S
tress Management Customer Service
q
q

Martial Arts
q

S
tructured Settlements Data Recovery & Computer
q
Backup
q

Medicine
q

Success Meditation
q
q

N
utritional Supplements Dating
q
q

Mobile & Cell Phone


q

T
ax Debt Consolidation
q
q

Mortgage Refinance
q

T
eam Building Debt Relief
q
q

Motivation
q

T
ime Management Depression
q
q

Motorcycle
q

T
op Quick Tips Diabetes
q
q

Music & MP3


q

T
raffic Building Divorce
q
q

Negotiation
q

V
acation Rental Domain Name

q
q

Network Marketing
q

V
ideo Conferencing E-Book
q
q

Networking
q

V
ideo Streaming E-commerce
q
q

Nutrition
q

V
OIP Elder Care
q
q

Get Organized - Organization


q

W
ealth Building Email Marketing
q
q

Outdoors
q

W
eb Design Entrepreneur
q
q

Parenting
q

W
eb Development Ethics
q
q

Personal Finance
q

W
eb Hosting Exercise & Fitness
q
q

Personal Technology
q

W
eight Loss Ezine Marketing
q
q

Pet
q

W
ine & Spirits Ezine Publishing
q
q

Philosophy
q

W
riting Fashion & Style
q
q

Photography
q

A
rticle Writing Fishing
Poetry
q

Yoga

Numerical Methods
. . . . Real-Time . . . . . . . . and . . . Embedded . . . . . . . . Systems . . . . . . Programming

....
Featuring in-depth coverage of:

Fixed and floating point mathematical techniques without a coprocessor

Numerical I/O for embedded systems

Data conversion methods

Don Morgan

Numerical Methods
Real-Time and Embedded Systems Programming

Numerical Methods
Real-Time and Embedded Systems Programming
Featuring in-depth coverage of:
Fixed and floating point mathematical techniques without a coprocessor
Numerical I/O for embedded systems
Data conversion methods

Don Morgan

M&T Books A Division of M&T Publishing, Inc. 411 BOREL AVE. SAN MATEO, CA 94402
1992 by M&T Publishing, Inc.
Printed in the United States of America
All rights reserved. No part of this book or disk may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopying, recording, or by any information storage and retrieval system, without prior written
permission from the Publisher. Contact the Publisher for information on foreign rights.
Limits of Liability and Disclaimer of Warranty The Author and Publisher of this book have used their best efforts in preparing
the book and the programs contained in it and on the diskette. These efforts include the development, research, and testing of the
theories and programs to determine their effectiveness.
The Author and Publisher make no warranty of any kind, expressed or implied, with regard to these programs or the
documentation contained in this book. The Author and Publisher shall not be liable in any event for incidental or consequential
damages in connection with, or arising out of, the furnishing, performance, or use of these programs.
Library of Congress Cataloging-in-Publication Data
Morgan, Don 1948Numerical Methods/Real-Time and Embedded Systems Programming by Don Morgan p. cm. Includes Index ISBN l-5585l-232-2
Book and Disk set 1. Electronic digital computersProgramming. 2. Real-time data processing. 3. Embedded computer systems
Programming. I. Title QA76.6.M669 1992 513.2 ' 0285dc20 91-47911
CIP
Project Editor: Sherri Morningstar Cover Design: Lauren Smith Design
95 94 93 92 4 3 2 1
Trademarks: The 80386, 80486 are registered trademarks and the 8051, 8048, 8086, 8088, 8OC196 and 80286 are products of
Intel Corporation, Santa Clara, CA. The Z80 is a registered trademark of Zilog Inc., Campbell, CA. The TMS34010 is a product
of Texas Instruments, Dallas, TX. Microsoft C is a product of Microsoft Corp. Redmond, WA.

Acknowledgment
Thank you Anita, Donald and Rachel
for your love and forbearance.

Contents
WHY THIS BOOK IS FOR YOU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.......3
CHAPTER 1: NUMBERS ............................................... 7 Systems of Representation . . . . . . . . . . . . . . . .
...........................................8
Bases .............................................................................. 9 The Radix Point, Fixed and
Floating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 2 Types of Arithmetic . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 5 Fixed
Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 5 Floating
Point . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 7 Positive and
Negative Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Fundamental Arithmetic
Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 1
Microprocessors ....................................................................... 21 Buswidth . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 Data type . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.............................................24
Flags........................................................................ 24 Rounding and the Sticky
Bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

NUMERICAL METHODS
Instructions
.............................................................................................
2
6
Addition
............................................................................................
26
Subtraction
........................................................................................
27
Multiplication
..................................................................................
27
Division
............................................................................................
28
Negation
and
Signs
.............................................................................
28
Shifts,
Rotates
and
Normalization
.......................................................
29
Decimal
and
ASCII
Instructions ......................................................... 30
CHAPTER 2: INTEGERS.......................................................... 33 Addition and
Subtraction ............................................................................. 33 Unsigned Addition and
Subtraction .................................................... 33 Multiprecision
Arithmetic .................................................................. 35 add64:
Algorithm ............................................................................... 36 add64:
Listing ..................................................................................... 36 sub64:
Algorithm ................................................................................ 37 sub64:
Listing ..................................................................................... 37 Signed Addition and Subtraction
........................................................ 38 Decimal Addition and
Subtraction ...................................................... 40 Multiplication and
Division ......................................................................... 42 Signed vs.
Unsigned ....................................................................... 43 signed-operation:
Algorithm ............................................................... 44 signed-operation:
Listing ................................................................... 45 Binary
Multiplication .................................................................................. 46 cmul:
Algorithm ................................................................................ 49 cmul:
Listing .................................................................................... 49

CONTENTS
A Faster Shift and Add ................................................................................. 50 cmul2:
Algorithm .............................................................................. 51 cmul2:
Listing .................................................................................... 52 Skipping Ones and
Zeros ............................................................................. 53 booth:
Algorithm ................................................................................ 55 booth:
Listing ..................................................................................... 55 bit-pair:
Algorithm ............................................................................. 57 bit-pair:
Listing ................................................................................. 58 Hardware Multiplication: Single
and Multiprecision ..................................... 61 mu132:
Algorithm ............................................................................... 62 mu132:
Listing .................................................................................... 63 Binary
Division ........................................................................................... 64 Error
Checking .................................................................................. 64 Software
Division ........................................................................................ 65 cdiv:
Algorithm .................................................................................. 67 cdiv:
Listing...................................................................................... 68 Hardware
Division ...................................................................................... 69 div32:
Algorithm ................................................................................ 74 div32:
Listing .................................................................................... 75 div64:
Algorithm ............................................................................... 79 div64:
Listing ..................................................................................... 80
CHAPTER
3;
REAL
NUMBERS
..................................................
85
Fixed
Point
.................................................................................................
86
Significant
Bits
............................................................................................
87
The
Radix
Point
.........................................................................................
89
Rounding .................................................................................................... 89 Basic Fixed-Point
Operations ...................................................................... 92

NUMERICAL METHODS
A Routine for Drawing Circles................................................................... 95 circle:
Algorithm .......................................................................... 98 circle:
Listing ................................................................................ 98 Bresenhams Line-Drawing
Algorithm ............................................ 100 line:
Algorithm ............................................................................. 101 line:
Listing....................................................................................... 102 Division by
Inversion ............................................................................. 105 divnewt:
Algorithm.............................................................................. 108 divnewt:
Listing................................................................................ 109 Division by
Multiplication.............................................................................. 114 divmul:
Algorithm.............................................................................. 116 divmul:
Listing................................................................................... 117
CHAPTER 4: FLOATING-POINT ARITHMETIC........................123 What To
Expect....................................................................................... 124 A Small Floating-Point
Package................................................................ 127 The Elements of a Floating-Point
Number.............................................. 128 Extended
Precision............................................................................ 131 The External
Routines................................................................................. 132 fp_add:
Algorithm............................................................................. 132 fp_add:
Listing................................................................................. 133 The Core
Routines........................................................................................ 134 Fitting These Routines to an
Application...................................................... 136 Addition and Subtraction:
FLADD.............................................................. 136 FLADD: The Prologue.
Algorithm...................................................... 138 FLADD: The Prologue.
Listing.......................................................... 138 The FLADD Routine Which Operand is
Largest? Algorithm.............. 140 The FLADD Routine: Which Operand is Largest?
Listing.................... 141

You might also like