1 / 25

Please do not SIT on the RIGHT (if facing me)

Please do not SIT on the RIGHT (if facing me). X. More on Integers CSCi 2021 : Machine Architecture and Organization. Chapter 2.3. Today: Integers. Representing information as bits Bit-level manipulations Integers Representation: unsigned and signed Conversion, casting

kadeem
Download Presentation

Please do not SIT on the RIGHT (if facing me)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Please do not SIT on the RIGHT (if facing me) X

  2. More on IntegersCSCi 2021: Machine Architecture and Organization Chapter 2.3

  3. Today: Integers • Representing information as bits • Bit-level manipulations • Integers • Representation: unsigned and signed • Conversion, casting • Expanding, truncating • Addition, negation, multiplication, shifting • Summary Project 1 questions?

  4. Last Time • Bit operations => bit vectors • Bitwise => Logical C operators (0 or 1) • Bit shifting – what was interesting about this?

  5. Casting • Allows compiler to track types, and perform type-checking • Does not change bit patterns! • Casting • Explicit casting between signed & unsigned same as U2T and T2U inttx, ty; unsigned ux, uy; tx = (int) ux; uy = (unsigned)ty; • Implicit casting also occurs via assignments and parameter passing tx = ux; uy = ty;

  6. Casting Surprises • Expression Evaluation • If there is a mix of unsigned and signed in single expression, signed values implicitly cast to unsigned • Including comparison operations <, >, ==, <=, >= • Examples for W = 32: TMIN = -2,147,483,648 , TMAX = 2,147,483,647 • Constant1 Constant2 Relation Evaluation 0 0U -1 0 -1 0U 2147483647 -2147483647-1 2147483647U -2147483647-1 -1 -2 (unsigned)-1 -2 2147483647 2147483648U 2147483647 (int) 2147483648U 0 0U == unsigned -1 0 < signed -1 0U > unsigned 2147483647 -2147483648 > signed 2147483647U -2147483648 < unsigned -1 -2 > signed (unsigned) -1 -2 > unsigned 2147483647 2147483648U < unsigned 2147483647 (int) 2147483648U > signed

  7. Casting => Bugs: Code Security Example • Taken from version of FreeBSD (Unix) /* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[KSIZE]; /* Copy at most maxlen bytes from kernel region to user buffer */ intcopy_from_kernel(void *user_dest, intmaxlen) { /* Byte count len is minimum of buffer size and maxlen */ intlen = KSIZE < maxlen ? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; }

  8. Typical Usage /* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[KSIZE]; /* Copy at most maxlen bytes from kernel region to user buffer */ int copy_from_kernel(void *user_dest, int maxlen) { /* Byte count len is minimum of buffer size and maxlen */ int len = KSIZE < maxlen ? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; } #define MSIZE 528 void getstuff() { char mybuf[MSIZE]; copy_from_kernel(mybuf, MSIZE); printf(“%s\n”, mybuf); }

  9. unsigned Malicious Usage /* Declaration of library function memcpy */ void *memcpy(void *dest, void *src, size_t n); /* Kernel memory region holding user-accessible data */ #define KSIZE 1024 char kbuf[KSIZE]; /* Copy at most maxlen bytes from kernel region to user buffer */ int copy_from_kernel(void *user_dest, int maxlen) { /* Byte count len is minimum of buffer size and maxlen */ int len = KSIZE < maxlen ? KSIZE : maxlen; memcpy(user_dest, kbuf, len); return len; } #define MSIZE 528 void getstuff() { char mybuf[MSIZE]; copy_from_kernel(mybuf, -MSIZE); . . . }

  10. SummaryCasting Signed ↔ Unsigned: Basic Rules • Bit pattern is maintained • But reinterpreted • Can have unexpected effects: adding or subtracting 2w • Expression containing signed and unsigned int • int is cast to unsigned!!

  11. Today • Representing information as bits • Bit-level manipulations • Integers • Representation: unsigned and signed • Conversion, casting • Expanding, truncating • addition, negation, multiplication, shifting • Summary

  12. Expand Bit Representation • Expand => add bits to a number but maintain its value • Done at the C level • Why? • Expressions involving two of: short, int, long, long long, … • Different for signed and unsigned • UNSigned: easy => just add leading 0’s (on the left) • Convince yourself this won’t change the value

  13. w X • • • • • • X • • • • • • w k Signed: Sign Extension • Task: • Given w-bit signed integer x • Convert it to w+k-bit integer with same value • Rule: • Make k copies of sign bit: • X = xw–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0 k copies of MSB

  14. Does it work? • Expand 1011 (4 bits unsigned/two’s complement) to 6 bits • W=4, 1011 => -5 • k = 2 => 111011 => -5

  15. Decimal Hex Binary 15213 x 3B 6D 00111011 01101101 15213 ix 00 00 3B 6D 00000000 00000000 00111011 01101101 -15213 y C4 93 11000100 10010011 -15213 iy FF FF C4 93 11111111 11111111 11000100 10010011 Sign Extension Example short intx = 15213; int ix = (int) x; short inty = -15213; intiy = (int) y; • Converting from smaller to larger integer data type • C automatically performs sign extension

  16. Truncation • Lose bits – opposite of extending … • Truncating (e.g., unsigned to unsigned short) w-> k bit number • drop high-order w-k bits • unsigned/signed: bits are truncated • 1011 (w=4, k=2) => 11 • Result reinterpreted – why? • Can overflow • Unsigned: mod operation x mod 2^k • 11 mod 4 => 3 • Signed: similar to unsigned • For small numbers yields expected behavior

  17. Summary:Expanding, Truncating: Basic Rules • Expanding (e.g., short int to int) • Unsigned: zeros added • Signed: sign extension • Both yield expected result • Truncation (e.g. int to short int, w to k bits) • Remove w-k leading bits • Done in software (C/compiler)

  18. Today • Representing information as bits • Bit-level manipulations • Integers • Representation: unsigned and signed • Conversion, casting • Expanding, truncating • Addition, negation, multiplication, shifting • Summary

  19. Complement & Increment Examples x = 15213, claim: complement ~x + 1 x = 0 (2 bytes) x + (~x + 1) = ? Additive inverse

  20. Practice • 4 bits • 3 • C 3 -3 0011 => 1100 => 1101 12 4? 1100 => 0011 => 0100 Add them: 12 + 4 => 10000 : 3 + -3 => 10000

  21. • • • • • • • • • • • • Unsigned Addition u • Standard Addition Function • Ignores carry output • Implements Modular Arithmetic s = UAddw(u , v) = u + v mod 2w Operands: w bits v + True Sum: w+1 bits u + v UAddw(u , v) Discard Carry: w bits overflow interpreted result

  22. Practice • Bitwise additions 011 + 100 111 + 001

  23. • • • • • • • • • • • • Two’s Complement Addition u • TAdd and UAdd have Identical Bit-Level Behavior • Signed vs. unsigned addition in C: int s, t, u, v; s = (int) ((unsigned) u + (unsigned) v); t = u + v • Will gives == t Operands: w bits + v True Sum: w+1 bits u + v Discard Carry: w bits TAddw(u , v)

  24. Two’s Complement Cases interpreted result

  25. Next Time • Integer multiplication • Have a great weekend

More Related