Professional Documents
Culture Documents
Full Download PDF of Digital Design and Computer Architecture: RISC-V Edition Sarah Harris & David Harris 1st Edition - Ebook PDF All Chapter
Full Download PDF of Digital Design and Computer Architecture: RISC-V Edition Sarah Harris & David Harris 1st Edition - Ebook PDF All Chapter
Full Download PDF of Digital Design and Computer Architecture: RISC-V Edition Sarah Harris & David Harris 1st Edition - Ebook PDF All Chapter
https://ebooksecure.com/download/digital-design-and-computer-
architecture-arm-edition-ebook-pdf/
https://ebooksecure.com/download/cissp-practice-exams-fourth-
edition-ebook-pdf/
https://ebooksecure.com/download/engineering-ethics-concepts-and-
cases-ebook-pdf/
http://ebooksecure.com/product/quantitative-chemical-
analysis-9th-edition-by-daniel-c-harris-ebook-pdf/
(eBook PDF) Exploring Chemical Analysis 5th Edition by
Daniel C. Harris
http://ebooksecure.com/product/ebook-pdf-exploring-chemical-
analysis-5th-edition-by-daniel-c-harris/
https://ebooksecure.com/download/cissp-all-in-one-exam-guide-
ebook-pdf-2/
https://ebooksecure.com/download/cissp-all-in-one-exam-guide-
ebook-pdf-3/
https://ebooksecure.com/download/cissp-all-in-one-exam-guide-
ebook-pdf/
http://ebooksecure.com/product/ebook-pdf-computer-organization-
architecture-themes-and-variations/
RISC-V Instruction Set Summary
31:25 24:20 19:15 14:12 11:7 6:0 • imm: signed immediate in imm11:0
funct7 rs2 rs1 funct3 rd op R-Type • uimm: 5-bit unsigned immediate in imm4:0
imm11:0 rs1 funct3 rd op • upimm: 20 upper bits of a 32-bit immediate, in imm31:12
I-Type
• Address: memory address: rs1 + SignExt(imm11:0)
imm11:5 rs2 rs1 funct3 imm4:0 op S-Type
data at memory location Address
• [Address]:
imm12,10:5 rs2 rs1 funct3 imm4:1,11 op B-Type • BTA: branch target address: PC + SignExt({imm12:1, 1'b0})
imm31:12 rd op U-Type • JTA: jump target address: PC + SignExt({imm20:1, 1'b0})
imm20,10:1,11,19:12 rd op J-Type • label: text indicating instruction address
fs3 funct2 fs2 fs1 funct3 fd op R4-Type • SignExt: value sign-extended to 32 bits
5 bits 2 bits 5 bits 5 bits 3 bits 5 bits 7 bits • ZeroExt: value zero-extended to 32 bits
• csr: control and status register
Figure B.1 RISC-V 32-bit instruction formats
In RV64I, registers are 64 bits, but instructions are still 32 bits. The term “word” generally refers to a 32-bit value. In RV64I, immediate shift instructions use
6-bit immediates: uimm5:0; but for word shifts, the most significant bit of the shift amount (uimm5) must be 0. Instructions ending in “w” (for “word”) operate
on the lower half of the 64-bit registers. Sign- or zero-extension produces a 64-bit result.
Harris and Harris have shown how to design a RISC-V processor from
the gates all the way up through the microarchitecture. Their clear
explanations combined with their comprehensive approach give a full
picture of both digital design and the RISC-V architecture. With the
exciting opportunity that students have to run large digital designs on
modern FPGAs, this approach is both informative and enlightening.
David A. Patterson University of California, Berkeley
What broad fields Harris and Harris have distilled into one book! As
semiconductor manufacturing matures, the importance of digital design
and computer architecture will only increase. Readers will find an
approachable and comprehensive treatment of both topics and will walk
away with a clear understanding of the RISC-V instruction set architecture.
Andrew Waterman SiFive
There are excellent texts for teaching digital design and there are excellent
texts for teaching computer hardware organization - and this textbook does
both! It is also unique in its ability to connect the dots. The writing makes
the RISC-V architecture understandable by building on the basics, and I
have found the exercises to be a great resource across multiple courses.
Roy Kravitz Portland State University
When I first read Harris and Harris’s MIPS textbook back in 2008, I
thought that it was one of the best books I had ever read for teaching
computer architecture. I started using it in my courses immediately.
Thirteen years later, I have had the honor of reviewing this new RISC-V
edition, and my opinion of their books has not changed: Digital Design
and Computer Architecture: RISC-V Edition is an excellent book, very
clear, thorough, with a high educational value, and in line with the
courses that we teach in the areas of digital design and computer archi-
tecture. I look forward to using this RISC-V textbook in my courses.
Daniel Chaver Martinez University Complutense of Madrid
This page intentionally left blank
Digital Design and
Computer Architecture
RISC-V Edition
This page intentionally left blank
Digital Design and
Computer Architecture
RISC-V Edition
Sarah L. Harris
David Harris
No part of this publication may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or any information storage
and retrieval system, without permission in writing from the publisher. Details on how to
seek permission, further information about the Publisher’s permissions policies and our
arrangements with organizations such as the Copyright Clearance Center and the Copyright
Licensing Agency, can be found at our website: www.elsevier.com/permissions.
This book and the individual contributions contained in it are protected under copyright by
the Publisher (other than as may be noted herein).
Notices
Knowledge and best practice in this field are constantly changing. As new research and
experience broaden our understanding, changes in research methods, professional practices,
or medical treatment may become necessary.
Practitioners and researchers must always rely on their own experience and knowledge in
evaluating and using any information, methods, compounds, or experiments described
herein. In using such information or methods they should be mindful of their own safety
and the safety of others, including parties for whom they have a professional responsibility.
To the fullest extent of the law, neither the Publisher nor the authors, contributors, or
editors, assume any liability for any injury and/or damage to persons or property as a matter
of products liability, negligence or otherwise, or from any use or operation of any methods,
products, instructions, or ideas contained in the material herein.
ISBN: 978-0-12-820064-3
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx
Online Supplements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
How to Use the Software Tools in a Course.. . . . . . . . . . . . . . . . xxii
Labs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
RVfpga. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii
Bugs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
Acknowledgments.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiv
About the Authors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
ix
x Contents
2.9 Timing���������������������������������������������������� 86
2.9.1 Propagation and Contamination Delay��������������� 86
2.9.2 Glitches������������������������������������������� 90
2.10 Summary�������������������������������������������������� 93
Exercises�������������������������������������������������� 95
Interview Questions�������������������������������������� 104
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 549
Preface
xix
xx Preface
FEATURES
Side-by-Side Coverage of SystemVerilog and VHDL
Hardware description languages (HDLs) are at the center of modern
digital design practices. Unfortunately, designers are evenly split between
the two dominant languages, SystemVerilog and VHDL. This book
introduces HDLs in Chapter 4 as soon as combinational and sequential
logic design has been covered. HDLs are then used in Chapters 5 and
7 to design larger building blocks and entire processors. Nevertheless,
Chapter 4 can be skipped and the later chapters are still accessible for
courses that choose not to cover HDLs.
This book is unique in its side-by-side presentation of SystemVerilog
and VHDL, enabling the reader to learn the two languages. Chapter 4
describes principles that apply to both HDLs, and then provides language-
specific syntax and examples in adjacent columns. This side-by-side
treatment makes it easy for an instructor to choose either HDL and
for the reader to transition from one to the other, either in a class or in
professional practice.
Real-World Perspectives
In addition to the real-world perspective in discussing the RISC-V
architecture, Chapter 6 illustrates the architecture of Intel x86 pro-
cessors to offer another perspective. Chapter 9 (available as an online
supplement) also describes peripherals in the context of SparkFun’s
RED-V RedBoard, a popular development board that centers on SiFive’s
Freedom E310 RISC-V processor. These real-world perspective chapters
show how the concepts in the chapters relate to the chips found in many
PCs and consumer electronics.
ONLINE SUPPLEMENTS
Supplementary materials are available online at ddcabook.com or
the publisher’s website: https://www.elsevier.com/books-and-journals/
book-companion/9780128200643. These companion sites (accessible to all
readers) include the following:
▸ Links to video lectures
▸ Solutions to odd-numbered exercises
▸ Figures from the text in PDF and PPTX formats
▸ Links to professional-strength computer-aided design (CAD) tools
from Intel®
▸ Instructions on how to use PlatformIO (an extension of Visual
Studio Code) to compile, assemble, and simulate C and assembly
code for RISC-V processors
▸ Hardware description language (HDL) code for the RISC-V processor
▸ Intel’s Quartus helpful hints
▸ Lecture slides in PowerPoint (PPTX) format
▸ Sample course and laboratory materials
▸ List of errata
EdX MOOC
This book also has a companion Massive Open Online Course (MOOC)
through EdX. The course includes video lectures, interactive practice
xxii Preface
problems, and interactive problem sets and labs. The MOOC is divided
into two parts: Digital Design (ENGR 85A) and Computer Architecture
(ENGR85B) offered by HarveyMuddX (on EdX, search for “Digital
Design HarveyMuddX” and “Computer Architecture HarveyMuddX”).
EdX does not charge for access to the videos but does charge for the inter-
active exercises and certificate. EdX offers discounts for students with
financial need.
PlatformIO
PlatformIO, which is an extension of Visual Studio Code, serves as a soft-
ware development kit (SDK) for RISC-V. With the explosion of SDKs for
each new platform, PlatformIO has streamlined the process of program-
ming and using various processors by providing a unified interface for a
large number of platforms and devices. It is available as a free download
and can be used with SparkFun’s RED-V RedBoard, as described in the
labs provided on the companion website. PlatformIO provides access
to a commercial RISC-V compiler and allows students to write both C
and assembly programs, compile them, and then run and debug them on
SparkFun’s RED-V RedBoard (see Chapter 9 and the accompanying labs).
LABS
The companion site includes links to a series of labs that cover topics
from digital design through computer architecture. The labs teach stu-
dents how to use the Quartus tools to enter, simulate, synthesize, and
implement their designs. The labs also include topics on C and assem-
bly language programming using PlatformIO and SparkFun’s RED-V
RedBoard.
After synthesis, students can implement their designs using the
Altera DE2, DE2-115, DE0, or other FPGA board. The labs are written
to target the DE2 or DE-115 boards. These powerful and competitively
priced boards are available from de2-115.terasic.com. The board con-
tains an FPGA that can be programmed to implement student designs.
We provide labs that describe how to implement a selection of designs
on the DE2-115 board using the Quartus software.
To run the labs, students will need to download and install
Intel’s Quartus Web or Lite Edition and Visual Studio Code with the
PlatformIO extension. Instructors may also choose to install the tools
on lab machines. The labs include instructions on how to implement the
projects on the DE2/DE2-115 board. The implementation step may be
skipped, but we have found it of great value. We have tested the labs on
Windows, but the tools are also available for Linux.
RVfpga
RISC-V FPGA, also referred to as RVfpga, is a free two-course sequence
that can be completed after learning the material in this book. The first
course shows how to target a commercial RISC-V core to an FPGA, pro-
gram it using RISC-V assembly or C, add peripherals to it, and analyze
and modify the core and memory system, including adding instructions
to the core. This course uses the open-source SweRVolf system-on-chip
(SoC) (https://github.com/chipsalliance/Cores-SweRVolf), which is based
on Western Digital’s open-source commercial SweRV EH1 core (https://
www.westerndigital.com/company/innovations/risc-v). The course also
shows how to use Verilator, an open-source HDL simulator, and Western
Digital’s Whisper, an open-source RISC-V instruction set simulator (ISS).
RVfpga-SoC, the second course, shows how to build an SoC based on
SweRVolf using building blocks such as the SweRV EH1 core, inter-
connect, and memories. The course then guides the user in loading and
running the Zephyr operating system on the RISC-V SoC. All neces-
sary software and system source code (Verilog/SystemVerilog files) are
free, and the courses may be completed in simulation, so no hardware
is required. RVfpga materials are freely available with registration from
the Imagination Technologies University Programme: https://university.
imgtec.com/rvfpga/.
xxiv Preface
BUGS
As all experienced programmers know, any program of significant com-
plexity undoubtedly contains bugs. So, too, do books. We have taken
great care to find and squash the bugs in this book. However, some
errors undoubtedly do remain. We will maintain a list of errata on the
book’s webpage.
Please send your bug reports to ddcabugs@gmail.com. The first
person to report a substantive bug with a fix that we use in a future
printing will be rewarded with a $1 bounty!
ACKNOWLEDGMENTS
We appreciate the hard work of Steve Merken, Nate McFadden, Ruby
Gammell, Andrae Akeh, Manikandan Chandrasekaran, and the rest of
the team at Morgan Kaufmann who made this book happen. We love
the art of Duane Bibby, whose cartoons enliven the chapters.
We thank Matthew Watkins, who contributed the section on
Heterogeneous Multiprocessors in Chapter 7, and Josh Brake, who
contributed to Chapter 9 on Embedded I/O Systems. We greatly appre-
ciate the work of Mateo Markovic and Geordie Ryder, who reviewed
the book and contributed to the exercise solutions. Numerous other
reviewers also substantially improved the book. They include Daniel
Chaver Martinez, Roy Kravitz, Zvonimir Bandic, Giuseppe Di Luna,
Steffen Paul, Ravi Mittal, Jennifer Winikus, Hesham Omran, Angel Solis,
Reiner Dizon, and Olof Kindgren. We also appreciate the students in our
courses at Harvey Mudd College and UNLV who have given us help-
ful feedback on drafts of this textbook. And last, but not least, we both
thank our families for their love and support.
About the Authors
xxv
From Zero to One
1
1. 1 The Game Plan
1. 2 The Art of Managing
Complexity
1. 3 The Digital Abstraction
1. 4 Number Systems
1. 5 Logic Gates
1. 6 Beneath the Digital
1.1 THE GAME PLAN Abstraction
1. 7 CMOS Transistors*
Microprocessors have revolutionized our world during the past three
decades. A laptop computer today has far more capability than a 1. 8 Power Consumption*
room-sized mainframe of yesteryear. A luxury automobile contains 1. 9 Summary and a Look Ahead
about 100 microprocessors. Advances in microprocessors have made Exercises
cell phones and the Internet possible, have vastly improved medicine, Interview Questions
and have transformed how war is waged. Worldwide semiconductor
industry sales have grown from US $21 billion in 1985 to $400 billion
in 2020, and microprocessors are a major segment of these sales.
We believe that microprocessors are not only technically, economi- Application >”hello
cally, and socially important, but are also an intrinsically fascinating Software world!”
human invention. By the time you finish reading this book, you will Operating
know how to design and build your own microprocessor. The skills Systems
you learn along the way will prepare you to design many other digital
systems. Architecture
We assume that you have a basic familiarity with electricity, some
prior programming experience, and a genuine interest in understanding Micro-
what goes on under the hood of a computer. This book focuses on the architecture
design of digital systems, which operate on 1’s and 0’s. We begin with
Logic +
digital logic gates that accept 1’s and 0’s as inputs and produce 1’s and
0’s as outputs. We then explore how to combine logic gates into more
Digital
complicated modules, such as adders and memories. Then, we shift gears Circuits
to programming in assembly language, the native tongue of the micro-
processor. Finally, we put gates together to build a microprocessor that Analog +
Circuits −
runs these assembly language programs.
A great advantage of digital systems is that the building blocks are
quite simple: just 1’s and 0’s. They do not require grungy mathematics or Devices
a profound knowledge of physics. Instead, the designer’s challenge is to
combine these simple blocks into complicated systems. A microprocessor Physics
may be the first system that you build that is too complex to fit in your
head all at once. One of the major themes woven through this book is
how to manage complexity.
1 . 2 . 1 Abstraction
The critical technique for managing complexity is abstraction: hid-
ing details when they are not important. A system can be viewed from
many different levels of abstraction. For example, American politicians
abstract the world into cities, counties, states, and countries. A county
contains multiple cities and a state contains many counties. When a
politician is running for president, the politician is mostly interested in
Application >”hello how the state as a whole will vote rather than how each county votes, so
world!” Programs
Software the state is the most useful level of abstraction. On the other hand, the
Operating Device Census Bureau measures the population of every city, so the agency must
Systems Drivers consider the details of a lower level of abstraction.
Instructions Figure 1.1 illustrates levels of abstraction for an electronic computer
Architecture
Registers system, along with typical building blocks at each level. At the lowest
Micro- Datapaths
level of abstraction is the physics, the motion of electrons. The behav-
architecture Controllers ior of electrons is described by quantum mechanics and Maxwell’s
equations. Our system is constructed from electronic devices such as
+ Adders
Logic
Memories transistors (or vacuum tubes, once upon a time). These devices have
well-defined connection points called terminals and can be modeled by
Digital AND Gates
Circuits NOT Gates the relationship between voltage and current as measured at each termi-
nal. By abstracting to this device level, we can ignore the individual elec-
Analog + Amplifiers
Circuits − Filters trons. The next level of abstraction is analog circuits, in which devices
are assembled to create components such as amplifiers. Analog circuits
Transistors
Devices
Diodes
input and output a continuous range of voltages. Digital circuits, such as
logic gates, restrict the voltages to discrete ranges, which we will use to
Physics Electrons indicate 0 and 1. In logic design, we build more complex structures, such
as adders or memories, from digital circuits.
Figure 1.1 Levels of abstraction Microarchitecture links the logic and architecture levels of abstrac-
for an electronic computing tion. The architecture level of abstraction describes a computer from
system the programmer’s perspective. For example, the Intel x86 architecture
1.2 The Art of Managing Complexity 3
The Model T Ford became the most-produced car of its era, selling over
15 million units. Another example of discipline was Ford’s famous say-
ing: “Any customer can have a car painted any color that he wants so
long as it is black.”
In the context of this book, the digital discipline will be very import-
ant. Digital circuits use discrete voltages, whereas analog circuits use
continuous voltages. Therefore, digital circuits are a subset of analog cir-
Model T Ford Photo from cuits and in some sense must be capable of less than the broader class
https://commons.wikimedia. of analog circuits. However, digital circuits are much simpler to design.
org/wiki/File: TModel_
launch_Geelong.jpg. By limiting ourselves to digital circuits, we can easily combine compo-
nents into sophisticated systems that ultimately outperform those built
from analog components in many applications. For example, digital tele-
visions, compact disks (CDs), and cell phones are replacing their analog
predecessors.
leak yet can be easily removed when the feed line needs replacement.
It is of a standardized diameter and thread pitch, tightened to a stan-
dardized torque by a standardized wrench. A car maker can buy the nut
from many different suppliers, as long as the correct size is specified.
Modularity dictates that there should be no side effects: the coupling nut
should not deform the elbow and should preferably be located where it
can be tightened or removed without taking off other equipment in the
engine.
Regularity teaches that interchangeable parts are a good idea. With
regularity, a leaking carburetor can be replaced by an identical part. The
carburetors can be efficiently built on an assembly line instead of being
painstakingly handcrafted.
We will return to these principles of hierarchy, modularity, and regu-
larity throughout the book.
1 . 4 . 1 Decimal Numbers
In elementary school, you learned to count and do arithmetic in
decimal. Just as you (probably) have ten fingers, there are ten decimal
digits: 0, 1, 2, …, 9. Decimal digits are joined together to form longer
decimal numbers. Each column of a decimal number has ten times the
weight of the previous column. From right to left, the column weights
are 1, 10, 100, 1000, and so on. Decimal numbers are referred to as
base 10. The base is indicated by a subscript after the number to pre-
vent confusion when working in more than one base. For example,
Figure 1.4 shows how the decimal number 974210 is written as the
sum of each of its digits multiplied by the weight of the correspond-
ing column.
An N-digit decimal number represents one of 10N possibilities: 0, 1,
2, 3, …, 10N − 1. This is called the range of the number. For example,
a three-digit decimal number represents one of 1000 possibilities in the
range of 0 to 999.
1 . 4 . 2 Binary Numbers
Bits represent one of two values, 0 or 1, and are joined together to form
binary numbers. Each column of a binary number has twice the weight
of the previous column, so binary numbers are base 2. In binary, the col-
umn weights (again, from right to left) are 1, 2, 4, 8, 16, 32, 64, 128,
1000's column
100's column
10's column
1's column
974210 = 9 × 10 + 7 × 10 + 4 × 10 + 2 × 10
3 2 1 0
256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, and so on. If
you work with binary numbers often, you’ll save time if you remember
these powers of two up to 216.
An N-bit binary number represents one of 2N possibilities: 0, 1, 2,
3, …, 2N − 1. Table 1.1 shows 1-, 2-, 3-, and 4-bit binary numbers and
their decimal equivalents.
1 01 001 0001 1
10 010 0010 2
11 011 0011 3
100 0100 4
101 0101 5
110 0110 6
111 0111 7
1000 8
1001 9
1010 10
1011 11
1100 12
1101 13
1110 14
1111 15
1.4 Number Systems 9
16's column
8's column
4's column
2's column
1's column
Figure 1.5 Conversion of a binary
number to decimal
10110 2 = 1 × 24 + 0 × 23 + 1 × 22 + 1 × 21+ 0 × 2 0 = 2210
one no one one no
sixteen eight four two one
Working from the left, start with the largest power of 2 less than or equal to
the number (in this case, 64). 84 ≥ 64, so there is a 1 in the 64’s column, leaving
84 − 64 = 20. 20 < 32, so there is a 0 in the 32’s column. 20 ≥ 16, so there is a 1
in the 16’s column, leaving 20 − 16 = 4. 4 < 8, so there is a 0 in the 8’s column.
4 ≥ 4, so there is a 1 in the 4’s column, leaving 4 − 4 = 0. Thus, there must be 0 s
in the 2’s and 1’s column. Putting this all together, 8410 = 10101002.
Working from the right, repeatedly divide the number by 2. The remainder goes in
each column. 84/2 = 42, so 0 goes in the 1’s column. 42/2 = 21, so 0 goes in the
2’s column. 21/2 = 10 with the remainder of 1 going in the 4’s column. 10/2 = 5,
so 0 goes in the 8’s column. 5/2 = 2 with the remainder of 1 going in the 16’s
column. 2/2 = 1, so 0 goes in the 32’s column. Finally, 1/2 = 0 with the remain-
der of 1 going in the 64’s column. Again, 8410 = 10101002.
1 . 4 . 3 Hexadecimal Numbers
Hexadecimal, a term coined
Writing long binary numbers becomes tedious and prone to error. A group by IBM in 1963, derives from
of four bits represents one of 24 = 16 possibilities. Hence, it is sometimes the Greek hexi (six) and Latin
decem (ten). A more proper
more convenient to work in base 16, called hexadecimal. Hexadecimal term would use the Latin
numbers use the digits 0 to 9 along with the letters A to F, as shown in sexa (six), but sexadecimal
Table 1.2. Columns in base 16 have weights of 1, 16, 162 (or 256), 163 sounded too risqué.
(or 4096), and so on.
Solution Conversion between hexadecimal and binary is easy because each hexa-
decimal digit directly corresponds to four binary digits. 216 = 00102, E16 = 11102
and D16 = 11012, so 2ED16 = 0010111011012. Conversion to decimal requires
the arithmetic shown in Figure 1.6.
10 CHAPTER ONE From Zero to One
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
256's column
16's column
1's column
Solution Again, conversion is easy. Start reading from the right. The four least signifi
cant bits are 10102 = A16. The next bits are 1112 = 716. Hence, 11110102 = 7A16.
1.4 Number Systems 11
Working from the right, repeatedly divide the number by 16. The remainder
goes in each column. 333/16 = 20, with the remainder of 1310 = D16 going in the
1’s column. 20/16 = 1, with the remainder of 4 going in the 16’s column. 1/16 = 0,
with the remainder of 1 going in the 256’s column. Again, the result is 14D16.
101100 DEAFDAD8
Figure 1.7 Least and most most least most least
significant bits and bytes significant significant significant significant
bit bit byte byte
(a) (b)
Solution Split the exponent into a multiple of ten and the remainder.
224 = 220 × 24. 220 ≈ 1 million. 24 = 16. So, 224 ≈ 16 million. Technically, 224 =
16,777,216, but 16 million is close enough for marketing purposes.
On-chip memory and RAM 1024 bytes is called a kilobyte (KB) or kibibyte (KiB). 1024 bits is
is measured in powers of two, called a kilobit (Kb or Kbit) or kibibit (Kib or Kibit). Similarly, MB / MiB,
but hard drives are measured Mb/Mib, GB/GiB, and Gb/Gib are used for millions and billions of bytes and
in powers of ten. For example,
32 GB (GiB) of RAM is
bits. Memory capacity is usually measured in bytes. Communication speed
actually 34,359,738,368 bytes is usually measured in powers of ten bits/second. For example, the maximum
of memory, whereas 32 GB speed of a dial-up modem is usually 56 kbits/sec, which is 56,000 bits/sec.
on a hard drive is 32,000,000
bytes of memory.
1 . 4 . 5 Binary Addition
Binary addition is much like decimal addition but easier, as shown in
Figure 1.8. As in decimal addition, if the sum of two numbers is greater
than what fits in a single digit, we carry a 1 into the next column. Figure
1.8 compares addition of decimal and binary numbers. In the rightmost
column of Figure 1.8(a), 7 + 9 = 16, which cannot fit in a single digit
because it is greater than 9. So, we record the 1’s digit, 6, and carry the
10’s digit, 1, over to the next column. Likewise, in binary, if the sum of
two numbers is greater than 1, we carry the 2’s digit over to the next
column. For example, in the rightmost column of Figure 1.8(b), the sum
11 carries 11
Figure 1.8 Addition examples 4277 1011
showing carries: (a) decimal + 5499 + 0011
(b) binary 9776 1110
(a) (b)
1.4 Number Systems 13
1 + 1 = 210 = 102 cannot fit in a single binary digit. So, we record the 1’s
digit (0) and carry the 2’s digit (1) of the result to the next column. In
the second column, the sum is 1 + 1 + 1 = 310 = 112. Again, we record
the 1’s digit (1) and carry the 2’s digit (1) to the next column. For obvi-
ous reasons, the bit that is carried over to the neighboring column is
called the carry bit.
Sign/Magnitude Numbers
Sign/magnitude numbers are intuitively appealing because they match
our custom of writing negative numbers with a minus sign followed
by the magnitude. An N-bit sign/magnitude number uses the most
14 CHAPTER ONE From Zero to One
The $7 billion Ariane 5 rocket, significant bit as the sign and the remaining N − 1 bits as the magnitude
launched on June 4, 1996, (absolute value). A sign bit of 0 indicates positive and a sign bit of 1
veered off course 40 seconds indicates negative.
after launch, broke up, and
exploded. The failure was
caused when the computer Example 1.9 SIGN/MAGNITUDE NUMBERS
controlling the rocket
overflowed its 16-bit range Write 5 and −5 as 4-bit sign/magnitude numbers.
and crashed.
Solution Both numbers have a magnitude of 510 = 1012. Thus, 510 = 01012 and
The code had been extensively
tested on the Ariane 4 rocket. −510 = 11012.
However, the Ariane 5 had a
faster engine that produced larger
values for the control computer, Unfortunately, ordinary binary addition does not work for sign/
leading to the overflow. magnitude numbers. For example, using ordinary addition on −510 + 510
gives 11012 + 01012 = 100102, which is nonsense.
An N-bit sign/magnitude number spans the range [−2N−1 + 1, 2N−1 − 1].
Sign/magnitude numbers are slightly odd in that both +0 and −0 exist.
Both indicate zero. As you may expect, it can be troublesome to have
two different representations for the same number.
Solution Start by representing the magnitude: +210 = 00102. To get −210, reverse
The process of reversing the
the sign by inverting the bits and adding 1. Inverting 00102 produces 11012. sign has historically been
11012 + 1 = 11102. So, −210 is 11102. called “taking the two’s
complement.” However,
we have found that this
Example 1.11 VALUE OF NEGATIVE TWO’S COMPLEMENT NUMBERS terminology sometimes makes
people think that they always
Find the decimal value of the 4-bit two’s complement number 0x9 (i.e., 10012). have to “take the two’s
complement” when working
Solution 10012 has a leading 1, so it must be negative. To find its magnitude, with two’s complement
reverse the sign by inverting the bits and adding 1. Inverting 10012 = 01102. numbers, but that is not true!
01102 + 1 = 01112 = 710. Hence, 10012 = −710. This process of reversing the
sign is only sometimes used
when working with negative
Two’s complement numbers have the compelling advantage that two’s complement numbers.
addition works properly for both positive and negative numbers. Recall
that when adding N-bit numbers, the carry out of the Nth bit (i.e., the
N + 1th result bit) is discarded.
Compute (a) −210 + 110 and (b) −710 + 710 using two’s complement numbers.
Solution (a) −210 + 110 = 11102 + 00012 = 11112 = −110. (b) −710 + 710 = 10012 +
01112 = 100002. The fifth bit is discarded, leaving the correct 4-bit result 00002.
Example 1.14 A
DDING TWO’S COMPLEMENT NUMBERS
WITH OVERFLOW
Compute 410 + 510 using 4-bit two’s complement numbers. Does the result
overflow?
Solution 410 + 510 = 01002 + 01012 = 10012 = −710. The result overflows the
range of 4-bit positive two’s complement numbers, producing an incorrect neg-
ative result. If the computation had been done using five or more bits, the result
010012 = 910 would have been correct.
System Range
Unsigned [0, 2N – 1]
–8 –7 –6 –5 –4 –3 –2 –1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Unsigned 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 Two's Complement
0000
1111 1110 1101 1100 1011 1010 1001
1000
0001 0010 0011 0100 0101 0110 0111 Sign / Magnitude
the sign), and then adding. Unless stated otherwise, assume that all
signed binary numbers use two’s complement representation.
Figure 1.11 shows a number line indicating the values of 4-bit num-
bers in each system. Unsigned numbers span the range [0, 15] in reg-
ular binary order. Two’s complement numbers span the range [−8, 7].
The nonnegative numbers [0, 7] share the same encodings as unsigned
numbers. The negative numbers [−8, −1] are encoded such that a larger
unsigned binary value represents a number closer to 0. Notice that the
weird number, 1000, represents −8 and has no positive counterpart.
Sign/magnitude numbers span the range [−7, 7]. The most significant
bit is the sign bit. The positive numbers [1, 7] share the same encodings
as unsigned numbers. The negative numbers are symmetric but have the
sign bit set. 0 is represented by both 0000 and 1000. Thus, N-bit sign/
magnitude numbers represent only 2N − 1 integers because of the two
representations for 0. N-bit unsigned binary or two's complement num-
bers represent 2N integers.