5toy2 - LINC TOY II Introduction to Computer Science...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computer Science Sedgewick and Wayne Copyright © 2007 http://www.cs.Princeton.EDU/IntroCS TOY II LINC 5 7 What We've Learned About TOY Data representation. Binary and hex. TOY. ! Box with switches and lights. ! 16-bit memory locations, 16-bit registers, 8-bit pc. ! 4,328 bits = (255 ! 16) + (15 ! 16) + (8) = 541 bytes! ! von Neumann architecture. TOY instruction set architecture. 16 instruction types. TOY machine language programs. Variables, arithmetic, loops. 8 What We Do Today Data representation. Negative numbers. Input and output. Standard input, standard output. Manipulate addresses. References (pointers) and arrays. TOY simulator in Java.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
9 Negative Numbers 10 How to add and subtract binary numbers Binary addition facts: ! 0 + 0 = 0 ! 0 + 1 = 1 + 0 = 1 ! 1 + 1 = 10 ! 1 + 1 + 1 = 11 (needed for carries) Bigger numbers example: 1 1 1 013 0 0 0 0 1 1 0 1 + 092 + 0 1 0 1 1 1 0 0 105 0 1 1 0 1 0 0 1 OK, but: subtract? ! Subtract by adding a negative integer (e.g., 6 - 4 = 6 + (-4)) ! OK, but: negative integers? carries 11 Representing Negative Integers TOY words are 16 bits each. ! We could use 16 bits to represent 0 to 2 16 - 1. ! We want negative integers too. ! Reserving half the possible bit-patterns for negative seems fair. Highly desirable property. If x is an integer, then the representation of -x, when added to x, is zero. x 0 0 1 1 0 1 0 0 +( -x ) + ? ? ? ? ? ? ? ? 0 0 0 0 0 0 0 0 0 x 0 0 1 1 0 1 0 0 + 1 1 0 0 1 0 1 1 +( -x ) 1 1 1 1 1 1 1 1 + 1 0 0 0 0 0 0 0 0 0 flip bits and add 1 12 “Two's Complement” Integers Properties: ! Leading bit (bit 15) signifies sign. ! Negative integer -N represented by 2 16 - N. ! Trick to compute -N: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 +4 13 12 11 10 15 14 7 9 8 6 4 1 0 3 2 5 1. Start with N. 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 2. Flip bits. 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 -4 3. Add 1.
Background image of page 2
13 Two's Complement Integers 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 13 12 11 10 15 14 7 9 8 6 4 1 0 3 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 7FFF 0004 0003 0002 0001 0000 FFFF FFFE FFFD FFFC 8000 +32767 +4 +3 +2 +1 +0 -1 -2 -3 -4 -32768 hex dec binary 14 Properties of Two's Complement Integers Nice properties: ! 0000000000000000 represents 0. ! -0 and +0 are the same. ! Addition is easy (see next slide). ! Checking for arithmetic overflow is easy. Not-so-nice properties.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

5toy2 - LINC TOY II Introduction to Computer Science...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online