Chapter 3A The CPU Computer Arithmetic Arithmetic & Logic Unit • Does the calculations • Everything else in the computer is there to service this unit • Handles integers • May handle floating point (real) numbers ALU Inputs and Outputs Binary numbers • Binary number is simply a number comprised of only 0's and 1's. • Computers use binary numbers because it's easy for them to communicate using electrical current -- 0 is off, 1 is on. • You can express any base 10 (our number system -- where each digit is between 0 and 9) with binary numbers. • The need to translate decimal number to binary and binary to decimal. • There are many ways in representing decimal number in binary numbers. 4 Bit Vs Byte Vs Word • A bit is the most basic unit of information in a computer. • It is a state of “on” or “off” in a digital circuit. • Sometimes they represent high or low voltage • A byte is a group of eight bits.. It is the smallest possible addressable unit of computer storage. • A word is a contiguous group of bytes. —Words can be any number of bits or bytes. —Word sizes of 16, 32, or 64 bits are most common. 5 How does the binary system work? • It is just like any other system • In decimal system the base is 10 —We have 10 possible values 0-9 • In binary system the base is 2 —We have only two possible values 0 or 1. —The same as in any base, 0 digit has non contribution, where 1’s has contribution which depends at there position. 6 Example • 3040510 = 30000 + 400 + 5 = 3*104+4*102+5*100 • 101012 =10000+100+1 =1*24+1*22+1*20 • Decimal – binary • 41 = • 32+8+1 41= 1 x 25 + 0 x 24 + 1 x 23 + 0 x 22 + 0 x 2 + 1 = (101001) • Find the binary representation of 12910. • Find the decimal value represented by the following binary representations: — 10000011 — 10101010 7 Integer Representations —Unsigned notation —Signed magnitude notion —One’s complement —Two’s complement notation. 8 Unsigned Representation • Represents positive integers. • Unsigned representation of 157: position 7 6 5 4 3 2 1 0 Bit pattern 1 0 0 1 1 1 0 1 contribution 27 24 23 22 20 • Addition is simple: 1 0 0 1 + 0 1 0 1 = 1 1 1 0. 9 Advantages and disadvantages of unsigned notation • Advantages: —One representation of zero —Simple addition • Disadvantages —Negative numbers can not be represented. —The need of different notation to represent negative numbers. 10 Representation of negative numbers • Is a representation of negative numbers possible? • Unfortunately: —you can not just stick a negative sign in front of a binary number. (it does not work like that) • There are three methods used to represent negative numbers. —Signed magnitude notation —One’ complement —Two’s complement notation 11 Signed Magnitude Representation • Unsigned: - and + are the same. • In signed magnitude – the left-most bit represents the sign of the integer. +0 for positive numbers. +1 for negative numbers. • The remaining bits represent to magnitude of the numbers. 12 Example • Suppose 10011101 is a signed magnitude representation. • The sign bit is 1, then the number represented is negative position 7 6 5 4 3 2 1 0 Bit pattern 1 0 0 1 1 1 0 1 24 23 22 contribution - 20 • The magnitude is 0011101 with a value 24+23+22+20= 29 • Then the number represented by 10011101 is –29. • Example • +18 = 00010010 • -18 = 10010010 13 Sign-Magnitude Arithmetic If sign is the same, just add the magnitude Sign-Magnitude Arithmetic Sign-Magnitude Arithmetic Exercise 1 1. 2. 3. 3710 has 0010 0101 in signed magnitude notation. Find the signed magnitude of –3710 ? Using the signed magnitude notation find the 8-bit binary representation of the decimal value 2410 and -2410. Find the signed magnitude of –63 using 8-bit binary sequence? 4. Add 100100112 (-19) to 000011012 (+13) using signed-magnitude arithmetic. 17 Disadvantage of Signed Magnitude • Addition and subtractions are difficult. • Signs and magnitude, both have to carry out the required operation. • There are two representations of 0 – 00000000 = + 010 – 10000000 = - 010 – To test if a number is 0 or not, the CPU will need to see whether it is 00000000 or 10000000. – 0 is always performed in programs. + Therefore, having two representations of 0 is inconvenient. 18 Signed-Summary • In signed magnitude notation, – The most significant bit is used to represent the sign. – 1 represents negative numbers – 0 represents positive numbers. – The unsigned value of the remaining bits represent The magnitude. • Advantages: – Represents positive and negative numbers • Disadvantages: – two representations of zero, – Arithmetic operations are difficult. 19 One’s complement • In diminished radix complement systems, a negative value is given by the difference between the absolute value of a number and one less than its base. • In the binary system, this gives us one’s complement. It amounts to little more than flipping the bits of a binary number. 20 One’s complement • For example, in 8-bit one’s complement, positive 3 is: 00000011 • Negative 3 is: 11111100 • In one’s complement, as with signed magnitude, negative values are indicated by a 1 in the high order bit. • Complement systems are useful because they eliminate the need for subtraction. The difference of two values is found by adding the minuend to the complement of the subtrahend. 21 One’s complement • With one’s complement addition, the carry bit is “carried around” and added to the sum. —Example: Using one’s complement binary arithmetic, find the sum of 48 and - 19 We note that 19 in one’s complement is 00010011, so -19 in one’s complement is: 11101100. 22 Example: Binary subtraction using 1’s complement 1’s complement: M – N = M + N’ Regular Approach: M= 01010100 M-N N= 10111011 + M= 01010100 ------------------N= 01000100 100001111 ------------------End Around Carry 00010000 1+ 00010000 Regular Approach: N-M N= 01000100 M=01010100------------------- 00010000 1’s complement: N + M’ N= 01000100 M=10101011+ ------------------No End Carry 11101111 Correction Step Required: -(1’s complement of 11101111) = -(00010000) One’s complement • Although the “end carry around” adds some complexity, one’s complement is simpler to implement than signed magnitude. • But it still has the disadvantage of having two different representations for zero: positive zero and negative zero. • Two’s complement solves this problem. It is the radix complement of the binary numbering system. 24 Twos Complement • Most common scheme of representing negative numbers in computers • Affords natural arithmetic (no special rules!) • To represent a negative number in 2’s complement notation… 1. Decide upon the number of bits (n) 2. Find the binary representation of the +ve value in n-bits 3. Flip all the bits (change 1’s to 0’s and vice versa) 4. Add 1 Twos Complement Example • Represent -5 in binary using 2’s complement notation 1. Decide on the number of bits 6 (for example) 1. Find the binary representation of the +ve value in 6 bits 000101 +5 1. Flip all the bits 111010 1. Add 1 111010 + 1 111011 -5 Sign Bit • In 2’s complement notation, the MSB is the sign bit (as with sign-magnitude notation) —0 = positive value —1 = negative value +5: 0 0 0 1 0 1 +ve 5 -5: 1 1 1 0 1 1 -ve ? (previous slide) “Complementary” Notation • Conversions between positive and negative numbers are easy • For binary (base 2)… 2’s C +ve -ve 2’s C Example +5 0 0 0 1 0 1 2’s C 1 1 1 0 1 0 + 1 -5 1 1 1 0 1 1 2’s C 0 0 0 1 0 0 + 1 +5 0 0 0 1 0 1 Exercise – 2’s C conversions • What is -20 expressed as an 8-bit binary number in 2’s complement notation? —Answer: • 1100011 is a 7-bit binary number in 2’s complement notation. What is the decimal value? —Answer: Skip answer Answer Exercise – 2’s C conversions Answer • What is -20 expressed as an 8-bit binary number in 2’s complement notation? —Answer: 1101100 • 1100011 is a 7-bit binary number in 2’s complement notation. What is the decimal value? —Answer: -29 Range for 2’s Complement • For example, 6-bit 2’s complement notation 100000 100001 -32 -31 111111 ... Negative, sign bit = 1 -1 000000 0 000001 1 011111 ... 31 Zero or positive, sign bit = 0 Ranges (revisited) Binary No. of bits Unsigned Min Max 1 0 1 2 0 3 Sign-magnitude 2’s complement Min Max Min Max 3 -1 1 -2 1 0 7 -3 3 -4 3 4 0 15 -7 7 -8 7 5 0 31 -15 15 -16 15 6 0 63 -31 31 -32 31 In General (revisited) No. of bits Unsigned Min Max n 0 n Binary Sign-magnitude 2’s complement Min 2 - 1 -(2 n-1 Max Min n-1 - 1) 2 -1 -2 n-1 Max 2 n-1 -1 Example: Binary subtraction using 2’s complement Regular Approach: 2’s complement: M-N M – N = M + N’ M= 01010100 M= 01010100 N= 01000100 N= 10111100 + ------------------------------------Discard End Carry 00010000 100010000 Regular Approach: N-M N=01000100 M=01010100------------------- 00010000 2’s complement: N + M’ N= 01000100 M=10101100+ ------------------No End Carry 11110000 Correction Step Required: -(2’s complement of 11110000) = -(00010000) What is -5 plus +5? • Zero, of course, but let’s see Sign-magnitude Twos-complement 11 1 1 1 1 11 -5: +5: 10000101 +00000101 10000000 -5: +5: 11111011 +00000101 00000000 2’s Complement Subtraction • Easy • No special rules • Just subtract, well … actually … just add! A – B = A + (-B) add 2’s complement of B What is 10 subtract 3? • 7, of course, but… • Let’s do it (we’ll use 6-bit values) 10 – 3 = 10 + (-3) = 7 +3: 000011 1s C: 111100 +1: 1 -3: 111101 001010 +111101 000111 What is 10 subtract -3? (-(-3)) = 3 • 13, of course, but… • Let’s do it (we’ll use 6-bit values) 10 – (-3) = 10 + (-(-3)) = 13 -3: 111101 1s C: 000010 +1: 1 +3: 000011 001010 +000011 001101 Benefits • One representation of zero • Arithmetic works easily (see later) • Negating is fairly easy —3 = 00000011 —Boolean complement gives 11111100 —Add 1 to LSB 11111101 Hardware for Addition and Subtraction Multiplication • • • • Complex Work out partial product for each digit Take care with place value (column) Add partial products • • • • • • • 1011 Multiplicand (11 dec) x 1101 Multiplier (13 dec) 1011 Partial products 0000 Note: if multiplier bit is 1 copy 1011 multiplicand (place value) 1011 otherwise zero 10001111 Product (143 dec) Note: need double length result Multiplying Negative Numbers • This does not work! • Solution 1 —Convert to positive if required —Multiply as above —If signs were different, negate answer • Example 6 * -2= -12 6=0110 2=0010 Multiplication Example • 0110 Multiplicand (6 dec) • x 0010 Multiplier (2 dec) • 0000 Partial products • 0110 • 01100 Product (12 dec) Note: If signs were different, negate answer So the answer is -12 • Division —More complex than multiplication —Negative numbers are really bad! —Based on long division • 6 /2=3 • 0110 0010 0 0011 01 0 011 0010 00010 0010 00000 Advantages of Two’s Complement Notation • One representation of zero • It is easy to add two numbers. 0001 +0 1 0 1 0110 +1 +5 +6 1 0 0 0 -8 + 0 1 0 1 +5 1 1 0 1 -3 • Subtraction can be easily performed. • Multiplication is just a repeated addition. • Division is just a repeated subtraction • Two’s complement is widely used in ALU 48 Floating-Point Representation • The signed magnitude, 1’s complement, and 2’s complement representations as such are not useful in scientific or business applications that deal with real number values over a wide range. • Floating-point representation solves this problem. 49 Floating-Point Representation • Most important computers use floating point binary to represent real numbers. • Because floating point binary makes particularly efficient use of computer register when we comes to representing ever extremely large values, extremely small values, or value that require high degree of precision. Floating-Point Representation • Computers use a form of scientific notation for floating-point representation • Numbers written in scientific notation have three components: 51 Floating-Point Representation • Computer representation of a floating-point number consists of three fixed-size fields: • This is the standard arrangement of these fields. 52 Floating-Point Representation • The one-bit sign field is the sign of the stored value. • The size of the exponent field, determines the range of values that can be represented. • The size of the significand determines the precision of the representation. 53 Floating-Point Representation • The significand of a floating-point number is always preceded by an implied binary point. • Thus, the significand always contains a fractional binary value. • The exponent indicates the power of 2 to which the significand is raised. 54 Floating-Point Representation • The IEEE-754 single precision floating point standard uses an 8-bit exponent and a 23-bit significand. • The IEEE-754 double precision standard uses an 11-bit exponent and a 52-bit significand. For illustrative purposes, we will use a 14-bit model with a 5-bit exponent and an 8-bit significand. 55 Floating-Point Representation • Example: —Express 3210 in the simplified 14-bit floating-point model. • We know that 32 is 25. So in (binary) scientific notation 32 = 1.0 x 25 = 0.1 x 26. • Using this information, we put 110 (= 610) in the exponent field and 1 in the significand as shown. 56 Floating-Point Representation • The illustrations shown at the right are all equivalent representations for 32 using our simplified model. • Not only do these synonymous representations waste space, but they can also cause confusion. Because synonymous forms such as these are not well-suited for digital computers, a convention has been established where the leftmost bit of the significand will always be a 1. This is called normalization. 57 Floating-Point Representation • Another problem with our system is that we have made no allowances for negative exponents. We have no way to express 0.5 (=2 -1)! (Notice that there is no sign in the exponent field!) All of these problems can be fixed with no changes to our basic model. 58 Floating-Point Representation • To resolve the problem of synonymous forms, we will establish a rule that the first digit of the significand must be 1. This results in a unique pattern for each floating-point number. —In the IEEE-754 standard, this 1 is implied meaning that a 1 is assumed after the binary point. —By using an implied 1, we increase the precision of the representation by a power of two. (Why?) In our simple instructional model, we will use no implied bits. 59 Floating-Point Representation • To provide for negative exponents, we will use a biased exponent. • A bias is a number that is approximately midway in the range of values expressible by the exponent. We subtract the bias from the value in the exponent to determine its true value. —In our case, we have a 5-bit exponent. We will use 16 for our bias. This is called excess-16 representation. • In our model, exponent values less than 16 are negative, representing fractional numbers. 60 Floating-Point Representation • Example: —Express 3210 in the revised 14-bit floatingpoint model. • We know that 32 = 1.0 x 25 = 0.1 x 26. • To use our excess 16 biased exponent, we add 16 to 6, giving 2210 (=101102). Graphically: 61 Steps for Converting Fractions 1. Successively multiply the given fraction by the required base, noting the integer portion of the product at each step. Use the fractional part of the product as the multiplicand for subsequent steps. Stop when the fraction either reaches 0 or recurs. 2. Collect the integer digits at each step from first to last and arrange them left to right. Floating-Point Representation • Example: —Express 0.062510 in the revised 14-bit floating-point model. • We know that 0.0625 is 2-4. So in (binary) scientific notation 0.0625 = 1.0 x 2-4 = 0.1 x 2 -3. • To use our excess 16 biased exponent, we add 16 to -3, giving 1310 (=011012). 63 Floating-Point Representation • Example: —Express 26.62510 in the revised 14-bit floating-point model. • We find 26.62510 = 11010.1012. Normalizing, we have: 26.62510 = 0.11010101 x 2 5. • To use our excess 16 biased exponent, we add 16 to 5, giving 2110 (=101012). We also need a 1 in the sign bit. 64 Character Codes • Calculations aren’t useful until their results can be displayed in a manner that is meaningful to people. • We also need to store the results of calculations, and provide a means for data input. • Thus, human-understandable characters must be converted to computer-understandable bit patterns using some sort of character encoding scheme. 65 Character Codes • As computers have evolved, character codes have evolved. • Larger computer memories and storage devices permit richer character codes. • The earliest computer coding systems used six bits. • Binary-coded decimal (BCD) was one of these early codes. It was used by IBM mainframes in the 1950s and 1960s. 66 Character Codes • In 1964, BCD was extended to an 8-bit code, Extended Binary-Coded Decimal Interchange Code (EBCDIC). • EBCDIC was one of the first widely-used computer codes that supported upper and lowercase alphabetic characters, in addition to special characters, such as punctuation and control characters. • EBCDIC and BCD are still in use by IBM mainframes today. 67 Character Codes • Other computer manufacturers chose the 7-bit ASCII (American Standard Code for Information Interchange) as a replacement for 6-bit codes. • While BCD and EBCDIC were based upon punched card codes, ASCII was based upon telecommunications (Telex) codes. • Until recently, ASCII was the dominant character code outside the IBM mainframe world. 68 Character Codes • Many of today’s systems embrace Unicode, a 16bit system that can encode the characters of every language in the world. —The Java programming language, and some operating systems now use Unicode as their default character code. • The Unicode codespace is divided into six parts. The first part is for Western alphabet codes, including English, Greek, and Russian. 69 Character Codes • The Unicode codespace allocation is shown at the right. • The lowest-numbered Unicode characters comprise the ASCII code. • The highest provide for user-defined codes. 70 The CPU –Instruction set: Characteristics and Functions –Instruction set: Addressing Modes –Processor Organization –Real World Computer Architectures 71 Machine Instruction Characteristics • The operation of the processor is determined by the instructions it executes, referred to as machine instructions or computer instructions • The collection of different instructions that the processor can execute is referred to as the processor’s instruction set • Each instruction must contain the information required by the processor for execution 72 Elements of an Instruction • Operation code (opcode) – Specifies the operation to be performed – Do this: ADD, SUB, MPY, DIV, LOAD, STOR • Source operand reference – operands that are inputs for the operation – To this: (address of) argument of op, e.g. register, memory location • Result operand reference – Put the result here (as above) • Next instruction reference (often implicit) – When you have done that, do this: BR 73 Instruction Representation • Within the computer each instruction is represented by a sequence of bits • The instruction is divided into fields, corresponding to the constituent elements of the instruction 74 Instruction Representation….. • Opcodes are represented by abbreviations, called mnemonics, that indicate the operation. Common examples include – ADD Add – SUB Subtract – MUL Multiply – DIV Divide – LOAD Load data from memory – STOR Store data to memory • Operands are also represented symbolically. For example, the instruction ADD R, Y may mean add the value contained in data location Y to the contents of register R. Y refers to the address of a location in memory, and R refers to a particular register. 75 Instruction Set Design(1) The most important fundamental design issues include the following: • Operation repertoire – How many operations? – which operations to provide – How complex operations should be • Data types: The various types of data upon which operations are performed • Instruction formats – Instruction length (in bits), – number of addresses, – size of various fields, and so on 76 Instruction Set Design(2) • Registers – Number of CPU registers available – Which operations can be performed on which registers? – General purpose and specific registers • Addressing: The mode or modes by which the address of an operand is specified 77 Instruction Types • Data processing: arithmetic and logical instructions • Data movement: I/O instructions • Data storage: Movement of data into or out of register and or memory locations • Control: Test and branch instructions – Test instructions are used to test the value of a data word or the status of a computation. – Branch instructions are then used to branch to a different set of instructions depending on the decision made. 78 Processor Actions for Various Types of Operations 79 Types of Operands • Addresses: addresses are, in fact, a form of data. addresses can be considered to be unsigned integers. • Numbers: All machine languages include numeric data types. Three types of numerical data are common in computers: Binary integer or binary fixed point , Binary floating point, decimal. • Characters: ASCII (128 printable and control characters + bit for error detection) • Logical Data: bits or flags, e.g., Boolean 0 and 1 80 Number of Addresses • More addresses – More complex (powerful?) instructions – More registers - inter-register operations are quicker – Less instructions per program • Fewer addresses – Less complex (powerful?) instructions – More instructions per program, e.g. data movement – Faster fetch/execution of instructions • Example: Y=(A-B):[(C+(DxE)] 81 3 addresses Operation Result, Operand 1, Operand 2 – Not common – Needs very long words to hold everything SUB Y,A,B Y <- A-B MPY T,D,E T <- DxE ADD T,T,C T <- T+C DIV Y,Y,T Y <- Y:T 82 2 addresses One address doubles as operand and result – Reduces length of instruction – Requires some extra work: temporary storage MOVE Y,A Y <- A SUB Y,B Y <- Y-B MOVE T,D T <- D MPY T,E T <- TxE ADD T,C T <- T+C DIV Y,T Y <- Y:T 83 1 address Implicit second address, usually a register (accumulator, AC) LOAD D AC <- D MPY E AC <- ACxE ADD C AC <- AC+C STOR Y Y <- AC LOAD A AC <- A SUB B AC <- AC-B DIV Y AC <- AC:Y STOR Y Y <- AC 84 0 (zero) addresses All addresses implicit, e.g. ADD – Uses a stack, e.g. pop a, pop b, add –c=a+b 85 Addressing Modes • addressing mode – method of forming a memory address • For a given instruction set architecture, addressing modes define how machine language instructions identify the operand (or operands) of each instruction. • Common addressing modes – – – – – – – Immediate Direct Indirect Register Register Indirect Displacement (Indexed) Stack 86 we use the f we use the In Addressing modes , we use the following notation: Basic Addressing Modes 87 Immediate Addressing • Simplest form of addressing • Operand = A • This mode can be used to define and use constants or set initial values of variables • the instruction itself contains the value to be used • Similar to using a constant in a high level language • Advantage: – fast, since the value is included in the instruction; no memory reference to fetch data – Disadvantage: – not flexible, since the value is fixed at compile-time – can have limited range in machines with fixed length instructions • Example – add #5 , Add number 5 to contents of accumulator – Mov CH,3AH , move the data 3AH to the register CH 88 Direct Addressing • • • • • • – – • • Address field contains the effective address of the operand. Effective address (EA) = address field (A) Was common in earlier generations of computers. Requires only one memory reference and no special calculation In a high level language, direct addressing is frequently used for things like global variables. Advantage Single memory reference to access data More flexible than immediate Limitation is that it provides only a limited address space. Example – Add A, add the contents of memory cell A to the accumulator. 89 Direct Addressing Diagram Instruction Opcode Address A Memory Operand 90 Indirect Addressing • Memory cell pointed to by address field contains the address of (pointer to) the operand • EA = (A) – Parentheses are to be interpreted as meaning contents of – Look in A, find address (A) and look there for operand • Advantage: – For a word length of N an address space of 2N is now available - Large address space • Disadvantage: – Instruction execution requires two memory references to fetch the operand -hence slower • One to get its address and a second to get its value • Example – ADD (A) • Add contents of cell pointed to by contents of A to accumulator. – ADD AX, (A) • Add contents of cell pointed to by contents of A to register AX 91 Indirect Addressing Diagram Instruction Opcode Address A Memory Pointer to operand Operand 92 Register Addressing • Address field refers to a register rather than a main memory address • EA = R • Advantages: – Only a small address field is needed in the instruction – No time-consuming memory references are required • Disadvantage: – The address space is very limited • Example – MOV AX, BX – ADD AX, BX 93 Register Addressing Diagram Instruction Opcode Register Address R Registers Operand 94 Register Indirect Addressing • Analogous to indirect addressing – The only difference is whether the address field refers to a memory location or a register • EA = (R) • Address space limitation of the address field is overcome by having that field refer to a word-length location containing an address • Uses one less memory reference than indirect addressing • Example – ADD (R) 95 Register Indirect Addressing Diagram Instruction Opcode Register Address R Memory Registers Pointer to Operand Operand 96 Displacement Addressing • Combines the capabilities of direct addressing and register indirect addressing • EA = A + (R) • Requires that the instruction have two address fields, at least one of which is explicit – The value contained in one address field (value = A) is used directly – The other address field refers to a register whose contents are added to A to produce the effective address – – – – Most common uses: Relative addressing Base-register addressing Indexing 97 Displacement Addressing Diagram Instruction Opcode Register R Address A Memory Registers Displacement + Operand 98 Relative Addressing • EA = A + (PC) • Address field A is treated as 2’s complement integer to allow backward references • Fetch operand from PC+A • Can be very efficient because of locality of reference & cache usage – But in large programs code and data may be widely separated in memory 99 Base-Register Addressing • A holds displacement • R holds pointer to base address • R may be explicit or implicit – E.g. segment registers in 80x86 are base registers and are involved in all EA computations – X86 processors have a wide variety of base addressing 100 Indexed addressing • A = Base • R = displacement • EA = A + R • Good for accessing arrays – EA = A + R – R++ • Iterative access to sequential memory locations is very common • Some architectures provide auto-increment or autodecrement • Pre index EA = A + (R++) • Post index EA = A + (++R) • The ARM architecture provides pre indexed and post indexed addressing 101 Stack Addressing • A stack is a linear array of locations – Sometimes referred to as a pushdown list or last-in-firstout queue • A stack is a reserved block of locations – Items are appended to the top of the stack so that the block is partially filled • Associated with the stack is a pointer whose value is the address of the top of the stack – The stack pointer is maintained in a register – Thus references to stack locations in memory are in fact register indirect addresses – Is a form of implied addressing – The machine instructions need not include a memory reference but implicitly operate on the top of the stack 102 Processor Organization Processor Requirements: • Fetch instruction – The processor reads an instruction from memory (register, cache, main memory) • Interpret instruction – The instruction is decoded to determine what action is required • Fetch data – The execution of an instruction may require reading data from memory or an I/O module • Process data – The execution of an instruction may require performing some arithmetic or logical operation on data • Write data – The results of an execution may require writing data to memory or an I/O module • In order to do these things the processor needs to store some data temporarily and therefore needs a small internal memory 103 CPU Internal Structure 104 Kinds of registers Within the processor there is a set of registers that function as a level of memory above main memory and cache in the hierarchy. User visible and modifiable • General Purpose • Data (e.g. accumulator) • Address (e.g. base addressing, index addressing) • Condition codes Control registers (not visible to user) • Program Counter (PC) • Instruction Register (IR) • Memory Address Register (MAR) • Memory Buffer Register (MBR) State register (visible to user but not directly modifiable) • Program Status Word (PSW) • 105 User visible registers . . … • A user-visible register is one that may be referenced by means of the machine language that the processor executes. • General-purpose registers can be assigned to a variety of functions by the programmer. – Advantages of general purpose registers • Increase flexibility and programmer options • Increase instruction size & complexity • Data registers may be used only to hold data and cannot be employed in the calculation of an operand address • Address registers may themselves be somewhat general purpose, or they may be devoted to a particular addressing mode. Examples include the following: – Segment pointers – Index registers – Stack pointer • Condition codes (also referred to as flags) are bits set by the processor hardware as the result of operations 106 Control Registers Four registers are essential to instruction execution • Program counter (PC) – Contains the address of an instruction to be fetched • Instruction register (IR) – Contains the instruction most recently fetched • Memory address register (MAR) – Contains the address of a location in memory • Memory buffer register (MBR) – Contains a word of data to be written to memory or the word most recently read 107 State Registers • Sets of individual bits – e.g. store if result of last operation was zero or not • Can be read (implicitly) by programs – e.g. Jump if zero • Can not (usually) be set by programs • There is always a Program Status Word (see next) • Possibly (for operating system purposes): – Interrupt vectors – Memory page table (virtual memory) – Process control blocks (multitasking) 108 Program Status Word (PSW) • The PSW typically contains condition codes plus other status information. • Common fields or flags include the following: Sign: Contains the sign bit of the result of the last arithmetic operation. Zero: Set when the result is 0. Carry: Set if an operation resulted in a carry (addition) into or borrow (sub-traction) out of a high-order bit. Used for multiword arithmetic operations. Equal: Set if a logical compare result is equality. Overflow: Used to indicate arithmetic overflow. Interrupt Enable/Disable: Used to enable or disable interrupts. Supervisor: Indicates whether the processor is executing in supervisor or user mode 109 General Purpose Registers • AX (Accumulator) – favored by CPU for arithmetic operations • BX – Base – can hold the address of a procedure or variable (SI, DI, and BP can also). – Can also perform arithmetic and data movement. • CX – acts as a counter for repeating or looping instructions. • DX – holds the high 16 bits of the product in multiply (also handles divide operations) Segment Registers – Used as base locations for program instructions, data and the stack • CS – Code Segment – holds base location for all executable instructions in a program • SS - Base location of the stack • DS – Data Segment – default base location for variables • ES – Extra Segment – additional base location for memory variables. Pointer Registers – Contain the offset of data(variables, labels) and instructions from its base segment. • BP – Base Pointer – contains an assumed offset from the SS register. Often used by a subroutine to locate variables that were passed on the stack by a calling program. • SP – Stack Pointer – Contains the offset of the top of the stack. Index registers – Speed up processing of strings, arrays, and other data structures containing multiple elements. • SI – Source Index – Used in string movement instructions. The source string is pointed to by the SI register. • DI – Destination Index – acts as the destination for string movement instructions.