Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS61C Final Review Fall 2005. Overview 1 st Half (Jeremy) –Number Rep (Int/Float) –C (Pointers & Malloc) –MIPS Assembly –Combinational Logic –Audience.

Similar presentations


Presentation on theme: "CS61C Final Review Fall 2005. Overview 1 st Half (Jeremy) –Number Rep (Int/Float) –C (Pointers & Malloc) –MIPS Assembly –Combinational Logic –Audience."— Presentation transcript:

1 CS61C Final Review Fall 2005

2 Overview 1 st Half (Jeremy) –Number Rep (Int/Float) –C (Pointers & Malloc) –MIPS Assembly –Combinational Logic –Audience Questions 5-10m Break 2 nd Half (Zhangxi) –CPU Design (Data Path, Control, Pipeline) –Cache/VM –I/O –Performance –Audience Questions

3 Changing Bases A number in any base can be expanded to calculate its decimal representation: 237 8 = 3F 16 =

4 Changing Bases A number in any base can be expanded to calculate its decimal representation: 237 8 = (2x8 2 ) + (3x8 1 ) + (7x8 0 ) = 2x64 + 3x8 + 7x1 10 = 128 + 24 + 7 10 = 159 10 3F 16 =

5 Changing Bases A number in any base can be expanded to calculate its decimal representation: 237 8 = (2x8 2 ) + (3x8 1 ) + (7x8 0 ) = 2x64 + 3x8 + 7x1 10 = 128 + 24 + 7 10 = 159 10 3F 16 = (3x16 1 ) + (15x16 0 ) = 48 + 15 10 = 63 10

6 Field width An N digit number in base b: –b N possible values How many values in an 16-bit number?

7 Field width An N digit number in base b: b N possible values How many values in an 16-bit number? 2 16 = 64 10 Kbi = 65536 10

8 Numbers in a Computer Unsigned integers - nothing tricky Signed Integers –Sign-magnitude –1’s complement –2’s complement Overflow

9 Signed Number Systems How are the following decimal values represented? DecimalSign Mag.1’s Comp.2’s Comp. 127 2 1 0 -2 -127 -128

10 Signed Number Systems DecimalSign Mag.1’s Comp.2’s Comp. 127 10 01111111 2 10 00000010 1 10 00000001 0 10 -1 10 -2 10 -127 10 -128 10 How are the following decimal values represented?

11 Signed Number Systems DecimalSign Mag.1’s Comp.2’s Comp. 127 10 01111111 2 10 00000010 1 10 00000001 0 10 00000000 or 10000000 00000000 or 11111111 00000000 -1 10 -2 10 -127 10 -128 10 How are the following decimal values represented?

12 Signed Number Systems DecimalSign Mag.1’s Comp.2’s Comp. 127 10 01111111 2 10 00000010 1 10 00000001 0 10 00000000 or 10000000 00000000 or 11111111 00000000 -1 10 100000011111111011111111 -2 10 100000101111110111111110 -127 10 111111111000000010000001 -128 10 How are the following decimal values represented?

13 Signed Number Systems DecimalSign Mag.1’s Comp.2’s Comp. 127 10 01111111 2 10 00000010 1 10 00000001 0 10 00000000 or 10000000 00000000 or 11111111 00000000 -1 10 100000011111111011111111 -2 10 100000101111110111111110 -127 10 111111111000000010000001 -128 10 CAN’T 10000000 How are the following decimal values represented?

14 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: 2 N-1 -1 –Lowest value: -2 N-1

15 IEEE Floating Point Representation Exp (11)Significand (52)SExp (8)Significand (23)S IEEE Single Precision Floating PointIEEE Double Precision Floating Point ExponentSignificandValue 111111110± ∞ 11111111≠ 0NaN Anything Else Anythingnormalized 000000000± zero 00000000≠ 0denormalized Normalized: (-1) S x (1.Significand) x 2 Exp-Bias Denormalized: (-1) S x (0.Significand) x 2 1-Bias

16 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?

17 Pointers in C Pointers –A pointer is a memory address (a number number) where we can look to find data –& gets the address of a variable –* dereferences a pointer int a; int *b = &a; int c = *b;

18 Pointers How would you create this situation in C without using malloc()? a b c d struct Node { int * i; struct Node * next; };

19 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; }

20 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( … ){ … }

21 Pointers void remove_node(node_t **head, int value_to_remove){ }

22 Pointers void remove_node(node_t **head, int value_to_remove){ if (head == NULL) return; }

23 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) { }

24 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) { }

25 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; }

26 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?

27 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 = ; int *i=(int *)malloc(sizeof(int) * len);

28 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 = ; int *i=(int *)malloc(sizeof(int) * len); String of length 16?

29 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 = ; int *i=(int *)malloc(sizeof(int) * len); String of length 16? char *str=(char*)malloc(sizeof(char)*17);

30 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

31 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

32 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

33 MIPS Instruction Formats Two Examples: Branches and Jumps 1.Branches (I-format): How do you determine a branch address given a branch instruction? 2.j and jal (J-format): How do you determine a jump address given a branch instruction? opcode (6)rs (5)rt (5)immediate/offset (16) opcode (6)jump target (26)

34 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) + ( x 4) 2.j and jal (J-format): How do you determine a jump address given a branch instruction? next PC = four leftmost bits of current PC jump target (26)00

35 Boolean Algebra Combinational Logic Truth Tables Sum of Products Algebraic Simplification Programmable Logic Arrays

36 Truth Tables Construct a truth table for a 3 input, 1 output logic function that determines if the majority of the bits are 0. InputOutput 0001 0011 0101 0110 1001 1010 1100 1110

37 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’ InputOutput 0001 0011 0101 0110 1001 1010 1100 1110

38 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(1 + B) = A(1) = A –A + A’B = A + B (A + AB) + A’B = A +(A + A’)B = A + B –(A+B)(A+C) = A + BC 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)’)

39 Finite State Machines FSMs contain a finite number of states, inputs and outputs. Can be represented on with a state transition diagram: state1 state2 Input1/output1 Input1/output2

40 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)

41 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. 00 1110 01 00 01 10 11

42 Finite State Machines Is the output state-determined? Yes Write a truth table for the nextState function curStateInputnextStatecurStateInputnextState 00 100010 0001 100111 0010 00 11 101101 0001110011 01 10110100 011011 1001 110011 10


Download ppt "CS61C Final Review Fall 2005. Overview 1 st Half (Jeremy) –Number Rep (Int/Float) –C (Pointers & Malloc) –MIPS Assembly –Combinational Logic –Audience."

Similar presentations


Ads by Google