The Secret Life of Programs
{"all_nodes":"PHASE 1 fill complete; deterministic lint complete — 1 review flag on this node (2026-10-12)"}local graph
The Secret Life of Programs — Jonathan E. Steinhart
Section titled “The Secret Life of Programs — Jonathan E. Steinhart”chapter_01
Section titled “chapter_01”node-book_the-secret-life-of-programs_chapter-01-acknowledgments — Acknowledgments
chapter_02
Section titled “chapter_02”node-book_the-secret-life-of-programs_chapter-02-preface — Preface
chapter_03
Section titled “chapter_03”node-book_the-secret-life-of-programs_chapter-03-introduction — Introduction
chapter_04
Section titled “chapter_04”node-book_the-secret-life-of-programs_chapter-04-the-internal-language-of-computers — The Internal Language of Computers
chapter_05
Section titled “chapter_05”node-book_the-secret-life-of-programs_chapter-05-combinatorial-logic — Combinatorial Logic
chapter_06
Section titled “chapter_06”node-book_the-secret-life-of-programs_chapter-06-sequential-logic — Sequential Logic
chapter_07
Section titled “chapter_07”node-book_the-secret-life-of-programs_chapter-07-computer-anatomy — Computer Anatomy
chapter_08
Section titled “chapter_08”node-book_the-secret-life-of-programs_chapter-08-computer-architecture — Computer Architecture
chapter_09
Section titled “chapter_09”node-book_the-secret-life-of-programs_chapter-09-communications-breakdown — Communications Breakdown
chapter_10
Section titled “chapter_10”node-book_the-secret-life-of-programs_chapter-10-organizing-data — Organizing Data
chapter_11
Section titled “chapter_11”node-book_the-secret-life-of-programs_chapter-11-language-processing — Language Processing
chapter_12
Section titled “chapter_12”node-book_the-secret-life-of-programs_chapter-12-the-web-browser — The Web Browser
chapter_13
Section titled “chapter_13”node-book_the-secret-life-of-programs_chapter-13-application-and-system-programming — Application and System Programming
chapter_14
Section titled “chapter_14”node-book_the-secret-life-of-programs_chapter-14-shortcuts-and-approximations — Shortcuts and Approximations
chapter_15
Section titled “chapter_15”node-book_the-secret-life-of-programs_chapter-15-deadlocks-and-race-conditions — Deadlocks and Race Conditions
chapter_16
Section titled “chapter_16”node-book_the-secret-life-of-programs_chapter-16-security — Security
chapter_17
Section titled “chapter_17”node-book_the-secret-life-of-programs_chapter-17-machine-intelligence — Machine Intelligence
chapter_18
Section titled “chapter_18”node-book_the-secret-life-of-programs_chapter-18-real-world-considerations — Real-World Considerations
chapter_19
Section titled “chapter_19”node-book_the-secret-life-of-programs_chapter-19-index — Index
Title Page
Section titled “Title Page”THE SECRET LIFE OF PROGRAMS
Section titled “THE SECRET LIFE OF PROGRAMS”UNDERSTAND COMPUTERS—CRAFT BETTER CODE
Section titled “UNDERSTAND COMPUTERS—CRAFT BETTER CODE”JONATHAN E. STEINHART
Section titled “JONATHAN E. STEINHART”FORTRAN
ASSEMBLY LANGUAGE
BINARY

[!visual-conversion] Cartoon miner holding blocks labeled FORTRAN, ASSEMBLY LANGUAGE, BINARY, with Cropped from the scan (PDF page 001): Cartoon miner holding blocks labeled FORTRAN, ASSEMBLY LANGUAGE, BINARY, with vintage computer parts and No Starch Press logo.
no starch press
THE SECRET LIFE OF PROGRAMS
THE SECRET LIFE OF PROGRAMS
Section titled “THE SECRET LIFE OF PROGRAMS”Understand Computers — Craft Better Code
Section titled “Understand Computers — Craft Better Code”by Jonathan E. Steinhart

[!visual-conversion] logo “no starch press” Cropped from the scan (PDF page 005): logo “no starch press”
no starch press
San Francisco
THE SECRET LIFE OF PROGRAMS. Copyright © 2019 by Jonathan E. Steinhart.
Section titled “THE SECRET LIFE OF PROGRAMS. Copyright © 2019 by Jonathan E. Steinhart.”All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN-10: 1-59327-970-1
ISBN-13: 978-1-59327-970-7
Publisher: William Pollock
Production Editor: Janelle Ludowise
Cover Illustration: Josh Ellingson
Interior Design: Octopod Studios
Developmental Editors: Corbin Collins and Annie Choi
Technical Reviewer: Aubrey Anderson
Copyeditor: Rachel Monaghan
Compositor: Happenstance Type-O-Rama
Proofreader: Paula L. Fleming
Indexer: JoAnne Burek
The following images are attributed as follows: Composition in Figure 6-36 courtesy of Hanalei Steinhart. Brick wall in Figures 11-3, 11-4, 11-7 and 11-8 from www.cadhatch.com. Figure 14-25 from The Rocky Horror Picture Show (1975).
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1.415.863.9900; [email protected]
www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Section titled “Library of Congress Cataloging-in-Publication Data”Names: Steinhart, Jonathan E., author.
Title: The Secret Life of Programs / Jonathan E. Steinhart.
Description: San Francisco : No Starch Press, Inc., [2019]
Identifiers: LCCN 2019018295 (print) | LCCN 2019021631 (ebook) | ISBN 9781593279714 (epub) | ISBN 159327971X (epub) | ISBN 9781593279707 (print) | ISBN 1593279701 (print)
Subjects: LCSH: Computer programming. | Programming languages (Electronic computers) | Browsers (Computer programs)
Classification: LCC QA76.6 (ebook) | LCC QA76.6 .S735 2019 (print) | DDC 005.1—dc23
LC record available at https://lccn.loc.gov/2019018295
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
To Julie and Hanalei for making
me learn how to explain complex
technologies to laypeople.
To the amazing place that was Bell
Telephone Laboratories and all the
people who worked there, especially to
Carl for making a place for me there
when I was a teenager.
About the Author
Section titled “About the Author”Jonathan E. Steinhart has been involved in engineering since the 1960s. He started designing hardware in middle school and software in high school, which led to summer jobs at Bell Telephone Laboratories. He received his BSEE in Electrical Engineering and Computer Science from Clarkson University in 1977. After graduation, he worked for Tektronix before trying his hand at startup companies. He became a consultant in 1987, focused on safety-critical systems engineering. He cut back a bit starting in the 1990s to start Four Winds Vineyard.
About the Technical Reviewer
Section titled “About the Technical Reviewer”Aubrey Anderson has a BSEE in Electrical Engineering and Computer Science from Tufts University. While there, he was a Teaching Fellow and helped to improve curricula for introductory computer science courses. He started programming at 14 and since then has worked on a variety of robotics, system design, and web programming projects. Aubrey currently works as a Software Engineer at Google.
Table of Contents
Section titled “Table of Contents”BRIEF CONTENTS
Section titled “BRIEF CONTENTS”Acknowledgments … xix
Preface … xxi
Introduction … xxv
Chapter 1: The Internal Language of Computers … 1
Chapter 2: Combinatorial Logic … 33
Chapter 3: Sequential Logic … 69
Chapter 4: Computer Anatomy … 93
Chapter 5: Computer Architecture … 117
Chapter 6: Communications Breakdown … 141
Chapter 7: Organizing Data … 183
Chapter 8: Language Processing … 217
Chapter 9: The Web Browser … 237
Chapter 10: Application and System Programming … 259
Chapter 11: Shortcuts and Approximations … 283
Chapter 12: Deadlocks and Race Conditions … 335
Chapter 13: Security … 351
Chapter 14: Machine Intelligence … 385
Chapter 15: Real-World Considerations … 413
Index … 445
CONTENTS IN DETAIL
Section titled “CONTENTS IN DETAIL”ACKNOWLEDGMENTS
Section titled “ACKNOWLEDGMENTS”xix
PREFACE
Section titled “PREFACE”xxi
INTRODUCTION
Section titled “INTRODUCTION”xxv
- Why Good Programming is Important … xxvi
- Learning to Code is Only a Starting Place … xxvii
- Importance of Low-Level Knowledge … xxviii
- Who Should Read This Book? … xxviii
- What Are Computers? … xxix
- What Is Computer Programming? … xxix
- Coding, Programming, Engineering, and Computer Science … xxxii
- The Landscape … xxxiii
- What’s in This Book … xxxv
1 THE INTERNAL LANGUAGE OF COMPUTERS
Section titled “1 THE INTERNAL LANGUAGE OF COMPUTERS”1
- What Is Language? … 2
- Written Language … 2
- The Bit … 3
- Logic Operations … 3
- Boolean Algebra … 4
- De Morgan’s Law … 5
- Representing Integers Using Bits … 6
- Representing Positive Numbers … 6
- Binary Addition … 8
- Representing Negative Numbers … 10
- Representing Real Numbers … 14
- Fixed-Point Representation … 14
- Floating-Point Representation … 15
- The IEEE Floating-Point Standard … 17
- Binary-Coded Decimal System … 18
- Easier Ways to Work with Binary Numbers … 18
- Octal Representation … 18
- Hexadecimal Representation … 19
- Representing the Context … 20
- Naming Groups of Bits … 20
- Representing Text … 22
- The American Standard Code for Information Interchange … 22
- The Evolution of Other Standards … 24
- Unicode Transformation Format 8-bit … 24
- Using Characters to Represent Numbers … 25
- Quoted-Printable Encoding … 26
- Base64 Encoding … 26
- URL Encoding … 27
Representing Colors 27
Adding Transparency 29
Encoding Colors 30
Summary 31
2 COMBINATORIAL LOGIC 33
Section titled “2 COMBINATORIAL LOGIC 33”The Case for Digital Computers 34
The Difference Between Analog and Digital 35
Why Size Matters in Hardware 36
Digital Makes for More Stable Devices 37
Digital in an Analog World 38
Why Bits Are Used Instead of Digits 40
A Short Primer on Electricity 41
Using Plumbing to Understand Electricity 41
Electrical Switches 44
Building Hardware for Bits 47
Relays 47
Vacuum Tubes 50
Transistors 51
Integrated Circuits 52
Logic Gates 53
Improving Noise Immunity with Hysteresis 54
Differential Signaling 55
Propagation Delay 57
Output Variations 58
Building More Complicated Circuits 60
Building an Adder 60
Building Decoders 63
Building Demultiplexers 64
Building Selectors 65
Summary 67
3 SEQUENTIAL LOGIC 69
Section titled “3 SEQUENTIAL LOGIC 69”Representing Time 70
Oscillators 70
Clocks 71
Latches 71
Gated Latches 73
Flip-Flops 74
Counters 77
Registers 78
Memory Organization and Addressing 79
Random-Access Memory 82
Read-Only Memory 83
Block Devices 85
Flash Memory and Solid State Disks 88
Error Detection and Correction 88
Hardware vs. Software 90
Summary 91
xii Contents in Detail
4 COMPUTER ANATOMY 93
- Memory 94
- Input and Output 96
- The Central Processing Unit 97
- Arithmetic and Logic Unit 97
- Shiftiness 99
- Execution Unit 100
- Instruction Set 102
- Instructions 102
- Addressing Modes 104
- Condition Code Instructions 105
- Branching 105
- Final Instruction Set 106
- The Final Design 109
- The Instruction Register 109
- Data Paths and Control Signals 109
- Traffic Control 110
- RISC vs. CISC Instruction Sets 113
- Graphics Processing Units 114
- Summary 115
5 COMPUTER ARCHITECTURE 117
- Basic Architectural Elements 118
- Processor Cores 118
- Microprocessors and Microcomputers 119
- Procedures, Subroutines, and Functions 120
- Stacks 122
- Interrupts 125
- Relative Addressing 128
- Memory Management Units 130
- Virtual Memory 132
- System and User Space 133
- Memory Hierarchy and Performance 133
- Coprocessors 135
- Arranging Data in Memory 136
- Running Programs 137
- Memory Power 138
- Summary 139
6 COMMUNICATIONS BREAKDOWN 141
- Low-Level I/O 142
- I/O Ports 142
- Push My Buttons 144
- Let There Be Lights 146
- Lights, Action, 147
- Bright Ideas 148
- Shades of Gray 149
Contents in Detail xiii
Quadrature 150
Parallel Communication 152
Serial Communication 152
Catch a Wave 154
Universal Serial Bus 156
Networking 156
Modern LANs 158
The Internet 158
Analog in the Digital World 160
Digital-to-Analog Conversion 160
Analog-to-Digital Conversion 162
Digital Audio 165
Digital Images 173
Video 174
Human Interface Devices 176
Terminals 176
Graphics Terminals 177
Vector Graphics 178
Raster Graphics 180
Keyboard and Mouse 181
Summary 181
7 ORGANIZING DATA 183
Section titled “7 ORGANIZING DATA 183”Primitive Data Types 184
Arrays 185
Bitmaps 187
Strings 188
Compound Data Types 189
Singly Linked Lists 191
Dynamic Memory Allocation 195
More Efficient Memory Allocation 196
Garbage Collection 197
Doubly Linked Lists 198
Hierarchical Data Structures 199
Storage for the Masses 203
Databases 204
Indices 206
Moving Data Around 206
Vectored I/O 210
Object-Oriented Pitfalls 211
Sorting 212
Making a Hash of Things 213
Efficiency vs. Performance 215
Summary 216
8 LANGUAGE PROCESSING 217
Section titled “8 LANGUAGE PROCESSING 217”Assembly Language 217
High-Level Languages 219
xiv Contents in Detail
Structured Programming 220
Lexical Analysis 221
State Machines 223
Regular Expressions 224
From Words to Sentences 226
The Language-of-the-Day Club 228
Parse Trees 228
Interpreters 231
Compilers 232
Optimization 234
Be Careful with Hardware 236
Summary 236
9 THE WEB BROWSER 237
Section titled “9 THE WEB BROWSER 237”Markup Languages 238
Uniform Resource Locators 239
HTML Documents 240
The Document Object Model 242
Tree Lexicon 243
Interpreting the DOM 244
Cascading Style Sheets 244
XML and Friends 248
JavaScript 251
jQuery 253
SVG 254
HTML5 255
JSON 255
Summary 256
10 APPLICATION AND SYSTEM PROGRAMMING 259
Section titled “10 APPLICATION AND SYSTEM PROGRAMMING 259”Guess the Animal Version 1: HTML and JavaScript 262
Application-Level Skeleton 263
Web Page Body 263
The JavaScript 264
The CSS 267
Guess the Animal Version 2: C 267
Terminals and the Command Line 267
Building the Program 268
Terminals and Device Drivers 268
Context Switching 269
Standard I/O 271
Circular Buffers 272
Better Code Through Good Abstractions 273
Some Mechanics 274
Buffer Overflow 275
The C Program 275
Training 281
Summary 282
Contents in Detail xv
11 SHORTCUTS AND APPROXIMATIONS
Section titled “11 SHORTCUTS AND APPROXIMATIONS”- Table Lookup
- Conversion
- Texture Mapping
- Character Classification
- Integer Methods
- Straight Lines
- Curves Ahead
- Polynomials
- Recursive Subdivision
- Spirals
- Constructive Geometry
- Shifting and Masking
- More Math Avoidance
- Power Series Approximations
- The CORDIC Algorithm
- Somewhat Random Things
- Space-Filling Curves
- L-Systems
- Going Stochastic
- Quantization
- Summary
12 DEADLOCKS AND RACE CONDITIONS
Section titled “12 DEADLOCKS AND RACE CONDITIONS”- What Is a Race Condition?
- Shared Resources
- Processes and Threads
- Locks
- Transactions and Granularity
- Waiting for a Lock
- Deadlocks
- Short-Term Lock Implementation
- Long-Term Lock Implementation
- Browser JavaScript
- Asynchronous Functions and Promises
- Summary
13 SECURITY
Section titled “13 SECURITY”- Overview of Security and Privacy
- Threat Model
- Trust
- Physical Security
- Communications Security
- Modern Times
Metadata and Surveillance 359
The Social Context 359
Authentication and Authorization 361
Cryptography 362
Steganography 362
Substitution Ciphers 363
Transposition Ciphers 365
More Complex Ciphers 366
One-Time Pads 367
The Key Exchange Problem 367
Public Key Cryptography 368
Forward Secrecy 369
Cryptographic Hash Functions 369
Digital Signatures 370
Public Key Infrastructure 370
Blockchain 371
Password Management 371
Software Hygiene 372
Protect the Right Stuff 372
Triple-Check Your Logic 373
Check for Errors 373
Minimize Attack Surfaces 373
Stay in Bounds 374
Generating Good Random Numbers Is Hard 375
Know Thy Code 376
Extreme Cleverness Is Your Enemy 378
Understand What’s Visible 378
Don’t Overcollect 379
Don’t Hoard 379
Dynamic Memory Allocation Isn’t Your Friend 379
Garbage Collection Is Not Your Friend Either 381
Data as Code 382
Summary 384
14 MACHINE INTELLIGENCE 385
Section titled “14 MACHINE INTELLIGENCE 385”Overview 386
Machine Learning 388
Bayes 389
Gauss 390
Sobel 393
Canny 398
Feature Extraction 399
Neural Networks 401
Using Machine Learning Data 406
Artificial Intelligence 407
Big Data 409
Summary 412
Contents in Detail xvii
15 REAL-WORLD CONSIDERATIONS
Section titled “15 REAL-WORLD CONSIDERATIONS”The Value Proposition … 414
How We Got Here … 416
A Short History … 416
Open Source Software … 419
Creative Commons … 420
The Rise of Portability … 420
Package Management … 421
Containers … 422
Java … 422
Node.js … 424
Cloud Computing … 424
Virtual Machines … 425
Portable Devices … 425
The Programming Environment … 425
Are You Experienced? … 426
Learning to Estimate … 426
Scheduling Projects … 426
Decision Making … 427
Working with Different Personalities … 428
Navigating Workplace Culture … 429
Making Informed Choices … 430
Development Methodologies … 430
Project Design … 432
Writing It Down … 432
Fast Prototyping … 432
Interface Design … 433
Reusing Code or Writing Your Own … 436
Project Development … 436
The Talk … 437
Portable Code … 439
Source Control … 440
Testing … 440
Bug Reporting and Tracking … 441
Refactoring … 441
Maintenance … 441
Be Stylish … 442
Fix, Don’t Re-create … 443
Summary … 444
445
xviii Contents in Detail