42
CS61C Final Review Fall 2005

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

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

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 Sign Mag. 1’s Comp. 2’s Comp.

127

2

1

0

-1

-2

-127

-128

Signed Number Systems

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

How are the following decimal values represented?

Signed Number Systems

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 11111111

00000000

-110

-210

-12710

-12810

How are the following decimal values represented?

Signed Number Systems

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 11111111

00000000

-110 10000001 11111110 11111111

-210 10000010 11111101 11111110

-12710 11111111 10000000 10000001

-12810

How are the following decimal values represented?

Signed Number Systems

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 11111111

00000000

-110 10000001 11111110 11111111

-210 10000010 11111101 11111110

-12710 11111111 10000000 10000001

-12810 CAN’T CAN’T 10000000

How are the following decimal values represented?

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

Exp (11) Significand (52)SExp (8) Significand (23)S

IEEE Single Precision Floating Point IEEE Double Precision Floating Point

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 pointerint a;

int *b = &a;

int c = *b;

Pointers

How would you create this situation in C without using malloc()?

a b

c

d

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

}

Pointersvoid remove_node(node_t **head, int value_to_remove){

}

Pointersvoid remove_node(node_t **head, int value_to_remove){

if (head == NULL)return;

}

Pointersvoid 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) {

}}

Pointersvoid 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) {

}}

}

Pointersvoid 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 Callsmy_function:

# Prologueaddi $sp, $sp, -8sw $ra, 0($sp) sw $s0, 4($sp)

# Body – Here’s where you do the meat of the# procedure

# Epiloguemy_fun_return:

lw $ra, 0($sp)lw $s0, 4($sp)add $sp, $sp, 8jr $ra

MIPS Instruction Formats

Two Examples: Branches and Jumps1. 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)

MIPS Instruction Formats

Two Examples: Branches and Jumps1. 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 =

four leftmost bits of current PC

jump target (26) 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 Output

000 1

001 1

010 1

011 0

100 1

101 0

110 0

111 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 Output

000 1

001 1

010 1

011 0

100 1

101 0

110 0

111 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(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)’)

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

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.

00

11 10

01

00 00

00 00

01

01 01

01

1010

10

1011

11

11

11

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