CSCE 212 Computer Architecture Lecture 2 Integers Topics

  • Slides: 35
Download presentation
CSCE 212 Computer Architecture Lecture 2 Integers Topics n n n January 16, 2018

CSCE 212 Computer Architecture Lecture 2 Integers Topics n n n January 16, 2018 Login in SWGN 1 D 39, a Linux Lab (3 D 22 is another) Code-all. tgz (all the code from the book) Basic Unix Commands

Overview • Last Time – Lec 01 slides 1 -? • Course Pragmatics n

Overview • Last Time – Lec 01 slides 1 -? • Course Pragmatics n n n Website http: //www. cse. sc. edu/~matthews/Courses/212/index. html Text - "Computer Systems: A Programmer's Perspective" 3 rd ed Ints are not integers; floats are not reals; unsigned are Z mod 2 w • Today n n Slides 29 - from Lec 01 Two’s Complement formal notation Two’s Complement representation (from text) -1 on highest bit Two’s Complement representation (take 2) l To represent a positive number - same as signed – 2 – magnitude l To represent a negative number – take two’s complement of magnitude CSCE 212 H Spring 2018

Review – Values represented by int Representations • Unsigned • Signed Magnitude • 2’s

Review – Values represented by int Representations • Unsigned • Signed Magnitude • 2’s complement – 3 – CSCE 212 H Spring 2018

Sign Extension • Task: n Given w-bit signed integer x n Convert it to

Sign Extension • Task: n Given w-bit signed integer x n Convert it to w+k-bit integer with same value • Rule: n n Make k copies of sign bit: X = xw– 1 , …, xw– 1 , xw– 2 , …, x 0 w k copies of MSB X • • • X – 4 – • • • k • • • w CSCE 212 H Spring 2018

Sign Extension Example short int x = 15213; int ix = (int) x; short

Sign Extension Example short int x = 15213; int ix = (int) x; short int y = -15213; int iy = (int) y; x ix y iy Decimal 15213 -15213 Hex 3 B 00 00 3 B C 4 FF FF C 4 6 D 6 D 93 93 Binary 00111011 00000000 00111011 11000100 11111111 11000100 01101101 10010011 • Converting from smaller to larger integer data type • C automatically performs sign extension – 5 – CSCE 212 H Spring 2018

Summary: Expanding, Truncating: Basic Rules • Expanding (e. g. , short int to int)

Summary: Expanding, Truncating: Basic Rules • Expanding (e. g. , short int to int) n Unsigned: zeros added n Signed: sign extension Both yield expected result n • Truncating (e. g. , unsigned to unsigned short) n n n – 6 – Unsigned/signed: bits are truncated Result reinterpreted Unsigned: mod operation Signed: similar to mod For small numbers yields expected behavior CSCE 212 H Spring 2018

Unsigned Addition u • • • +v u+v UAddw(u , v) • • •

Unsigned Addition u • • • +v u+v UAddw(u , v) • • • Operands: w bits True Sum: w+1 bits Discard Carry: w bits • Standard Addition Function n Ignores carry output • Implements Modular Arithmetic s = – 7 – UAddw(u , v) = u + v mod 2 w CSCE 212 H Spring 2018

Visualizing (Mathematical) Integer Addition Add 4(u , v) • Integer Addition n 4 -bit

Visualizing (Mathematical) Integer Addition Add 4(u , v) • Integer Addition n 4 -bit integers u, v n Compute true sum Add 4(u , v) n Values increase linearly with u and v Forms planar surface n v u – 8 – CSCE 212 H Spring 2018

Visualizing Unsigned Addition Overflow • Wraps Around n If true sum ≥ 2 w

Visualizing Unsigned Addition Overflow • Wraps Around n If true sum ≥ 2 w n At most once UAdd 4(u , v) True Sum 2 w+1 Overflow 2 w 0 – 9 – v Modular Sum u CSCE 212 H Spring 2018

Two’s Complement Addition Operands: w bits True Sum: w+1 bits Discard Carry: w bits

Two’s Complement Addition Operands: w bits True Sum: w+1 bits Discard Carry: w bits u • • • + v u+v • • • TAddw(u , v) • • TAdd and UAdd have Identical Bit-Level Behavior n Signed vs. unsigned addition in C: int s, t, u, v; s = (int) ((unsigned) u + (unsigned) v); t = u + v n – 10 – Will give s == t CSCE 212 H Spring 2018

TAdd Overflow True Sum • Functionality n n n – 11 – True sum

TAdd Overflow True Sum • Functionality n n n – 11 – True sum requires w+1 bits Drop off MSB Treat remaining bits as 2’s comp. integer 0 111… 1 0 100… 0 2 w– 1 Pos. Over 2 w – 1– 1 TAdd Result 011… 1 0 000… 0 1 011… 1 – 2 w – 1 100… 0 1 000… 0 – 2 w Neg. Over CSCE 212 H Spring 2018

Visualizing 2’s Complement Addition Neg. Over • Values n 4 -bit two’s comp. n

Visualizing 2’s Complement Addition Neg. Over • Values n 4 -bit two’s comp. n Range from -8 to +7 TAdd 4(u , v) • Wraps Around n If sum 2 w– 1 l Becomes negative l At most once n l Becomes – 12 – v If sum < – 2 w– 1 positive l At most once u Pos. Over CSCE 212 H Spring 2018

Multiplication • Goal: Computing Product of w-bit numbers x, y n Either signed or

Multiplication • Goal: Computing Product of w-bit numbers x, y n Either signed or unsigned • But, exact results can be bigger than w bits n Unsigned: up to 2 w bits l Result range: 0 ≤ x * y ≤ (2 w – 1) 2 = 22 w – 2 w+1 + 1 n Two’s complement min (negative): Up to 2 w-1 bits l Result range: x * y ≥ (– 2 w– 1)*(2 w– 1– 1) = – 22 w– 2 + 2 w – 1 n Two’s complement max (positive): Up to 2 w bits, but only for (TMinw)2 l Result range: x * y ≤ (– 2 w– 1) 2 = 22 w– 2 • So, maintaining exact results… – 13 – n would need to keep expanding word size with each product CSCE 212 H Spring 2018 computed

Unsigned Multiplication in C Operands: w bits True Product: 2*w bits u·v u •

Unsigned Multiplication in C Operands: w bits True Product: 2*w bits u·v u • • • * v • • • UMultw(u , v) Discard w bits: w bits • Standard Multiplication Function n Ignores high order w bits • Implements Modular Arithmetic UMultw(u , v) – 14 – = u · v mod 2 w CSCE 212 H Spring 2018

Signed Multiplication in C u * v Operands: w bits True Product: 2*w bits

Signed Multiplication in C u * v Operands: w bits True Product: 2*w bits u·v Discard w bits: w bits • • • TMultw(u , v) • • • • Standard Multiplication Function n – 15 – Ignores high order w bits Some of which are different for signed vs. unsigned multiplication Lower bits are the same CSCE 212 H Spring 2018

Power-of-2 Multiply with Shift • Operation n u << k gives u * 2

Power-of-2 Multiply with Shift • Operation n u << k gives u * 2 k n Both signed and unsigned u * 2 k Operands: w bits True Product: w+k bits Discard k bits: w bits k u · 2 k • • • 0 • • • 0 1 0 • • • 0 0 • • • UMultw(u , 2 k) TMultw(u , 2 k) • • • • Examples n u << 3 == u * 8 (u << 5) – (u << 3)== u * 24 n Most machines shift and add faster than multiply n – 16 – l Compiler generates this code automatically CSCE 212 H Spring 2018

Unsigned Power-of-2 Divide with Shift • Quotient of Unsigned by Power of 2 n

Unsigned Power-of-2 Divide with Shift • Quotient of Unsigned by Power of 2 n u >> k gives u / 2 k n Uses logical shift Operands: Division: Result: – 17 – u • • • k • • • / 2 k u / 2 k 0 • • • 0 1 0 • • • 0 0 • • • u / 2 k 0 • • • 0 0 • • • Binary Point 0 0. • • • CSCE 212 H Spring 2018

Arithmetic: Basic Rules • Addition: n n Unsigned/signed: Normal addition followed by truncate, same

Arithmetic: Basic Rules • Addition: n n Unsigned/signed: Normal addition followed by truncate, same operation on bit level Unsigned: addition mod 2 w l Mathematical addition + possible subtraction of 2 w n Signed: modified addition mod 2 w (result in proper range) l Mathematical addition + possible addition or subtraction of 2 w • Multiplication: n – 18 – n Unsigned/signed: Normal multiplication followed by truncate, same operation on bit level Unsigned: multiplication mod 2 w CSCE 212 H Spring 2018

Why Should I Use Unsigned? • Don’t use without understanding implications n Easy to

Why Should I Use Unsigned? • Don’t use without understanding implications n Easy to make mistakes unsigned i; for (i = cnt-2; i >= 0; i--) a[i] += a[i+1]; n Can be very subtle #define DELTA sizeof(int) int i; for (i = CNT; i-DELTA >= 0; i-= DELTA). . . – 19 – CSCE 212 H Spring 2018

Counting Down with Unsigned • Proper way to use unsigned as loop index unsigned

Counting Down with Unsigned • Proper way to use unsigned as loop index unsigned i; for (i = cnt-2; i < cnt; i--) a[i] += a[i+1]; • See Robert Seacord, Secure Coding in C and C++ n C Standard guarantees that unsigned addition will behave like modular arithmetic l 0 – 1 UMax • Even better size_t i; for (i = cnt-2; i < cnt; i--) a[i] += a[i+1]; n – 20 – n Data type size_t defined as unsigned value with length = word size CSCE 212 H Spring 2018 Code will work even if cnt = UMax

Why Should I Use Unsigned? (cont. ) • Do Use When Performing Modular Arithmetic

Why Should I Use Unsigned? (cont. ) • Do Use When Performing Modular Arithmetic n Multiprecision arithmetic • Do Use When Using Bits to Represent Sets n – 21 – Logical right shift, no sign extension CSCE 212 H Spring 2018

Byte-Oriented Memory Organization F • • 0 0 • • • F • •

Byte-Oriented Memory Organization F • • 0 0 • • • F • • • F • Programs refer to data by address n Conceptually, envision it as a very large array of bytes l In reality, it’s not, but can think of it that way n An address is like an index into that array l and, a pointer variable stores an address • Note: system provides private address spaces to each “process” n Think of a process as a program being executed CSCE 212 H Spring 2018 So, a program can clobber its own data, but not that of others – 22 – n

Machine Words • Any given computer has a “Word Size” n Nominal size of

Machine Words • Any given computer has a “Word Size” n Nominal size of integer-valued data l and of addresses n Until recently, most machines used 32 bits (4 bytes) as word size l Limits addresses to 4 GB (232 bytes) n Increasingly, machines have 64 -bit word size l Potentially, could have 18 PB (petabytes) of addressable memory l That’s 18. 4 X 1015 – 23 –n Machines still support multiple data formats CSCE 212 H Spring 2018

Word-Oriented Memory Organization • Addresses Specify Byte Locations n n Address of first byte

Word-Oriented Memory Organization • Addresses Specify Byte Locations n n Address of first byte in word Addresses of successive words differ by 4 (32 -bit) or 8 (64 -bit) 32 -bit 64 -bit Words Addr = 0000 ? ? Addr = 0004 ? ? Addr = 0008 ? ? Addr = 0012 ? ? – 24 – Addr = 0000 ? ? Addr = 0008 ? ? Bytes Addr. 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 CSCE 212 H Spring 2018

Example Data Representations C Data Type – 25 – Typical 32 -bit Typical 64

Example Data Representations C Data Type – 25 – Typical 32 -bit Typical 64 -bit x 86 -64 char 1 1 1 short 2 2 2 int 4 4 4 long 4 8 8 float 4 4 4 double 8 8 8 long double − − 10/16 pointer 4 8 8 CSCE 212 H Spring 2018

Byte Ordering • So, how are the bytes within a multi-byte word ordered in

Byte Ordering • So, how are the bytes within a multi-byte word ordered in memory? • Conventions n Big Endian: Sun, PPC Mac, Internet l Least significant byte has highest address n Little Endian: x 86, ARM processors running Android, i. OS, and Windows l Least significant byte has lowest address – 26 – CSCE 212 H Spring 2018

Byte Ordering Example • Example n Variable x has 4 -byte value of 0

Byte Ordering Example • Example n Variable x has 4 -byte value of 0 x 01234567 n Address given by &x is 0 x 100 Big Endian Little Endian – 27 – 0 x 100 0 x 101 0 x 102 0 x 103 01 23 45 67 0 x 100 0 x 101 0 x 102 0 x 103 67 45 23 01 CSCE 212 H Spring 2018

Representing Integers Binary: Decimal: 15213 Hex: int A = 15213; IA 32, x 86

Representing Integers Binary: Decimal: 15213 Hex: int A = 15213; IA 32, x 86 -64 6 D 3 B 00 00 Sun 00 00 3 B 6 D int B = -15213; IA 32, x 86 -64 93 C 4 FF FF – 28 – Sun FF FF C 4 93 0011 1011 0110 1101 3 B 6 D long int C = 15213; IA 32 6 D 3 B 00 00 x 86 -64 Sun 6 D 3 B 00 00 3 B 6 D Two’s complement representation CSCE 212 H Spring 2018

Examining Data Representations • Code to Print Byte Representation of Data n Casting pointer

Examining Data Representations • Code to Print Byte Representation of Data n Casting pointer to unsigned char * allows treatment as a byte array typedef unsigned char *pointer; void show_bytes(pointer start, size_t len){ size_t i; for (i = 0; i < len; i++) printf(”%pt 0 x%. 2 xn", start+i, start[i]); printf("n"); } Printf directives: %p: Print pointer %x: Print Hexadecimal – 29 – CSCE 212 H Spring 2018

show_bytes Execution Example int a = 15213; printf("int a = 15213; n"); show_bytes((pointer) &a,

show_bytes Execution Example int a = 15213; printf("int a = 15213; n"); show_bytes((pointer) &a, sizeof(int)); Result (Linux x 86 -64): int a = 15213; 0 x 7 fffb 7 f 71 dbc 0 x 7 fffb 7 f 71 dbd 0 x 7 fffb 7 f 71 dbe 0 x 7 fffb 7 f 71 dbf – 30 – 6 d 3 b 00 00 CSCE 212 H Spring 2018

Representing Pointers int B = -15213; int *P = &B; Sun IA 32 x

Representing Pointers int B = -15213; int *P = &B; Sun IA 32 x 86 -64 EF AC 3 C FF 28 1 B FB F 5 FE 2 C FF 82 FD 7 F 00 00 Different compilers & machines assign different locations to objects – 31 – CSCE 212 H Spring 2018

Representing Strings • Strings in C n Represented by array of characters n Each

Representing Strings • Strings in C n Represented by array of characters n Each character encoded in ASCII format n n IA 32 Sun 31 set l Standard 7 -bit encoding of character 31 l Character “ 0” has code 0 x 30 38 38 » Digit i has code 0 x 30+i String should be null-terminated 32 32 31 31 l Final character = 0 33 33 00 00 • Compatibility – 32 – char S[6] = "18213"; Byte ordering not an issue CSCE 212 H Spring 2018

Integer C Puzzles Initialization int x = foo(); int y = bar(); unsigned ux

Integer C Puzzles Initialization int x = foo(); int y = bar(); unsigned ux = x; unsigned uy = y; – 33 – • • • • x < 0 �� ux >= 0 x & 7 == 7 �� ux > -1 x > y �� x * x >= 0 x > 0 && y > 0 �� x >= 0 �� x <= 0 �� (x|-x)>>31 == -1 ux >> 3 == ux/8 x >> 3 == x/8 x & (x-1) != 0 ((x*2) < 0) (x<<30) < 0 -x < -y x + y > 0 -x <= 0 -x >= 0 CSCE 212 H Spring 2018

Application of Boolean Algebra • Applied to Digital Systems by Claude Shannon n 1937

Application of Boolean Algebra • Applied to Digital Systems by Claude Shannon n 1937 MIT Master’s Thesis n Reason about networks of relay switches l Encode closed switch as 1, open switch as 0 A&~B A Connection when ~B A&~B | ~A&B ~A&B – 34 – = A^B CSCE 212 H Spring 2018

– 35 – CSCE 212 H Spring 2018

– 35 – CSCE 212 H Spring 2018