Presentation is loading. Please wait.

Presentation is loading. Please wait.

CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics.

Similar presentations


Presentation on theme: "CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics."— Presentation transcript:

1 CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics Arizona State University Slides courtesy: Prof. Yann Hang Lee, ASU, Prof. Mary Jane Irwin, PSU, Ande Carle, UCB

2 CML CMLAnnouncements Quiz 2 –Complete Chapter 2 MIPS Assembly Language Programming, including function calls –Thursday, Sept 24, 2009 Project 2 –MIPS Assembly Language Programming, including function calls –Will take a day or two to program –Due tomorrow

3 CML CML CSE 230 Road map So far –Write any program in MIPS assembly language –Convert into binary instructions Today –How are numbers represented –How are they added, subtracted, multiplied divided

4 CML Numbers Bits are just bits (no inherent meaning) –conventions define relationship between bits and numbers Unsigned Numbers –0 - 2 31 Large Numbers –Avogadro's number 6.0221415 × 10 23 ~ 64 bits Signed Numbers –How to represent –ve numbers Fractions –How to represent

5 CML CML Unsigned Numbers Representation (d 31 d 30 … d 2 d 1 d 0 ) 2 = d 31 *2 31 + d 30 *2 30 + … d 2 *2 2 + d 1 * 2 1 + d 0 *2 0 0000 0000 0000 0000 0000 0000 0000 0000 = 0 10 0000 0000 0000 0000 0000 0000 0000 0001 = 1 10 0000 0000 0000 0000 0000 0000 0000 0010 = 2 10.......... 1111 1111 1111 1111 1111 1111 1111 1111 = 4,294,967,295 10 Minimum Number = 0 = (0000….0000) 2 Maximum Number = (1111….1111) 2 = (1 0000….0000) 2 – 1 = 2 32 -1 32

6 CML CML Addition of Unsigned Numbers Addition 0 0 1 1 3 + 0 0 1 0 +2 --------------------- 0 1 0 1 5 What happens when –Addition results in a number that does not fit in 32-bits 2 32 -1 + 2 32 -1 = 2*2 32 – 2 = 2 33 -2 -- needs 33 bits –Overflow

7 CML CML Subtraction of Unsigned Numbers Subtraction 0 0 1 1 3 - 0 0 1 0 -2 --------------------- 0 0 0 1 1 What happens when –You subtract greater number from smaller number Need –ve numbers

8 CML CML Signed Numbers One simple strategy –Sign Magnitude Representation Leftmost bit is sign bit Rest 31-bits are unsigned –Representation (d 31 d 30 … d 2 d 1 d 0 ) 2 = (-1)*d 31 + d 30 *2 30 + … d 2 *2 2 + d 1 * 2 1 + d 0 *2 0 –Number Range = -(2 31 -1), …,-1,-0, +0,+1, …, +(2 31 -1) 2 zero’s –How to find –ve of a number Just change the sign-bit –Addition & Subtraction Add/Sub the 31-bits, and change the sign bit logically Need a seamless way to perform these very frequent operations 31-bit magnitude 1-bit sign

9 CML CML 2s Complement Representation Representation (d 31 d 30 … d 2 d 1 d 0 ) 2 = d 31 *(-2) 31 + d 30 *2 30 + … d 2 *2 2 + d 1 * 2 1 + d 0 *2 0 Examples 0000 0000 0000 0000 0000 0000 0000 0000 two = 0 ten 0000 0000 0000 0000 0000 0000 0000 0001 two = + 1 ten 0000 0000 0000 0000 0000 0000 0000 0010 two = + 2 ten... 0111 1111 1111 1111 1111 1111 1111 1110 two = + 2,147,483,646 ten 0111 1111 1111 1111 1111 1111 1111 1111 two = + 2,147,483,647 ten 1000 0000 0000 0000 0000 0000 0000 0000 two = – 2,147,483,648 ten 1000 0000 0000 0000 0000 0000 0000 0001 two = – 2,147,483,647 ten 1000 0000 0000 0000 0000 0000 0000 0010 two = – 2,147,483,646 ten... 1111 1111 1111 1111 1111 1111 1111 1101 two = – 3 ten 1111 1111 1111 1111 1111 1111 1111 1110 two = – 2 ten 1111 1111 1111 1111 1111 1111 1111 1111 two = – 1 ten

10 CML CML Negate 2's Complement Number Negating a two's complement number: invert all bits and add 1 –remember: “negate” and “invert” are quite different! 0000 0000 0000 0000 0000 0000 0000 0010 two = + 2 ten 1111 1111 1111 1111 1111 1111 1111 1110 two = – 2 ten Converting n bit numbers into numbers with more than n bits: –MIPS 16 bit immediate gets converted to 32 bits for arithmetic "sign extension" –copy the most significant bit (the sign bit) into the other bits 0010 -> 0000 0010 1010 -> 1111 1010

11 CML CML Add 2’s Complement Numbers Just like unsigned numbers 7 + 6 = 13 -4 + -5 = -9 0111 0110+ 1101 11 1100 1 011+ 0111 1 0 0 0 1 1 1

12 CML CML Subtract 2’s Complement Numbers A – B = A + (-B) = A + (!B + 1) Just like unsigned numbers 6 – 7 = 6 + (~7 + 1) = -1 -3 – 5 = -3 + (~(5)+1) = -8 1101 0101- 1000 0110 0111- 1111 0 0 1 0110 1001+ 1111 0 1 1 1 0 1 1101 1011+ 1000 1 1 1

13 CML CMLOverflow When result of operation too large to fit in 32-bits 7 – (-13) = 20 Detect Overflows –Positive + Positive -> Negative –Negative + Negative -> Positive –Positive – Negative -> Negative –Negative – Positive -> Positive Consider the operations A + B, and A – B –Can overflow occur if B is 0 ? –Can overflow occur if A is 0 ? 0111 0011- 0 1 0111 1101+ 0100 0 0 1

14 CML CML Effects of Overflow An exception (interrupt) occurs –Control jumps to predefined address for exception –Interrupted address is saved for possible resumption Details based on software system / language –example: flight control vs. homework assignment Don't always want to detect overflow –new MIPS instructions: addu, addiu, subu note: addiu still sign-extends! note: sltu, sltiu for unsigned comparisons

15 CML CML Yoda says… Luke: I can’t believe it. Yoda: That is why you fail


Download ppt "CML CML CS 230: Computer Organization and Assembly Language Aviral Shrivastava Department of Computer Science and Engineering School of Computing and Informatics."

Similar presentations


Ads by Google