1 / 32

RAM Allocation

RAM Allocation. RAM Allocation. Chapter 3. What you MUST know before we start:. RAM Allocation. (Remember: The topics in this course build on each other). What basic data types there are:. Signed/Unsigned Characters. Signed/Unsigned Shorts/Integers/Longs. float/double.

astra
Download Presentation

RAM Allocation

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. RAM Allocation RAM Allocation Chapter 3

  2. What you MUST know before we start: RAM Allocation (Remember: The topics in this course build on each other) • What basic data types there are: • Signed/Unsigned Characters • Signed/Unsigned Shorts/Integers/Longs • float/double • The difference between characters & numbers • How many bits each data type requires • How the sign-bit is used • How negative values are stored • What precision and magnitude are and how they affect real numbers

  3. Storing Characters in RAM RAM Allocation • We know that characters are really numbers: • signed char (or char, by default) on 8-bits: Value Sign bit 8-bits = 1-byte = X X X X X X X X • unsigned char Value Regardless of whether signed or unsigned, the data type char requires 1-byte of storage per variable

  4. Consider the following C Declaration RAM Allocation chara, b=‘f’, c = 87; What is the effect of the command ??? 1. We are reserving 3-bytes of storage 2. We are associating each location with a variable name (LOCATIONSa, b, and c) 3. We are initializing location b with the Character ‘f’ = ASCII 102 = 11001102 4. We are placing the value 87 (= 10101112) into location c

  5. Where in RAM will we find locations a, b, and c ??? RAM Allocation We Don’t Know: • Address allocations are made at RUN-TIME and are • based on available locations. • Assume that at run-time, we find that Addresses 5010, 5014, and 5015 are Available: • Variable a will be assigned to address 5010 • Variable b will be assigned to address 5014 • Variable c will be assigned to address 5015 How Will this appear in RAM ???

  6. 5018 5005 5020 5006 5007 5008 5015 (c) 5009 5019 5011 5017 5012 5013 5016 5014 (b) 5010 (a) 11100001 01100110 00101001 11000110 10111001 00100111 00000100 00000000 00000000 01110011 00000001 10010010 11010011 00100010 01010111 00110110 Given: chara, b=‘f’, c = 87; RAM Allocation Where: Variable a => 5010: Unassigned Variable b => 5014: ‘f’ = 10210 = 011001102 Variable c => 5015: 8710 = 010101112

  7. Why Does Location a (address 5010) contain something?? We did NOT initialize the variable. RAM Allocation Whatever was previously stored at that location is still there. If we were now (after variable allocation) to issue the command: printf (“%c %d”, a, a); We would produce the output:  -31 That Makes NO Sense at ALL !! Let’s Examine why this would occur

  8. 11100001 Location 5010 (variable a) contains: RAM Allocation Because location 5010 contains a (signed) char, the numeric value is: 1 1 1 0 0 0 0 1 Left-most bit = ‘1’: Value is negative: COMPLIMENT Since we are using 2’s Compliment: ADD 1: 0 0 1 1 1 1 0 + 1 0 0 1 1 1 1 1 = -(24 + 23 + 22 + 21 + 20) = -(16 + 8 + 4 + 2 + 1) = -31

  9. That still doesn't explain why the ASCII character  is printed !!! RAM Allocation Even though the numeric value is -31, The value of location a is checked against the ASCII table as if it were an unsignedchar: 1 1 1 0 0 0 0 1 Evaluates to 27 + 26 + 25 + 20 = 128 + 64 + 32 + 1 = 225 Which corresponds to the ASCII Character 

  10. I don’t know if I believe – I know about your propensity to lie to us !!! RAM Allocation OK – here’s a program I wrote: And here’s the output I received:

  11. What if we try to store an illegal number, say 837, into a character location in RAM ??? RAM Allocation If we were to issue the commands: charillegal =837; printf (“%c %d”, illegal, illegal); We would produce the output: E 69 WHY ??? 83710 = 11010001012 BUT requires 10-bits of storage WE RESERVED ONLY 8-BITS !!!

  12. Because we reserved only 1-byte, ONLY the right-most 8-bits will be stored: RAM Allocation 1 1 0 1 0 0 0 1 0 1 = 26 + 22 + 20 = 64 + 4 + 1 = 69 Which Corresponds to the ASCII Character ‘E’

  13. Integers (c data type int) RAM Allocation Integers require 2 CONTIGUOUS bytes (16-bits) of storage (of course we all know it is really 4 Contiguous bytes) Consider the c declaration: intx = ‘W’, y, z = 5487; In Fact we are (once again): 1. Requesting 6-bytes (2 per variable) of RAM be reserved 2. Associating each variable name with a reserved location (LOCATIONSx, y, and z) 3. Initializing location x with ‘W’ (= ASCII 87 = 10101112= 00000000010101112 on 16-bits) 4. Initializing location z with 5487 (548710 = 10101011011112 = 00010101011011112 on 16-bits)

  14. Let’s Assume locations 7212, 7213, 7214, 7216, and 7219 through 7250 Are available: RAM Allocation • Variable x is assigned address 7212 (and 7213) • Variable y is assigned address 7219 (and 7220) • Variable z is assigned address 7221 (and 7222) Why aren’t addresses 7214 and 7216 used??? Because we need 2 contiguous bytes of storage for integers: • We can’t use location 7214 because location 7215 is NOT available • We can’t use location 7216 because location 7217 is NOT available Looking at RAM, We might see:

  15. 7220 (y) 7217 7216 7222 (z) 7215 7214 7223 7213 (x) 7226 7212 (x) 7224 7211 7221 (z) 7225 7219 (y) 7218 11100001 00011001 00101001 00000000 01010111 00100111 00000100 01110011 01100110 00000000 000101010 00000000 10010010 00100010 01101111 00110110 intx = ‘W’,y,z= 5487; x = 0000000001010111 y is unassigned z = 0001010101101111 RAM Allocation • x stored at address 7212 (and 7213) • y stored at address 7219 (and 7220) • z stored at address 7221 (and 7222)

  16. 00011001 01100110 7219 7220 Once again, Notice that there is ‘garbage’ in location y (Addresses 7219 & 7220): RAM Allocation = 212 + 211 + 28 + 26 + 25 + 22 + 21 = 4096 + 2048 + 256 + 64 + 32 + 4 + 2 = 6,502 Which is the output we would obtain if we issued the command: printf (“%d”, y);

  17. 7223 (x) 7222 (y) 7211 7228 7217 7216 7212 (x) 7224 (x) 7218 7213 (x) 7219 (y) 7225 (x) 7226 (x) 7214 (x) 7220 (y) 7221 (y) 7227 7215 (x) 01111000 00101001 00000000 00010010 01100110 01100001 00000000 00000000 00000000 00000000 00000000 00010101 01101111 00000000 00000000 01010111 00010000 00011001 Lest we forget, since we know that integers are really stored on 32-bits: RAM Allocation intx = ‘W’,y,z= 5487; x = 0000000001010111 y is unassigned z = 0001010101101111 • x stored at address 7212 (to 7215) • y stored at address 7219 (to 7222) • z stored at address 7223 (to 7226)

  18. What would happen if we tried to print an integer as as an ASCII character ??? RAM Allocation Depends: intanumber =104; printf (“%c”, anumber); • If we issue the commands: h The output will be: Which is the ASCII character for decimal value 104 intanumber =6502; printf (“%c”, anumber); • If we issue the commands: The output will be: f WHAT ???

  19. Don’t forget how the integer 6502 was stored (on 16-bits): RAM Allocation 0 0 0 1 1 0 0 1 0 1 1 0 0 1 1 0 When we try to print out a character according to the ASCII tables, we consider ONLY 8-bits (the right-most 8-bits) = 26 + 25 + 22 + 21 = 64 + 32 + 4 + 2 = 102 Which corresponds to the ASCII character ‘f’

  20. What if an illegal value is entered?? The same situation occurs as did when we entered an illegal value for the data type char: RAM Allocation intbadnumber =-52434; If we make the declaration: Since: 52,43410= 11001100110100102 Then: - 52,43410= 00110011001011012 1’s Comp. + 1 0011001100101110 2’s Comp. = 213 + 212 + 29 + 28 + 25 + 23 + 22 + 21 = 8192 + 4096 + 512 + 256 + 32 + 8 + 4 + 2 = 13,102 Which is the output produced by the Statement: printf (“%d”, badnumber);

  21. I don’t know if I believe anymore -- About anything!!! RAM Allocation OK – let’s write another program – but instead of using the data type int, let’s use the data type short WHY ???? • Because, as we know, the data type short still requires 16-bits (2-bytes) of storage (As integers used to) • We will NOT have to change our illustration

  22. RAM Allocation Once again, here is the program I wrote: And here is the output I received:

  23. One more quick example: RAM Allocation Suppose we make the declaration: intbadnumber =72925; Requiring 17-bits Where: 7292510 = 100011100110111012 Since an integer is stored on 16-bits: = 212 + 211 + 210 + 27 + 26 + 24 + 23 + 22 + 20 = 4096 + 2048 + 1024 + 128 + 64 + 16 + 8 + 4 + 1 = 7,389 Which is the output produced by the Statement: printf (“%d”, badnumber);

  24. Other Integers: RAM Allocation Data Type Bits Required Legal Values unsigned int 16 (2-bytes) 0 through 65,535 (No sign bit) 32 (4-bytes) long Or signed long -2,147,483,648 through 2,147,483,647 32 (4-bytes) 0through 4,294,967,295 unsigned long How would the data type long appear in RAM ???

  25. Long Integers (c data type long) RAM Allocation Integers require 4 CONTIGUOUS bytes (32-bits) of storage (REMEMBER: The data types int and long are now the same) Consider the c declaration: longl1 = ‘3’, l2; In Fact we are (once again): 1. Requesting 8-bytes (4 per variable) of RAM be reserved 2. Associating each variable name with a reserved location (LOCATIONSl1, and l2) 3. Initializing location l1 with ‘3’ ( = ASCII 51 = 1100112 = 0000000000000000000000001100112 on 32-bits)

  26. Let’s again assume locations 9011 through 9017, and 9021 through 9076 Are available: RAM Allocation • Variable l1 is assigned address 9011 (through 9014) • Variable l2 is assigned address 9021 (through 9024) Why aren’t addresses 9015 through 9017 used??? Because we need 4 contiguous bytes of storage for longs: • If we were to try and store variable l2 at location 9015, we would need addresses 9016 through 9018 to be available also Looking at RAM, We might see:

  27. 9020 9017 9016 9022 9015 9014 9023 9013 9026 9012 9024 9011 9021 9025 9019 9018 11100001 00011001 00000000 00000000 00000000 00110011 00000100 01110011 01100110 00000000 111111111 00000000 10010010 00100010 01101111 00110110 longl1 = ‘3’, l2; l1 = 0000000000000000 0000000000110011 l2 is unassigned RAM Allocation • l1 stored at address 9011 (through 9014) • l2 stored at address 9021 (through 9024)

  28. What ‘Garbage’ will we find at location l1 ??? At addresses 9021 through 9024, we find: RAM Allocation (On 32-bits) 11111111011011110010001010010010 Since the left-most bit = ‘1’ ( ==> the number is negative) we must compliment: (1’s Comp.) 0000000100100001101110101101101 + 1 (2’s Comp.) 0000000100100001101110101101110 = -(223 + 220 + 215 + 214 + 212 + 211 + 210 + 28 + 26 + 25 + 23 + 22 + 21) =-(8,388,608 + 1,048,576 + 32,768 + 16,384 + 4,096 + 2,048 + 1,024 + 256 + 64 + 32 + 8 + 4 + 2) = -9,493,870

  29. Floating-point numbers (real numbers) RAM Allocation • data type float: • 4CONTIGUOUS bytes (32-bits) per variable • 7 decimals of precision • Sometimes also referred to as single precision reals • data type double: • 8CONTIGUOUS bytes (64-bits) per variable • ANSI: 10 decimals of precision • data type long double: • 16 CONTIGUOUS bytes (128-bits) per variable • ANSI: 10 decimals of precision

  30. 8918 8915 8924 8914 8920 8916 8912 8921 8913 8919 8910 8922 8909 8925 8917 8911 00110011 00000000 00100010 00110110 00000000 10100010 01001110 00000100 00100010 01011101 00100010 101110011 00100010 01100110 01101111 10100010 RAM Storage ??? RAM Allocation floatf1 = -92.337, f2; Available: Addresses 8910 through 8993 • f1 stored at address 8910 (through 8913) • f2 stored at address 8914 (through 8917)

  31. So what do we need to do ?? RAM Allocation • Make sure you THOUROGHLY understand ALL of the concepts covered in these slides • Answer ALL of the relevant questions on the Review Page • Submit your References • Submit your Question(s) • Look at the Bits/Bytes/ASCII C/C++ Programming Assignment (it’s not due yet, but it can’t hurt to look at it) • Get ready for Quiz 1 --- It’s coming ??? Any Questions ??? (Please!!)

  32. RAM Allocation

More Related