110 likes | 167 Views
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.
E N D
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
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
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 ???
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
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
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
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
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 !!!
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’
This Concludes The Slides for this Section Choose an Option: Repeat Slides for this Section Go To Next Set of Slides For this Chapter Go To Slide Index For Chapter 3 Go To Slide Index For Chapter 4 Go To Slide Index For Textbook Go To Home Page