{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 11_toy2 - What We've Learned About TOY TOY II Data...

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

2/3/08 1 Introduction to Computer Science Sedgewick and Wayne Copyright © 2007 http://www.cs.Princeton.EDU/IntroCS TOY II LINC 2 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. 3 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. 4 Negative Numbers

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

View Full Document
2/3/08 2 5 Decimal and binary addition. Subtraction. Add a negative integer. Q. How to represent negative integers? 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 Adding and Subtracting Binary Numbers carries e.g., 6 - 4 = 6 + (-4)) 6 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 7 -5 -4 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 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 +4 Two's Complement Integers To compute -x from x: Start with x. Flip bits. Add one. leading bit 8 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
2/3/08 3 9 Properties of Two's Complement Integers Properties. Leading bit (bit 15) signifies sign. 0000000000000000 represents zero. Negative integer -x represented by 2 16 - x. Addition is easy. Checking for arithmetic overflow is easy. Not-so-nice property. Can represent one more negative integer than positive integer. -32,768 = -2 15 32,768 = 2 15 10 Standard Input and Output 11 Standard Output Standard output. Writing to memory location FF sends one word to TOY stdout. Ex. 9AFF writes the integer in register A to stdout. 00: 0000 0 01: 0001 1 10: 8A00 RA mem[00] a = 0 11: 8B01 RB mem[01] b = 1 while(a > 0) { print a 13: 1AAB RA RA + RB a = a + b 14: 2BAB RB RA - RB b = a - b 15: DA12 if (RA > 0) goto 12 } 16: 0000 halt 0000 0001 0001 0002 0003 0005 0008 000D 0015 0022 0037 0059 0090 00E9 0179 0262 03DB 063D 0A18 1055 1A6D 2AC2 452F 6FF1 fibonacci.toy 12 Standard Input Standard input.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

11_toy2 - What We've Learned About TOY TOY II Data...

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

View Full Document
Ask a homework question - tutors are online