Current Version, Ch01-03
Total Page:16
File Type:pdf, Size:1020Kb
Digital Logic and Computer Organization Neal Nelson c May 2013 Contents 1 Numbers and Gates 5 1.1 Numbers and Primitive Data Types . .5 1.2 Representing Numbers . .6 1.2.1 Decimal and Binary Systems . .6 1.2.2 Binary Counting . .7 1.2.3 Binary Conversions . .9 1.2.4 Hexadecimal . 11 1.3 Representing Negative Numbers . 13 1.3.1 Ten's Complement . 14 1.3.2 Two's Complement Binary Representation . 18 1.3.3 Negation in Two's Complement . 20 1.3.4 Two's Complement to Decimal Conversion . 21 1.3.5 Decimal to Two's Complement Conversion . 22 1.4 Gates and Circuits . 22 1.4.1 Logic Expressions . 23 1.4.2 Circuit Expressions . 24 1.5 Exercises . 26 2 Logic Functions 29 2.1 Functions . 29 2.2 Logic Functions . 30 2.2.1 Primitive Gate Functions . 31 2.2.2 Evaluating Logic Expressions and Circuits . 31 2.2.3 Logic Tables for Expressions and Circuits . 32 2.2.4 Expressions as Functions . 34 2.3 Equivalence of Boolean Expressions . 36 2.4 Logic Functional Completeness . 36 2.5 Boolean Algebra . 37 2.6 Tables, Expressions, and Circuits . 37 1 2.6.1 Disjunctive Normal Form . 38 2.6.2 Logic Expressions from Tables . 39 2.7 Exercises . 41 3 Dataflow Logic 43 3.1 Data Types . 44 3.1.1 Unsigned Int . 45 3.1.2 Signed Int . 45 3.1.3 Char and String . 45 3.2 Data Buses . 46 3.3 Bitwise Functions . 48 3.4 Integer Functions . 51 3.5 Decoders . 57 3.6 Multiplexers . 58 3.7 Control Logic . 60 3.8 Data Path Circuits . 61 3.9 Exercises . 63 4 Integer Arithmetic and Adders 65 4.1 Unsigned Addition . 65 4.2 Signed Addition and Subtraction . 66 4.3 Adder-Subtracter Circuit . 66 4.4 Multiplication and Division . 73 4.5 Floating Point Numbers . 73 4.6 Exercises . 73 5 Registers and Sequential Logic 76 5.1 Clocked Logic . 76 5.2 Memory Bits . 78 5.3 Registers . 81 5.4 Counter and Shifter Registers . 82 5.5 Register Transfers . 83 5.6 Register Transfer Sequences . 86 5.7 Adder Datapaths . 88 5.8 Sample RT Sequences for Adder Datapaths . 91 5.9 Exercises . 93 6 Memory and Stored Programs 90 6.1 Stored Programs . 92 6.2 Memory Components . 93 2 6.3 Instruction Memory . 95 6.4 Instruction Fetch Datapath . 96 6.5 Instruction Execution Datapath . 98 6.6 Combined Fetch-Execute Datapath . 100 6.7 Exercises . 102 7 Instruction Set Architecture 104 7.1 SAM - Simple Accumulator Machine . 104 7.2 SAM Word Size, Data Types, and Registers . 105 7.3 SAM Address Space and Physical Memory . 105 7.4 SAM Assembly Language and Machine Code . 106 7.5 SAM Instruction Set . 109 7.6 SAM Instruction Execution Traces . 111 7.7 Exercises . 113 8 Register Transfer Architecture 115 8.1 Stored Program Architecture . 115 8.2 Fetch-Execute Cycle . 116 8.3 RT Machine Organization . 116 8.4 The SAM Datapath . 118 8.5 Instruction Decoder . 119 8.6 Control Sequencer . 120 8.7 Instruction Fetch Control Sequence . 121 8.8 Instruction Execution Control Sequences . 122 8.9 Control Sequence Logic . 123 8.10 The SAM . 125 8.11 Exercises . 125 9 Assembly Language Programming 111 9.1 Assembly Code . 111 9.2 Machine Code . 111 9.3 Code Sequences . 111 9.4 Decisions . 111 9.5 Loops . 111 9.6 Overflow and Carry . 111 9.7 Exercises . 111 3 Preface This book is a reference for Logic and Computer Organization at The Ev- ergreen State College. We presume a stored-program processor architecture built using silicon technology in a non-quantum setting. The heart of a computer is the processor. We will learn the inner work- ings of a processor by studying the construction of a processor from primitive digital logic gates realizable in hardware. The presentation is intellectually honest, but greatly simplified relative to current technology. Nevertheless, the essential ideas are the same and the resulting processor that we explore can do everything any other processor can do { given enough time and space. The next significant revolution in computer organization will come only with a quantum processor. I want readers to hear my thanks to the Computer Science Foundations students of 2013-14 and 2014-15 for beta testing this text in their study of Digital Logic and Computer Organization. I especially thank Mary Kallam for her careful editing of the original version. 4 Chapter 1 Numbers and Gates This chapter introduces the two most fundamental ideas upon which we will build our processor. First, processors operate on symbolic and numerical data so we will need to represent symbols and numbers in our machines. Second, we will need primitive logic components that can be constructed in hardware and used both in combinations and sequences to compute. The magic of how a single primitive logic gate can be combined with feed- back and a hardware clock to run complex programs will unfold throughout the remaining chapters. 1.1 Numbers and Primitive Data Types In the first part of this chapter we examine the binary representation of inte- gers. Integers are one of several kinds of numbers in mathematics. Numbers in general (and integers in particular) are abstract mathematical entities whose existence is only accessible through our minds and thoughts. Num- bers have proved very useful in understanding our material world quantita- tively, but we must have a standard way to denote specific numbers - a way to write them down and communicate them to each other in text and formu- las. A system of representing numbers in written form is called a numeral system. Our commonly accepted practice for denoting numbers in written form is the called the Hindu-Arabic decimal numeral system. We also need a standard way of representing numbers in computing systems. This chapter is concerned with the denotation and representation of numbers for computing systems. In programming languages numbers show up as one of a small set of primitive data types that are closely related to the set of primitive values 5 built into all processors. In this chapter we will specifically focus on the representation of integers, leaving real numbers and other primitive types for later study. By the end of this chapter we will know how integers in mathematics are denoted by decimal numerals in writing, and commonly coded as the primitive data type Int in programming languages, and subsequently stored in computing systems in binary using a two's complement representation in a fixed size computer word. 1.2 Representing Numbers The earliest counting systems began with the Natural Numbers which we think of now as the collection or set of positive integers: f1; 2; 3;:::g The number zero was historically much later in achieving the status of a number, but zero is such a handy and natural number when you get used to it that it is now commonly included as a natural number. So although there is not complete agreement on the matter of zero, we will assume that the natural numbers are the set of non-negative integers: N = f0; 1; 2; 3;:::g 1.2.1 Decimal and Binary Systems It is a convenient fact that our Hindu-Arabic decimal system of numerals has a base of ten and we have ten digits on our hands. It is an equally convenient fact that both logic and numbers can be represented in a base two binary system and hardware can efficiently store and manipulate binary information. So our understanding of logic and numbers translates directly into hardware, but the somewhat inconvenient fact is that we must translate between base two and base ten representations of numbers. A decimal system of numerals is a base 10 system with 10 distinct digits. A binary system of numerals is a base 2 system with only two distinct digits, which we denote with the usual symbols 0 and 1. It is a common convention in mathematical logic to use the symbol F for false, and T for true. Because both systems are binary, we can choose either convention and both conventions are commonly used. We will initially use both conventions. When we are speaking about logic in the beginning of the text we'll use F and T as is common in logic. When we are speaking about circuits and numbers, we will migrate toward using 0 and 1 as is common in digital logic and Boolean algebra, where 0 is interpreted as equivalent to F , that is, false 6 and 1 is interpreted as equivalent to T , true. Eventually we'll converge on the usual digital logic convention of using 0 and 1 for both binary digits and the logic constants. The actual circuit hardware, of course, does not care a hoot about the symbols. The hardware will simply insist that two distinct voltage levels represent the two binary constants. 1.2.2 Binary Counting The Hindu-Arabic decimal numeral system is a positional system with each digit position tracking some power of 10 that contributes to the value of the number. A fairly natural understanding of this system comes from following a counting sequence in three decimal digits: 000 001 002 ::: 009 010 011 012 ::: 019 . 090 091 092 ::: 099 100 101 102 ::: 109 . 990 991 992 ::: 999 Each time we reach 9 in an given position we start over at 0 and also add one to the digit immediately to the left. Of course when get to a sequence of 9's this so-called rollover has a ripple effect as we can see from the rollover from 99 to 100 above.