Вы находитесь на странице: 1из 25

CS61C Discussion 3

Riyaz Faizullabhoy riyazdf@berkeley.edu OH: Thursday 3-5PM, 330 Soda

Number representation:
Inside computers, everything is a number! We know that computers use binary Quick review: binary to decimal:

Number representation:
Inside computers, everything is a number! We know that computers use binary Quick review: binary to decimal:

0b0101101 = ???

Number representation:
Inside computers, everything is a number! We know that computers use binary Quick review: binary to decimal:

0b0101101 = 0*26 + 1*25 + 0*24 + 1*23 + 1*22 + 0*21 + 1*20 = 45

Number representation:
Inside computers, everything is a number! We know that computers use binary Quick review: binary to decimal:

0b0101101 = 0*26 + 1*25 + 0*24 + 1*23 + 1*22 + 0*21 + 1*20 = 45

Quick review: decimal to binary:

How would we convert 73 to binary?

Number representation:
Inside computers, everything is a number! We know that computers use binary Quick review: binary to decimal:

0b0101101 = 0*26 + 1*25 + 0*24 + 1*23 + 1*22 + 0*21 + 1*20 = 45

Quick review: decimal to binary:

How would we convert 73 to binary? Recursively remove the largest powers of two one at a time! 73 = 64 + 8 + 1

Unsigned:
So far, weve been working with unsigned binary numbers Its impossible to represent a negative number in unsigned binary Fairly easy to understand how large numbers are in comparison to each other by a quick look at the bits

Fill out the unsigned row with your group in the Differences in Representation table!

Unsigned: solutions
0: 0b00000

-1: N/A

15: 0b01111

-16: N/A

MAX: 0b11111 (31)

MIN: 0b00000 (0)

Sign and Magnitude:


To allow for negative numbers, we impose that the leftmost bit is designated as the sign bit: sign bit is set to 1: number is negative sign bit is set to 0: number is positive to negate a number, just flip the sign bit! Cons with this approach: Two zeroes: ex: 00000, and 10000 We lose a bit in representation

Fill out the sign and magnitude row with your group!

Sign and Magnitude: solutions


0: 0b00000 0b10000 -1: 0b10001

15: 0b01111

-16: N/A

MAX: 0b01111 (15)

MIN: 0b11111 (-15)

Ones complement:
Sign and magnitude was nice, but it had a strange increment! So, we introduce ones complement: To negate a number, flip ALL the bits! Ex: given 1 = 0b001, -1 = 0b110 The first bit still serves as a sign bit

Fill out the ones complement row with your group!

Ones complement: solutions


0: 0b00000 0b11111 -1: 0b11110

15: 0b01111

-16: N/A

MAX: 0b01111 (15)

MIN: 0b10000 (-15)

Twos complement:
Ones complement had two zeroes! What if we want just one? And we still want to be able to handle negative numbers too! Now, we introduce twos complement: To negate a number: Invert the number (flip all the bits) Add 1 Ex: negative 0b01011 in twos comp: 0b10100 (inverted) 0b10101 (add 1) When we calculate: -1*1*24 + 1*22 + 1*21

Fill out the twos complement row with your group!

Twos complement: solutions


0: 0b00000 -1: 0b11111

15: 0b01111

-16: 0b10000

MAX: 0b01111 (15)

MIN: 0b10000 (-16) Are the positive and negative numbers balanced?

Changing bases
Formally, for a base B number, we calculate as follows: 1. Value of i-th digit is B x digiti , where i starts at 0 and increases from right to left.
2. We then sum the digits together.

Ternary Example:
2120013 = 2*35 + 1*34 + 2*33 + 0*32 + 0*31 + 1*30 = 62210

Lets try a couple together: 1010 to base 8 1011 10002 to base 16 hint: in hex 10 = A, 11 = B, 12 = C, 13 = D, 14 = E, 15 = F

Administrivia
Proj 1-1 due Sunday My advice: plan out each mapper and reducer! HW 3 due Sunday How was lab?

MIPS
Its just like C, except we break down our program into simpler instructions!

-instead of while/for, we have jumps (j fib) -instead of if/else, we have branches (bne, beq) Lets try the first C to MIPS example! It may be helpful to consider the information above!

MIPS
addiu $s0, $0, 5 addiu $s1, 0, 10 add $t0, $s0, $s0 bne $t0, $s1, else add $s0, $0, $0 j exit else: addiu $s1, $s0, -1 exit: #done

MIPS: Recursion
We have conventions so we dont clobber register values! $ra, $a0, $s1, To make sure we keep them around for when we return, we allocate stack space with $sp by decrementing. The stack grows downward!

MIPS: Recursion: a skeleton


func: addi $sp,$sp,-8 # make space on the stack sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save other vars (args) # branch to recurse? # if we dont recurse: # do operations for base case addi $sp, $sp,8 # restore stack pointer jr $ra are varying styles and skeletons for NOTE: There # return to caller implementing recursion in MIPS! recursion: # do operations to prepare # for next recursion jal func # jump and link if recursing # we will return here upon jr $ra lw $a0, 0($sp) lw $ra, 4($sp) # retrieve saved vars # do stuff with saved vars

addi $sp, $sp,8 # restore stack pointer This is just one of them! Use it as a tool, but it is not a jr $ra # return to caller comprehensive guide to all forms of MIPS recursion

MIPS: Recursion: a skeleton


func: addi $sp,$sp,-8 # make space on the stack sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save other vars (args) # branch to recurse? # if we dont recurse: # do operations for base case addi $sp, $sp,8 # restore stack pointer jr $ra are varying styles and skeletons for NOTE: There # return to caller implementing recursion in MIPS! recursion: # do operations to prepare # for next recursion jal func # jump and link if recursing # we will return here upon jr $ra lw $a0, 0($sp) lw $ra, 4($sp) # retrieve saved vars # do stuff with saved vars

addi $sp, $sp,8 # restore stack pointer With this in mind, This is just one of them! Use it as a tool, but it is not a jr $ra lets try the MIPS recursion problem! # return to caller comprehensive guide to all forms of MIPS recursion
Hint: think about factorial, and the skeleton!

MIPS: Recursion:
sum: addi $sp, $sp, -8 # allocate space on stack sw $ra, 0($sp) # store the return address sw $a0, 4($sp) # store the argument slti $t0,$a0,1 # check if n>0 beq $t0, $0, recurse # n > 0 case add $v0, $0, $0 # start return value to 0 addi $sp, $sp, 8 # pop 2 items off stack jr $ra # return to caller recurse: addi $a0, $a0, -1 # calculate n-1 jal sum # recursively call sum(n-1) lw $ra, 0($sp) # restore saved return address lw $a0, 4($sp) # restore saved argument addi $sp, $sp, 8 # pop 2 items off stack remember: $v0 is our return value! add $v0, $a0, $v0 # calculate n + sum(n-1) jr $ra # return to caller

MIPS to C:
Feel free to make your own variables in C! Also, try to relate MIPS-exclusive instructions (like j) to C code.

For when youre done: what does this code do?

MIPS to C:

int a = 0; int b = 1; int c = 30; while (a != c) { b = b << 1; a++; }

What does this code do? > Calculates 2^30 in variable b.

Topics for next time!


Please email me at if there are any topics youd like to see in discussion! riyazdf@berkeley.edu

Вам также может понравиться