CS61C Final Review Fall 2005 Overview • 1st Half (Jeremy) – – – – – Number Rep (Int/Float) C (Pointers & Malloc) MIPS Assembly Combinational Logic Audience Questions • 5-10m Break • 2nd Half (Zhangxi) – – – – – CPU Design (Data Path, Control, Pipeline) Cache/VM I/O Performance Audience Questions Changing Bases • A number in any base can be expanded to calculate its decimal representation: 2378 = 3F16 = Changing Bases • A number in any base can be expanded to calculate its decimal representation: 2378 = (2x82) + (3x81) + (7x80) = 2x64 + 3x8 + 7x110 = 128 + 24 + 710 = 15910 3F16 = Changing Bases • A number in any base can be expanded to calculate its decimal representation: 2378 = (2x82) + (3x81) + (7x80) = 2x64 + 3x8 + 7x110 = 128 + 24 + 710 = 15910 3F16 = (3x161) + (15x160) = 48 + 1510 = 6310 Field width • An N digit number in base b: – bN possible values • How many values in an 16-bit number? Field width • An N digit number in base b: bN possible values • How many values in an 16-bit number? 216 = 64 10 Kbi = 65536 10 Numbers in a Computer • Unsigned integers - nothing tricky • Signed Integers – Sign-magnitude – 1’s complement – 2’s complement • Overflow Signed Number Systems How are the following decimal values represented? Decimal 127 2 1 0 -1 -2 -127 -128 Sign Mag. 1’s Comp. 2’s Comp. Signed Number Systems How are the following decimal values represented? Decimal Sign Mag. 1’s Comp. 2’s Comp. 12710 01111111 01111111 01111111 210 00000010 00000010 00000010 110 00000001 00000001 00000001 010 -110 -210 -12710 -12810 Signed Number Systems How are the following decimal values represented? Decimal Sign Mag. 1’s Comp. 2’s Comp. 12710 01111111 01111111 01111111 210 00000010 00000010 00000010 110 00000001 00000001 00000001 010 00000000 or 00000000 or 00000000 10000000 11111111 -110 -210 -12710 -12810 Signed Number Systems How are the following decimal values represented? Decimal Sign Mag. 1’s Comp. 2’s Comp. 12710 01111111 01111111 01111111 210 00000010 00000010 00000010 110 00000001 00000001 00000001 010 00000000 or 10000000 00000000 or 00000000 11111111 -110 10000001 11111110 11111111 -210 10000010 11111101 11111110 -12710 11111111 10000000 10000001 -12810 Signed Number Systems How are the following decimal values represented? Decimal Sign Mag. 1’s Comp. 2’s Comp. 12710 01111111 01111111 01111111 210 00000010 00000010 00000010 110 00000001 00000001 00000001 010 00000000 or 10000000 00000000 or 00000000 11111111 -110 10000001 11111110 11111111 -210 10000010 11111101 11111110 -12710 11111111 10000000 10000001 -12810 CAN’T CAN’T 10000000 Two’s complement • Benefits over SM and 1’s: – Only one 0 – Numbers go in simple unsigned order with discontinuity only at 0 • Extremes: – Highest value: 2N-1-1 – Lowest value: -2N-1 IEEE Floating Point Representation IEEE Single Precision Floating Point S Exp (8) Significand (23) IEEE Double Precision Floating Point S Exp (11) Significand (52) Exponent Significand Value 11111111 0 ±∞ 11111111 ≠0 NaN Anything Else Anything normalized 00000000 0 ± zero 00000000 ≠0 denormalized Normalized: (-1)S x (1.Significand) x 2Exp-Bias Denormalized: (-1)S x (0.Significand) x 21-Bias Floating Point Thought Questions For IEEE Single/Double Precision Floating Point Rep: • How many numbers can you represent? • What is the smallest/largest positive numbers you can represent? • How do you compare two floating point numbers? • How do you add/subtract two floating point numbers? Pointers in C • Pointers – A pointer is a memory address (a number number) where we can look to find data – &<variable> gets the address of a variable – *<pointer> dereferences a pointer int a; int *b = &a; int c = *b; Pointers How would you create this situation in C without using malloc()? a b d c struct Node { int * i; struct Node * next; }; Pointers struct Node { int * i; struct Node * next; }; int main() { struct Node a, b, c[5], d; a.next = &b; b.next = c; c[0].next = &d; return 0; } Pointers How would you remove an element from a linked list given its value? struct node { int * data; struct node * next; }; typedef struct node node_t; void remove_node( … ){ … } Pointers void remove_node(node_t **head, int value_to_remove){ } Pointers void remove_node(node_t **head, int value_to_remove){ if (head == NULL) return; } Pointers void remove_node(node_t **head, int value_to_remove){ node_t *it, *prev; if (head == NULL) return; for(prev=NULL, it = *head; it != NULL; prev = it, it = it->next) { } } Pointers void remove_node(node_t **head, int value_to_remove){ node_t *it, *prev; if (head == NULL) return; for(prev=NULL, it = *head; it != NULL; prev = it, it = it->next) { if(it->data == value_to_remove) { } } } Pointers void remove_node(node_t **head, int value_to_remove){ node_t *it, *prev; if (head == NULL) return; for(prev=NULL, it = *head; it != NULL; prev = it, it = it->next) { if(it->data == value_to_remove) { if(it == *head) { *head = it->next; } else { prev->next = it->next; } free(it); return; } } } Malloc • Allocates memory on the heap • Data is persistent after the calling function is removed from stack • How do you allocate an array of integers whose size is determined at runtime? Malloc • Allocates memory on the heap • Data is persistent after the calling function is removed from stack • How do you allocate an array of integers whose size is determined at runtime? int len = <some value from user>; int *i=(int *)malloc(sizeof(int) * len); Malloc • Allocates memory on the heap • Data is persistent after the calling function is removed from stack • How do you allocate an array of integers whose size is determined at runtime? int len = <some value from user>; int *i=(int *)malloc(sizeof(int) * len); • String of length 16? Malloc • Allocates memory on the heap • Data is persistent after the calling function is removed from stack • How do you allocate an array of integers whose size is determined at runtime? int len = <some value from user>; int *i=(int *)malloc(sizeof(int) * len); • String of length 16? char *str=(char*)malloc(sizeof(char)*17); C Strings • C Strings are byte arrays which are null terminated. • In ASCII, each character in the string is represented by one byte in the array. The numeric value of the byte determines the character. – Not true with unicode • Comparing strings: – Right: if(strcmp(str1, str2) != 0) … – Wrong: If( str1 == str2 ) … • Copying Strings: – Right: dst = strdup(src) /* Or malloc followed by strcpy */ – Wrong: dst = src MIPS Assembly • Procedure call convention: – Arguments in $a0,$a1… – Return values in $v0, $v1… – Saved Registers ($s0, $s1, …, $sp) • preserved over function calls: • Callee must save these if it uses them • Caller must save all other registers because they can be trashed by the callee MIPS Procedure Calls my_function: # Prologue addi $sp, $sp, -8 sw $ra, 0($sp) sw $s0, 4($sp) # Body – Here’s where you do the meat of the # procedure # Epilogue my_fun_return: lw $ra, 0($sp) lw $s0, 4($sp) add $sp, $sp, 8 jr $ra MIPS Instruction Formats Two Examples: Branches and Jumps 1. Branches (I-format): How do you determine a branch address given a branch instruction? opcode (6) 2. rs (5) rt (5) immediate/offset (16) j and jal (J-format): How do you determine a jump address given a branch instruction? opcode (6) jump target (26) MIPS Instruction Formats Two Examples: Branches and Jumps 1. Branches (I-format): How do you determine a branch address given a branch instruction? next PC = (PC + 4) + (<signed immediate> x 4) 2. j and jal (J-format): How do you determine a jump address given a branch instruction? next PC = jump target (26) four leftmost bits of current PC 00 Boolean Algebra • • • • • Combinational Logic Truth Tables Sum of Products Algebraic Simplification Programmable Logic Arrays Truth Tables • Construct a truth table for a 3 input, 1 output logic function that determines if the majority of the bits are 0. Input 000 001 010 011 100 101 110 111 Output 1 1 1 0 1 0 0 0 Sum of Products • To find the sum of products, you AND together the bits of each line that has 1 on the output and then OR the terms together. • Find the sum of products for the previous function: S = A’B’C’ + A’B’C + A’BC’ + AB’C’ Input 000 001 010 011 100 101 110 111 Output 1 1 1 0 1 0 0 0 Simplify using Boolean Algebra • • And = Multiplication, Or = Addition Simple Boolean Identities: – – – – – • (A + B) + C = A + (B + C) (Associative Addition) (AB)C = A(BC) (Associative Multiplication) A + B = B + A (Commutative Addition) AB = BA (Commutative Multiplication) A(B + C) = AB + AC (Distributive) More Complex Identities: – A + AB = A • – A + A’B = A + B • – (A + AB) + A’B = A +(A + A’)B = A + B (A+B)(A+C) = A + BC • • A(1 + B) = A(1) = A AA + AC + AB + BC = A(A + B + C) + BC = A + BC Our “majority are zeros” expression becomes: – – – – – – S = A’B’C’ + A’B’C + A’BC’ + AB’C’ (Initial) S = A’B’(C’ + C) + (A’B + AB’)C’ (Distributive) S = A’B’ + (A’B + AB’)C’ (A + A’ = 1) S = A’B’(1 + C’) + (A’B + AB’)C’ (A + 1 = 1) S = A’B’ + (A’B’ + A’B + AB’)C’(Dist, Commutative Addition, Dist) S = A’B’ + (AB)’C’ (A’B’ + A’B + AB’ = (AB)’) Finite State Machines • FSMs contain a finite number of states, inputs and outputs. • Can be represented on with a state transition diagram: Input1/output1 state1 state2 Input1/output2 Finite State Machines • Outputs: – State determined: output(currentState); outputs can be marked on states (Moore Machine) – State and input determined: output(currentState, input); outputs marked on transition arcs (Mealey Machine) Finite State Machine • Construct a state transition diagram for a 2 bit accumulator that takes a 2 bit input. It will wrap back around on overflow. 01 00 11 00 10 01 01 10 10 11 11 00 00 10 11 01 11 01 10 00 Finite State Machines • Is the output state-determined? Yes • Write a truth table for the nextState function curState Input nextState curState Input nextState 00 00 00 10 00 10 00 01 01 10 01 11 00 10 10 10 10 00 00 11 11 10 11 01 01 00 01 11 00 11 01 01 10 11 01 00 01 10 11 11 10 01 01 11 00 11 11 10