CS61CL Machine Structures Lec 6 – Number Representation David Culler Electrical Engineering and Computer Sciences University of California, Berkeley.

Slides:



Advertisements
Similar presentations
CS/COE0447 Computer Organization & Assembly Language
Advertisements

Chapter Three.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
Computer Structure - Computer Arithmetic Goal: Representing Numbers in Binary  Base 10 (decimal) - Numbers are represented using 10 numerals: 0, 1, 2,
10/28/091 Implementing an Instruction Set David E. Culler CS61CL Oct 28, 2009 Lecture 9 UCB CS61CL F09 Lec 9.
Comp Sci instruction encoding 1 Instruction Encoding MIPS machine language Binary encoding of instructions MIPS instruction = 32 bits Three instruction.
CPE 731 Advanced Computer Architecture Instruction Set Principles Dr. Gheith Abandah Adapted from the slides of Prof. David Patterson, University of California,
EECS Components and Design Techniques for Digital Systems Lec 17 – Addition, Subtraction, and Negative Numbers David Culler Electrical Engineering.
1 Representing Numbers Using Bases Numbers in base 10 are called decimal numbers, they are composed of 10 numerals ( ספרות ) = 9* * *10.
MIPS Assembler Programming
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 6: Logic/Shift Instructions Partially adapted from Computer Organization and Design, 4.
MIPS on FLEET Amir Kamil. Goals Run most MIPS assembly code on FLEET  Attempt to duplicate level of support in SPIM interpreter  MIPS assembly translated.
1  1998 Morgan Kaufmann Publishers Chapter Four Arithmetic for Computers.
Chapter 3 Arithmetic for Computers. Arithmetic Where we've been: Abstractions: Instruction Set Architecture Assembly Language and Machine Language What's.
Arithmetic for Computers
EECS Components and Design Techniques for Digital Systems Lec 15 – Addition, Subtraction, and Negative Numbers David Culler Electrical Engineering.
1 Bits are just bits (no inherent meaning) — conventions define relationship between bits and numbers Binary numbers (base 2)
CMPE 325 Computer Architecture II Cem Ergün Eastern Mediterranean University Integer Representation and the ALU.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 3.
IT253: Computer Organization Lecture 5: Assembly Language and an Introduction to MIPS Tonga Institute of Higher Education.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
CS61CL Machine Structures Lec 5 – Instruction Set Architecture
Chapter 2 CSF 2009 The MIPS Assembly Language. Stored Program Computers Instructions represented in binary, just like data Instructions and data stored.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
EI 209 Chapter 3.1CSE, 2015 EI 209 Computer Organization Fall 2015 Chapter 3: Arithmetic for Computers Haojin Zhu ( )
EEL5708/Bölöni Lec 3.1 Fall 2006 Sept 1, 2006 Lotzi Bölöni EEL 5708 High Performance Computer Architecture Lecture 3 Review: Instruction Sets.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 9 Binary Representation and Logical Operations.
1 Signed Arithmetic Logical Operations Ellen Spertus MCS 111 October 1, 2002.
CMPUT Computer Organization and Architecture I1 CMPUT229 - Fall 2003 Topic6: Logic, Multiply and Divide Operations José Nelson Amaral.
CDA 3101 Spring 2016 Introduction to Computer Organization
MIPS Instruction Set Architecture Prof. Sirer CS 316 Cornell University.
COM181 Computer Hardware Lecture 6: The MIPs CPU.
Computer Arthmetic Chapter Four P&H. Data Representation Why do we not encode numbers as strings of ASCII digits inside computers? What is overflow when.
EEL5708/Bölöni Lec 3.1 Fall 2004 Sept 1, 2004 Lotzi Bölöni Fall 2004 EEL 5708 High Performance Computer Architecture Lecture 3 Review: Instruction Sets.
9/23/2004Comp 120 Fall September Chapter 4 – Arithmetic and its implementation Assignments 5,6 and 7 posted to the class web page.
Pirouz Bazargan SabetDecember 2003 Effective Implementation of a 32-bit RISC Processor Pirouz Bazargan Sabet University of Paris 6 - LIP6 - ASIM
May 2, 2001System Architecture I1 Systems Architecture I (CS ) Lecture 11: Arithmetic for Computers * Jeremy R. Johnson May 2, 2001 *This lecture.
MIPS Arithmetic and Logic Instructions
CS Computer Organization Numbers and Instructions Dr. Stephen P. Carl.
COMPUTER ARCHITECTURE & OPERATIONS I Instructor: Yaohang Li.
Based on slides from D. Patterson and www-inst.eecs.berkeley.edu/~cs152/ COM 249 – Computer Organization and Assembly Language Chapter 3 Arithmetic For.
Instructor: Prof. Hany H Ammar, LCSEE, WVU
Computer Arthmetic Chapter Four P&H.
CS 230: Computer Organization and Assembly Language
Computer Organization and Design Instruction Sets - 2
Morgan Kaufmann Publishers
Computer Organization and Design Instruction Sets - 2
32-bit MIPS ISA.
ENGR 3410 – Computer Architecture Mark L. Chang Fall 2006
Computer Organization and Design Instruction Sets
Appendix A Classifying Instruction Set Architecture
MPIS Instructions Functionalities of instructions Instruction format
How to represent signed integers
Computer Architecture & Operations I
ECE232: Hardware Organization and Design
Instruction encoding The ISA defines Format = Encoding
CS/COE0447 Computer Organization & Assembly Language
CS/COE0447 Computer Organization & Assembly Language
/ Computer Architecture and Design
A 1-Bit Arithmetic Logic Unit
Basic Building Blocks Multiplexer Demultiplexer Adder +
Basic Building Blocks Multiplexer Demultiplexer Adder +
MIPS Instruction Set Architecture
MIPS Assembly.
CS352H Computer Systems Architecture
Reduced Instruction Set Computer (RISC)
COMS 361 Computer Organization
MIPS Arithmetic and Logic Instructions
MIPS Arithmetic and Logic Instructions
Presentation transcript:

CS61CL Machine Structures Lec 6 – Number Representation David Culler Electrical Engineering and Computer Sciences University of California, Berkeley

6/23/2015 cs61cl f09 lec 6 2 Review: MIPS R r0 r1 ° r31 PC lo hi Programmable storage 2^32 x bytes 31 x 32-bit GPRs (R0=0) 32 x 32-bit FP regs (paired DP) HI, LO, PC Data types ? Format ? Addressing Modes? Arithmetic logical Add, AddU, Sub, SubU, And, Or, Xor, Nor, SLT, SLTU, AddI, AddIU, SLTI, SLTIU, AndI, OrI, XorI, LUI SLL, SRL, SRA, SLLV, SRLV, SRAV Memory Access LB, LBU, LH, LHU, LW, LWL,LWR SB, SH, SW, SWL, SWR Control J, JAL, JR, JALR BEq, BNE, BLEZ,BGTZ,BLTZ,BGEZ,BLTZAL,BGEZAL 32-bit instructions on word boundary

MIPS Instruction Format Reg-Reg instructions (op == 0) –add, sub, and, or, nor, xor, sltR[rd] := R[rs] funct R[rt]; pc:=pc+4 –sll. srl, sraR[rd] := R[rt] shft shamt Reg-Immed (op != 0) –addi, andi, ori, xori, lui,R[rt] := R[rs] op Im16 –addiu, slti, sltiu –lw, lh, lhu, lb, lbuR[rt] := Mem[ R[ rs ] + signEx(Im16) ]* –sw, sh, sbMem[ R[ rs ] + signEx(Im16) ] := R[rt] 6/23/2015 cs61cl f09 lec 6 3 op 6 rs 5 rt 5 rd 5 shamt 5 funct 6 immediate 16 op 6 rs 5 rt 5 R: Reg-Reg I: Reg-Imed immediate 26 op 6 J: Jump

6/23/2015 cs61cl f09 lec 6 Computer Number Systems We all take positional notation for granted –D k-1 D k-2 …D 0 represents D k-1 B k-1 + D k-2 B k-2 + …+ D 0 B 0 where B  { 0, …, B-1 } We all understand how to compare, add, subtract these numbers –Add each position, write down the position bit and possibly carry to the next position Computers represent finite number systems –Generally radix 2 –B k-1 B k-2 …B 0 represents B k-1 2 k-1 + B -2 2 k-2 + …+ B where B  { 0,1 } 4

6/23/2015 cs61cl f09 lec 6 Unsigned Numbers - Addition Example: = 5 Unsigned binary addition Is just addition, base 2 Add the bits in each position and carry

6/23/2015 cs61cl f09 lec 6 Number Systems Desirable properties: –Efficient encoding (2 n bit patterns. How many numbers?) –Positive and negative »Closure (almost) under addition and subtraction Except when overflow »Representation of positive numbers same in most systems »Major differences are in how negative numbers are represented –Efficient operations »Comparison: =, »Addition, Subtraction »Detection of overflow –Algebraic properties? »Closure under negation? »A == B iff A – B == 0 Three Major schemes: –sign and magnitude –ones complement –twos complement –(excess notation) Which one did you learn in 2 nd grade? 6

How do we represent signed numbers? What algebraic mapping would have this assignment? 6/23/2015 cs61cl f09 lec

6/23/2015 cs61cl f09 lec 6 Twos Complement 8 B k-1 B k-2 …B 0 represents -B k-1 2 k-1 + B -2 2 k-2 + …+ B 0 2 0

Sign Extension 6/23/2015 cs61cl f09 lec 6 9 0xxxxxxx xxxxxxx Positive Number 1xxxxxxx xxxxxxx Negative Number B B B B B B B *2 7 + B B B B B B B * … + 0*2 7 + B B B B B B B … B B B B B B B

MIPS Instruction Format Reg-Reg instructions (op == 0) Reg-Immed (op != 0) Jumps –jPC := PC || addr || 00 –jalPC := PC || addr || 00; R[31] := PC + 4 –jrPC := R[rs] 6/23/2015 cs61cl f09 lec 6 10 op 6 rs 5 rt 5 rd 5 shamt 5 funct 6 immediate 16 op 6 rs 5 rt 5 R: Reg-Reg I: Reg-Imed Addr 26 op 6 J: Jump

MIPS Instruction Format Reg-Reg instructions (op == 0) Reg-Immed (op != 0) Jumps Branches –BEQ, BNEPC := (R[rs] ?= R[rt]) ? PC + signEx(im16) : PC+4 –BLT, BGT, BLE, BGTE are pseudo ops –Move and LI are pseudo ops too 6/23/2015 cs61cl f09 lec 6 11 op 6 rs 5 rt 5 rd 5 shamt 5 funct 6 immediate 16 op 6 rs 5 rt 5 R: Reg-Reg I: Reg-Imed Addr 26 op 6 J: Jump

6/23/2015 cs61cl f09 lec 6 So what about subtraction? Subtraction – or addition of a negative number – should move “backwards” on the number wheel?

6/23/2015 cs61cl f09 lec 6 Finite representation? What happens when A + B > 2 N - 1 ? –Overflow –Detect? What happens when A - B < 0 ? –Negative numbers? –Borrow out?

Administrative Issues HW 5 is due tonight before midnight Proj 1 is due Friday –your submission must build by running make in the directory containing the source files - that is what the autograder does –you should confirm that it passes proj1.tests/test.proj1 –autograder will run additional tests, so generate your own »feel free to share them »can run reference asm16 to generate.o and.syms Midterm is wed 10/7 in 10 evans –alternate is mon 10/5 in 310 Soda, send , and do poll –review session is Sunday evening (TBA) –homework this week is to study previous midterms Monday office hours moved to tues 9-11 in QC 3 6/23/2015 cs61cl f09 lec 6 14

Can’t get enough C fun&games… Write a program in C that reproduces itself –the output is the text of the program –you can compile the output with gcc and it will produce a program that reproduces itself… Hint: –Write the following sentence twice, the second in quotes. “Write the following sentence twice, the second in quotes.” –it is way easier in scheme 6/23/2015 cs61cl f09 lec 6 15

Other choices for negative numbers 6/23/2015 cs61cl f09 lec 6 16

6/23/2015 cs61cl f09 lec 6 Sign and Magnitude High order bit is sign: 0 = positive (or zero), 1 = negative Remaining low order bits is the magnitude: 0 (000) thru 7 (111) Number range for n bits = +/- 2 n Representations for 0? Operations: =,, +, - ??? Example: N = = =

6/23/2015 cs61cl f09 lec 6 Ones Complement N is positive number, then N is its negative 1's complement N = (2 n - 1) - N Example: 1's complement of 7 -7 in 1's comp. Bit manipulation: simply complement each of the bits > Algebraically … 18

6/23/2015 cs61cl f09 lec 6 Ones Complement on the number wheel Subtraction implemented by addition & 1's complement Sign is easy to determine Closure under negation. If A can be represented, so can -A Still two representations of 0! If A = B then is A – B == 0 ? Addition is almost clockwise advance, like unsigned 19

6/23/2015 cs61cl f09 lec 6 Twos Complement number wheel Easy to determine sign (0?) Only one representation for 0 Addition and subtraction just as in unsigned case Simple comparison: A < B iff A – B < 0 One more negative number than positive number - one number has no additive inverse 20

6/23/2015 cs61cl f09 lec 6 Twos Complement (algebraically) N* = 2 n - N Example: Twos complement of 7 2 = = = repr. of -7 Example: Twos complement of = = = repr. of 7 4 sub Bit manipulation: Twos complement: take bitwise complement and add one > > 1001 (representation of -7) > > 0111 (representation of 7) 21

6/23/2015 cs61cl f09 lec 6 How is addition performed in each number system? Operands may be positive or negative 22

6/23/2015 cs61cl f09 lec 6 Twos Complement Addition (-3) Simpler addition scheme makes twos complement the most common choice for integer number systems within digital systems Perform unsigned addition and Discard the carry out. Overflow? 23

6/23/2015 cs61cl f09 lec 6 Sign Magnitude Addition (-3) result sign bit is the same as the operands' sign Operand have same sign: unsigned addition of magnitudes Operands have different signs: subtract smaller from larger and keep sign of the larger 24

6/23/2015 cs61cl f09 lec 6 Ones complement addition (-3) End around carry Perform unsigned addition, then add in the end-around carry 25

6/23/2015 cs61cl f09 lec 6 2s Comp: ignore the carry out -M + N when N > M: M* + N = (2 - M) + N = 2 + (N - M) n n Ignoring carry-out is just like subtracting 2 n -M + -N where N + M ≤ 2 n-1 -M + (-N) = M* + N* = (2 - M) + (2 - N) = 2 - (M + N) + 2 n n After ignoring the carry, this is just the right twos compl. representation for -(M + N)! nn 26

6/23/2015 cs61cl f09 lec 6 2s Complement Overflow Add two positive numbers to get a negative number or two negative numbers to get a positive number = -8! = +7! How can you tell an overflow occurred ? 27

6/23/2015 cs61cl f09 lec 6 2s comp. Overflow Detection Overflow No overflow Overflow occurs when carry in to sign does not equal carry out 28