DOC PREVIEW
Berkeley COMPSCI 61C - Lecture 7

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS61C Negative Numbers and Logical Operations Lecture 7Review 1/2Review 2/2Numbers: ReviewOverviewWhat if too big?How avoid overflow, allow it sometimes?What if Overflow Detected?How Represent Negative Numbers?Search for Negative Number RepresentationTwo’s ComplementTwo’s Complement Formula, ExampleOverflow for Two’s Complement Numbers?Signed v. Unsigned ComparisonsExample: Signed v. Unsigned ComparisonsAdministrivia“What’s This Stuff Good For?”Two’s complement shortcut: NegationTwo’s complement shortcut: Sign extensionMore Compact Representation v. Binary?Logical OperationsShift InstructionsExtracting a field of bitsAnd instructionOr instructionInserting a field of bits (almost OK)Sign Extension of ImmediatesSummary: 12 new instructions (with formats)Example: show C, assembly, machineSlide 30“And in Conclusion...” 1/1cs 61C L7 Number.1Patterson Spring 99 ©UCBCS61CNegative Numbers and Logical Operations Lecture 7February 10, 1999Dave Patterson (http.cs.berkeley.edu/~patterson)www-inst.eecs.berkeley.edu/~cs61c/schedule.htmlcs 61C L7 Number.2Patterson Spring 99 ©UCBReview 1/2°MIPS assembly language instructions mapped to numbers in 3 formats•Op field determines format °Binary  Decimal  Assembly  Symbolic Assembly  C•Reverse Engineering or Disassembly•Its hard to do, therefore people like shipping binary machine language more than assembly or C6 bits 5 bits 5 bits 5 bits 5 bits 6 bitsop rs rt rd functshamtop rs rt immediateop addressRIJcs 61C L7 Number.3Patterson Spring 99 ©UCBReview 2/2°Programming language model of memory allocation and pointers•Allocate in stack vs. heap vs. global areas•Arguments passed call by value vs. call by reference•Pointer in C is HLL version of machine addresscs 61C L7 Number.4Patterson Spring 99 ©UCBNumbers: Review°Number Base B  B symbols per digit:•Base 10 (Decimal): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9Base 2 (Binary): 0, 1°Number representation: d31d30 ... d2d1d0•d31 x B31+ d30 x B30 + ... + d2 x B2 + d1 x B1 + d0 x B0•One billion (1,000,000,000ten ) is0011 1011 1001 1010 1100 1010 0000 0000two 228 224 220 216 212 28 24 20= 1x229+1x228+1x227+1x225+1x224+1x223+1x220 +1x219+1x217+1x215 +1x214 +1x211 +1x29 = 536,870,912 + 268,435,456 + 134,217,728+ 33,554,432 + 16,777,216 + 8,388,608 + 1,048,576+ 524,288+ 131,072+ 32,768+ 16,384+ 2,048+ 512cs 61C L7 Number.5Patterson Spring 99 ©UCBOverview°What if Numbers too Big?°How Represent Negative Numbers?°What if Result doesn’t fit in register?°More Compact Notation than Binary?°Administrivia,“What’s this stuff good for”°Shift Instructions°And/Or Instructions°Conclusioncs 61C L7 Number.6Patterson Spring 99 ©UCBWhat if too big?°Binary bit patterns above are simply representatives of numbers °Numbers really have an infinite number of digits•with almost all being zero except for a few of the rightmost digits •Just don’t normally show leading zeros°If result of add (or any other arithmetic operation), cannot be represented by these rightmost hardware bits, overflow is said to have occurred°Up to Compiler and OS what to docs 61C L7 Number.7Patterson Spring 99 ©UCBHow avoid overflow, allow it sometimes?°Some languages detect overflow (Ada), some don’t (C)°MIPS solution is 2 kinds of arithmetic instructions to recognize 2 choices:•add (add), add immediate (addi), and subtract (sub) cause exceptions on overflow•add unsigned (addu), add immediate unsigned (addiu), and subtract unsigned (subu) do not cause exceptions on overflow°Compiler selects appropriate arithmetic•MIPS C compilers produce addu, addiu, subucs 61C L7 Number.8Patterson Spring 99 ©UCBWhat if Overflow Detected?°An “exception” (or “interrupt”) occurs•Address of the instruction that overflowed is saved in a register•Computer jumps to predefined address to invoke appropriate routine for that exception•Like an unplanned hardware function call°Operating system decides what to do•In some situations program continues after corrective code is executed°MIPS support: exception program counter (EPC) contains address of that instruction•move from system control (mfc0) to copy EPCcs 61C L7 Number.9Patterson Spring 99 ©UCBHow Represent Negative Numbers?°Obvious solution: add a separate sign! •sign represented in a single bit! •representation called sign and magnitude°Shortcomings of sign and magnitude•Where to put the sign bit: right? left?•Separate sign bit means it has both a positive and negative zero, lead to problems for inattentive programmers: +0 = -0?•Adder may need extra step size don’t know sign in advance°Thus sign and magnitude was abandonedcs 61C L7 Number.10Patterson Spring 99 ©UCBSearch for Negative Number Representation°Obvious solution didn’t work, find another°What is result for unsigned numbers if tried to subtract large number from a small one?•Would try to borrow from string of leading 0s, so result would have a string of leading 1s•With no obvious better alternative, pick representation that made the hardware simple: leading 0s  positive, leading 1s  negative•000000...xxx is >=0, 111111...xxx is < 0°This representation called two’s complementcs 61C L7 Number.11Patterson Spring 99 ©UCBTwo’s Complement 0000 ... 0000 0000 0000 0000two = 0ten0000 ... 0000 0000 0000 0001two = 1ten0000 ... 0000 0000 0000 0010two = 2ten. . .0111 ... 1111 1111 1111 1101two = 2,147,483,645ten0111 ... 1111 1111 1111 1110two = 2,147,483,646ten0111 ... 1111 1111 1111 1111two = 2,147,483,647ten1000 ... 0000 0000 0000 0000two = –2,147,483,648ten1000 ... 0000 0000 0000 0001two = –2,147,483,647ten1000 ... 0000 0000 0000 0010two = –2,147,483,646ten. . . 1111 ... 1111 1111 1111 1101two = –3ten1111 ... 1111 1111 1111 1110two = –2ten1111 ... 1111 1111 1111 1111two = –1ten°One zero, 1st bit => >=0 or <0, called sign bit •but one negative with no positive –2,147,483,648tencs 61C L7 Number.12Patterson Spring 99 ©UCBTwo’s Complement Formula, Example°Recognizing role of sign bit, can represent positive and negative numbers in terms of the bit value times a power of 2:•d31 x -231+ d30 x 230 + ... + d2 x 22 + d1 x 21 + d0 x 20°Example1111 1111 1111 1111 1111 1111 1111 1100two= 1x-231 +1x230 +1x229+... +1x22+0x21+0x20= -231 + 230 + 229 + ... + 22 + 0 + 0 = -2,147,483,648ten +


View Full Document

Berkeley COMPSCI 61C - Lecture 7

Documents in this Course
SIMD II

SIMD II

8 pages

Midterm

Midterm

7 pages

Caches

Caches

7 pages

Lecture 9

Lecture 9

24 pages

Lecture 1

Lecture 1

28 pages

Lecture 2

Lecture 2

25 pages

VM II

VM II

4 pages

Midterm

Midterm

10 pages

Load more
Download Lecture 7
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?