Академический Документы
Профессиональный Документы
Культура Документы
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:
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:
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
Fill out the sign and magnitude row with your group!
15: 0b01111
-16: N/A
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
15: 0b01111
-16: N/A
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
15: 0b01111
-16: 0b10000
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!
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
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.
MIPS to C: