Review Topics Midterm Exam Review Slides Original slides from Gregory Byrd, North Carolina State University Modified slides by Chris Wilcox, Colorado State University Computer Arithmetic Combinational Logic Finite State Machines C Programming gdb Debugging 2 What can a binary number mean? Interpretations of a 32-bit memory location: 32-bit floating point (IEEE) 32-bit unsigned/signed integer 6-bit unsigned/signed integer (2) 8-bit unsigned/signed bytes (4) ASCII characters (4) RISC instruction Control or status register.jpg..mpg,.mp3.,.avi, 3 Hexadecimal to Binary Conversion Method: Convert hexadecimal digits to binary using table. Question: What is hexadecimal 0xFEBD4570 in binary? F E B D 4 5 7 0 0 0 0 000 00 0 0000 Answer: 0000000000000 Hexadecimal Binary 0 0000 000 2 000 3 00 4 000 5 00 6 00 7 0 8 000 9 00 A 00 B 0 C 00 D 0 E 0 F 4
Binary to Hexadecimal Conversion Method: Group binary digits, convert to hex digits using table. Question: What is binary 0000000000000000 in hexadecimal? 00 0 0 000 000 00 0000 C D E F 2 3 0 Answer: 0xCDEF230 Hexadecimal Binary 0 0000 000 2 000 3 00 4 000 5 00 6 00 7 0 8 000 9 00 A 00 B 0 C 00 D 0 E 0 F 5 Decimal to Binary Conversion Method: Convert decimal to binary with divide by 2, check odd/even. Question: What is decimal 49 in binary? 49 is odd, prepend a 49 / 2 = 24 is even, prepend a 0 0 24 / 2 = 2 is even, prepend a 0 00 2 / 2 = 6 is even, prepend a 0 000 6 / 2 = 3 is odd, prepend a 000 3 / 2 = is odd, prepend a 000 Answer: 000 2 n Decimal 2 0 2 2 2 2 4 2 3 8 2 4 6 2 5 32 2 6 64 2 7 28 2 8 256 2 9 52 2 0 024 6 Binary to Decimal Conversion Method: Convert binary to decimal by multiplying by 2, add if bit set. Question: What is binary 00 in decimal? Start with 0 0 Left bit set, multiply by 2, add Left bit set, multiply by 2, add 3 Left bit clear, multiply by 2 6 Left bit set, multiply by 2, add 3 Left bit clear, multiply by 2 26 Left bit set, multiply by 2, add 53 Answer: 53 2 n Decimal 2 0 2 2 2 2 4 2 3 8 2 4 6 2 5 32 2 6 64 2 7 28 2 8 256 2 9 52 2 0 024 7 Binary to Floating Point Conversion Single-precision IEEE floating point number: 0 0000000000000000000000 sign exponent fraction Sign is number is negative. Exponent field is 0 = 27 27 = 0 (decimal). Fraction is.00000000000 =.5 (decimal). Value = -.5 x 2 (27-27) = -.5 x 2 0 = -.5 8 2
Floating Point to Binary Conversion Value = 6.25 Number is positive sign is 0 Fraction is 0.00 (binary), normalize to.000 * 2 2 Exponent is 2 + 27 = 29 (decimal) = 000000 Single-precision IEEE floating point number: 0 000000 000000000000000000000 sign exponent fraction 9 Hexadecimal to ASCII Conversion Method: Convert values to ASCII by table lookup. Each two (hex) digits is a single character. Question: What is hex 0x4245444 in ASCII? 0x42 = 'B' 0x45 = 'E' 0x4 = 'A' 0x44 = 'D' Answer: BEAD Char ASCII Code Char ASCII Code 'A' 0x4 '0' 0x30 'B' 0x42 '' 0x3 'C' 0x43 '2' 0x32 'D' 0x44 '3' 0x33 'E' 0x45 '4' 0x34 'F' 0x46 '5' 0x35 'G' 0x47 '6' 0x36 0 Computer Arithmetic Signed Integer Representations Binary Number Signed Magnitude s Complement 2 s Complement 0000 0 0 0 000 000 2 2 2 00 3 3 3 000 4 4 4 00 5 5 5 00 6 6 6 0 7 7 7 000-0 -7-8 00 - -6-7 00-2 -5-6 0-3 -4-5 00-4 -3-4 0-5 -2-3 0-6 - -2-7 -0 - Computer Arithmetic 2 s Complement Arithmetic Binary Arithmetic (unsigned integers): 0 0 0 0 0 + 0 0 0 0 0 0 0 0 Hex Equivalent: 0x92 + 0x35 = 0xC7 Decimal Equivalent: 46 + 53 = 99 Binary Arithmetic (signed integers): 0 0 0 0 0 + 0 0 0 0 0 0 0 0 Hex Equivalent: 0x92 + 0x35 = 0xC7 Decimal Equivalent: -0 + 53 = -57 2 3
Computer Arithmetic Bitwise Logical Operations Bitwise AND (&): 0 0 0 0 & 0 0 0 0 0 0 0 0 0 0 Hex Equivalent: 0xF0 & 035 = 0xC0 Bitwise OR ( ): 0 0 0 0 0 0 0 0 0 0 Hex Equivalent: 0xF0 035 = 0xF5 Transistor Basics (p-type and n-type) Gate voltage determines current flow between source and drain. P-type: 0V closes circuit, 2.9V opens circuit. N-type: 2.9V closes circuit, 0V opens circuit. 3 4 Transistor Basics (p-type and n-type) NOT Gate Transistors are switches which have a propagation delay, waveform is not ideal, and voltage transition is not instantaneous! A T T2 B 0 Closed Open Open Closed 0 5 6 4
NOR Gate NAND Gate A B T T2 T3 T4 C 0 0 Closed Closed Open Open 0 Closed Open Open Closed 0 0 Open Closed Closed Open 0 Open Open Closed Closed 0 A B T T2 T3 T4 C 0 0 Open Open Closed Closed 0 Open Closed Closed Open 0 Closed Open Open Closed Closed Closed Open Open 0 7 8 De Morgan s Law Converting AND to OR (with some help from NOT) Consider the following gate: A B A B A B 0 0 0 0 0 0 0 0 0 0 0 0 Same as A OR B! A B To convert AND to OR (or vice versa), invert inputs and output. NOT(NOT(A) AND NOT(B)) = A OR B NOT(NOT(A) OR NOT(B)) = A AND B Logical Completeness. AND/OR/NOT are logically complete, if you have enough gates you can build any truth table. 2. NAND/NOR are logically complete, same as above, so only these gates are sufficient! Proof : Programmable logic array proves that any truth table can be built from AND/OR/NOT. Proof 2: Can synthesize AND/OR/NOT from NAND/NOR, though it may take more gates. 9 20 5
Combinational Logic Combinational Circuit to Truth Table Combinational Logic Truth Table to Combinational Circuit A B C V W X Y Z 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 A B C X Y 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 22 Combinational Logic Decoder Circuit n inputs, 2 n outputs exactly one output is for each input pattern Combinational Logic Multiplexer Circuit n-bit selector and 2 n inputs, one output output equals one of the inputs, depending on selector A B O00 O0 O0 O 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23 24 6
Combinational Logic Full Adder Circuit Add two bits and carry-in, produce one-bit sum and carry-out. A B C in S C out 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Difference from Combinational Sequential circuits differ from combinational circuits because they have persistent state. For a combinational circuit, the outputs depend only on the inputs. For a sequential circuit, the outputs depend on the inputs and the state. Sequential circuits can be used to implement a finite state machine. 25 26 S-R Latch Circuit Suppose we start with output = 0, then change S to zero (Set), latch state will change to. Or we start with output =, then change R to zero (Reset), latch state will change to 0. Setting S or R back to makes latch quiescent, never do S = R = 0! 0 Output changes to one. 0 0 D-Latch Circuit Two inputs: D (data) and WE (write enable) when WE =, latch is set to value of D S = NOT(D), R = D when WE = 0, latch holds previous value S = R = 0 0 27 28 7
D-Latch Truth Table Exhaustive Testing WE D Previous Q New Q 0 x 0 0 0 x 0 x 0 x How many test cases for combinational logic? 2 n, where n is the number of input bits Example: 4-bit decoder requires 6 test cases How many test cases for sequential logic? 2 n * 2 m, where m is number of states Example: -bit D-latch requires 8 test cases 29 30 - Memory Architecture address write enable word select word WE input bits Memory Address Space and Width Now that we know how to store bits, we can build a memory a logical k m array of stored bits. Address Space: number of locations (usually a power of 2) k = 2 n locations address decoder output bits 3 Address Width (Addressability): number of bits per location (e.g., byte-addressable) m bits 32 8
Finite State Machines Finding States from Inputs Just follow the arrows, for example: Starting state is A Inputs given: 64, 3, 29, 47 States visited: C, D, C, E State outputs: 2, 3, 2, 4 Finite State Machines Finding Inputs from States Just follow the arrows, for example: Starting state is C States visited: E, A, B, A, C Inputs given: 47, 33, 27, 99, 64 Not all paths are possible! 33 34 C Programming Bit Manipulation C Programming Control Structures C code to read or write a bit: int readbit(int value, int bit) { return (value >> bit) & 0; // return!!(value >> bit); } void writebit(int *value, int bit) { *value = <<bit; } C conditional and iterative statements if statement if (value == 0x2345678) printf("value matches 0x2345678\n"); for loop for (int i = 0; i < 8; ++i) printf("i = %d\n", i); while loop int j = 6; while (j--) printf("j = %d\n", j); 35 36 9
C Programming Pointers and Arrays C pointers and arrays void foo(int *pointer) { *(pointer+0) = pointer[2] = 0x234; *(pointer+) = pointer[3] = 0x5678; } int main(int argc, char *argv[]) { int array[]= {0,, 2, 3}; foo(array); for (int i = 0; i <= 3; ++i) printf("array[%d] = %x\n", i, array[i]); } 37 gdb Debugger Basic Commands How to debug a program using gdb: $ gdb a.out // debug a program (gdb) break main // set breakpoint on function (gdb) break 23 // set breakpoint in file (gdb) run (gdb) list 20 (gdb) step (gdb) print v (gdb) print *p (gdb) quit // run program // list current file // single step // display value of variable // deference pointer and display // quit debugger Commands can be single letters (b, r, l, s, p, q) 38 Programming Basics Programming Constructs Task Combinational Logic Storage State Machine True Test False Test Subtask condition condition True Subtask Subtask 2 Subtask 2 Subtask Sequential Conditional Iterative False 39 LC-3 Architecture System Architecture 40 0
Instruction Set (First Half) Instruction Set (Second Half) 4 42 Addressing Modes Load -- read data from memory to register LD: PC-relative mode LDR: base+offset mode LDI: indirect mode Store -- write data from register to memory ST: PC-relative mode STR: base+offset mode STI: indirect mode Load pointer: compute address, save in register LEA: immediate mode does not access memory 43 Machine Code to Assembly What is the assembly code for machine instruction 0000000? Step ) Identify opcode: 00 = AND Step 2) Parse entire instruction (use reference) Step 3) Get values from each field OPCODE DR SR imm5 5:2 :9 8:6 5 4:0 00 00 00 0 AND R2 R2-3 Step 4) Translate to mnemonics: AND R2,R2,#-3 44
Assembly to Machine Code What is the machine code for assembly instruction NOT R7,R6? Step ) Identify opcode: NOT = 00 Step 2) Put values into each field: NOT R7 R6 OPCODE DR SR 5:2 :9 8:6 5:0 00 0 Step 3) Build machine instruction: 000 Assembly Code Syntax.ORIG x3000 MAIN AND R0,R0,#0 ; Initialize Sum JSR COMPUTE ; Call function ST R0, SUM ; Store Sum HALT ; Program complete COMPUTE LD R,OPERAND ; Load Operand LD R2,OPERAND2 ; Load Operand2 ADD R0,R,R2 ; Compute Sum RET ; Function return ;; Input data set OPERAND.FILL x234 ; Operand OPERAND2.FILL x432 ; Operand2 SUM.BLKW ; Sum.END 45 46 2