1 / 82

Internal Representation of Data in COBOL Data Types

Internal Representation of Data in COBOL Data Types. By K.V.R.S.Sarma (E&R) Bangalore. Audience. This document explains how data is internally represented in COBOL data types on IBM Mainframes.

Mercy
Télécharger la présentation

Internal Representation of Data in COBOL Data Types

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. Internal Representation of Data in COBOL Data Types By K.V.R.S.Sarma (E&R) Bangalore

  2. Audience • This document explains how data is internally represented in COBOL data types on IBM Mainframes. • Discussion on data types requires knowledge of binary data representation and binary arithmetic. Hence, this document introduces binary data representation and binary arithmetic as well.

  3. Topics • Binary & Hexadecimal Representation • Binary Arithmetic • Cobol Data Types & their Internal Representation • ASCII to EBCDIC Conversion Chart • Note: If you are comfortable with Binary arithmetic, you are advised to move on to Cobol Data Types & their Internal Representation

  4. Binary & Hexadecimal Number Systems

  5. Decimal Number System • We use decimal number system in our daily life. • Decimal number system has ten symbols. The symbols are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. • These ten symbols are used to represent all numbers. • If a number is represented by 456, it’s value is 4*102 + 5*101 + 6*100.

  6. Binary Number System • In Binary number system we use only two symbols, 0 and 1. These two symbols are used to represent all numbers. • If a number is represented by 10, it’s value is 1*21 + 0*20 = 2. • Similarly 101 = 1*210 + 0*21 + 1*20 = 1*22 + 0*21 + 1*20 = 4 + 0 + 1 = 5.

  7. Hexadecimal Number System • In hexadecimal number system we use sixteen different symbols, 0 - 9 & A,B,C,D,E,F. • Here A=10,B=11,C=12, D=13, E=14, & F = 15. • If a number is represented by 456 in hexadecimal number system, it’s value is – 4*162 + 5*161 + 6*160.

  8. Number Systems -1 • To represent a value 15, we need - • Two digits in decimal system. ( 1 & 5) • One digit in hexadecimal system. (F) • Four digits in binary system. (1111)

  9. Number Systems -2 • In decimal system using 2 digits we can represent 102 different numbers. (00 … 99, a total of 100) • Using ‘n’ digits in decimal system, we can represent, 10n different numbers.

  10. Number Systems -3 • In binary number system, using two digits we can represent four numbers – 00, 01, 10, 11 (22 numbers). • With ‘n’ digits we can represent - 2n numbers in binary number system. • Using eight digits we can represent 28 (i.e, 256), different numbers.

  11. Conversion from Binary to Hexadecimal Number System • To convert from one number system to another, we normally convert – • From Number System 1 to Decimal System • Then from Decimal System to Number System 2 • However, there is a direct mapping between binary number system and hexadecimal number system. • Hence it is very easy to convert from binary to hexadecimal and hexadecimal to binary system.

  12. Conversion from Binary to Hexadecimal Number System -2 • A group of four binary digits are clubbed into a single Hexadecimal digit. (or) a hexadecimal digit can be converted to a set of four binary digits. • The conversion is - 0000 binary = 0 hex; 0001 binary = 1 hex 0010 binary = 2 hex; 0011 binary = 3 hex 0100 binary = 4 hex; 0101 binary = 5 hex 0110 binary = 6 hex; 0111 binary = 7 hex etc

  13. Conversion from Binary to Hexadecimal Number System -3 • Let’s take a binary number 11000010. • It’s decimal value is – 1*27+1*26+1*21 = 128 + 64 + 2 = 194 • Applying the conversion – =1100 0010 binary = C2 hex • Decimal value of C2 hex - = C*161 + 2*160 = 12*16 + 2*1= 192 + 2 = 194

  14. Some Terminology • In computer systems, binary digits 0 and 1 are used to represent data. • in a computer system, a memory location storing a binary digit is known as a – ‘bit’. • Hence, in general a binary digit is also called as a bit.

  15. Some Terminology -2 • Bit is a very small unit. • In general data storage locations are specified in terms of ‘Bytes’, where 8 bits is One byte. • Similarly - • 2 Bytes is one ‘Half Word’ • 4 Bytes is one ‘Word’ • 8 Bytes is one ‘Double Word’ • 4 BITS is one ‘Nibble’ (Rarely Used)

  16. Binary Number System -5 • 210 Bytes is 1 KB (One Kilo Byte) • It is 1024 bytes actually. However, we call this as one Kilo bytes. • 220 Bytes is 1 MB (One Mega Byte) • Same as 210 KB • 230 Bytes is 1 GB (One Giga Byte) • Same as 210 MB • 240 Bytes is one Tera-byte • 250 Bytes is one Peta-byte • 260 Bytes is one Exa-byte

  17. Representation • Now we have three different number systems – decimal, binary & hexadecimal. • ‘100’ is a valid number in all three number systems. However, it’s value is different in each system. How do we decide, to which number system it belongs to? • To make things clear we use ‘0x’ as a prefix to hexadecimal numbers, and ‘0b’ as a prefix to binary numbers and none if it is a decimal number.

  18. Representation -2 • Hence - • 0x 100 is a hexadecimal number (value 256) • 0b 100 is a binary number (value 4) • 100 is a decimal number (value 100)

  19. Summary of Number Systems • If a number systems base is ‘b’; then using ‘n’ digits, we can represent ‘bn’ different values. • Direct conversion from binary to hexadecimal and hexadecimal to binary is very easy.

  20. Binary Arithmetic

  21. Binary Arithmetic • All mathematical operations in binary system are similar to the ones done in decimal number system. We can draw parallels and use them. • Mathematical operations on hexadecimal numbers are carried out by – • Converting hexadecimal numbers to binary numbers. • Carry out arithmetic operation on binary numbers. • Convert the binary numbers back to hexadecimal numbers.

  22. Addition -1 • 7 + 8 = 15 (Decimal System) • In the above, 5 is the sum and 1 is a carry that goes to the higher digit. • Similarly, in binary system – • 0 + 0 = 0 , no carry • 0 + 1 = 1 , no carry • 1 + 1 = 0 , 1 carry

  23. Addition -2 • Addition 67 + 98 (decimal) is done by • Adding 7 & 8 – result is 5 with a carry of 1 • Now, 6 & 9 are added along with the carry, resulting in 6, and carry of 1 • Hence, the result is 165.

  24. Addition -3 • Similarly, adding 0b10 to 0b11 is done by • Adding 0 & 1 – result is 1, no carry • Now, 1 & 1 are added, resulting in result of 0 and carry of 1 • Hence, the result is 0b101. • Similarly, adding 0b11 to 0b11 is done by • Adding 1 & 1 – result is 0, carry 1 • Now, 1 & 1 are added, along with the carry, resulting in result of 1 and carry of 1 • Hence, the result is 0b110.

  25. Subtraction -1 • Subtraction in decimal system can be carried out by a method called tens complement method. • Tens complement of 4 is 6. (sum is ten) • Tens complement of any number can be obtained by nines complement and adding one to it. • Nines complement of 4 is 5. (sum is nine) • Hence tens complement of 4 is 5+1 = 6.

  26. Subtraction -2 • Tens complement of a multi-digit number is obtained by – • Getting nines complement of individual digits • Adding one to that 9’s complement number • Hence to get tens complement of 64 • Get nines complement (it is 35) • Add one to the nines complement (result 36)

  27. Subtraction -3 • To subtract 64 from 86 – • Get tens complement of 64 (Tens complement is 36) • Add tens complement to 86. • Discard the carry generated in the result. • Hence 86 – 64 is converted into 86 + 36 • The sum is – 122 • Discarding the carry, the result is 22. • Verification (86 – 64 = 22)

  28. Subtraction -4 • Complement theory can be extended to binary arithmetic also. • In decimal we use 10’s complement. Here in binary arithmetic, we use 2’s complement. • 2’s complement of a number is obtained by getting one’s complement and then adding one to it.

  29. Subtraction -5 • One’s complement can be obtained by replacing 0 with 1, and 1 with 0. • i.e one’s complement of 0b1001 is 0b0110. • Now, two’s complement of 0b1001 is • 0110 + 0001 = 0111 • What’s the twos complement of 0b0000? • One’s complement is – 0b1111 • Two’s complement is – 0b1111 + 0b0001 = 0b1 0000 • Hence the result is 0b0000. (Discard carry generated.)

  30. Subtraction -6 • Assume subtracting 12 from 15. (Decimal) • The result is 15 – 12 = 03. • Convert the above numbers to binary. • They are 0b1100 (12) and 0b1111 (15) • One’s complement of 0b1100 is 0b0011 • Two’s complement of 0b1100 is 0b0100 • Adding 0b0100 to 0b1111, we get 0b1 0011 • Discarding the carry – the result is 0b0011, which is equivalent to 03.

  31. Subtraction -7 • Two’s complement method is used as – • It is easy to implement in systems • We can use addition circuits to do subtraction operation as well.

  32. Positive and Negative Numbers -1 • Till now we discussed about unsigned numbers only. • Using unsigned numbers, we did addition and subtraction. • However, we can look at subtraction as addition of a negative number. • Then, the question is – “How do we represent negative numbers?”

  33. Positive and Negative Numbers -2 • We said that, “We can look at subtraction as addition of a negative number.” • We also said that, “Adding a two’s complement of a number is same as subtraction of that number.” • Hence, we can deduce that – “Two’s complement of a number can be seen as a negative number!”

  34. Positive and Negative Numbers -3 • Here are a few four bit numbers, and their two’s complement numbers. Number  Two’s complement 0b0000 = +0  0b0000 = -0 (Same!) 0b0001 = +1  0b1111 = -1 0b0010 = +2  0b1110 = -2 0b0011 = +3  0b1101 = -3 0b0100 = +4  0b1100 = -4 0b0101 = +5  0b1011 = -5 Etc…

  35. Positive and Negative Numbers -4 • If a number 1234 is given, in that 1 is the most significant digit and 4 is the least significant digit. • Similarly in 0b 1010 – • 1 is the Most Significant Bit (MSB) & • 0 is the Least Significant Bit (LSB)

  36. Positive and Negative Numbers -5 • All positive numbers have 0 in MSB • All negative numbers have 1 in MSB • 0b1100 is a four bit representation for ‘-4’ • To get eight bit representation of ‘-4’ – • Take a eight bit representation of ‘4’, and get the corresponding 2’s complement. (or) • Take the four bit representation and extend the sign. • i.e. ‘-4’can be represented in eight bit as 0x1111 1100 • Verification. • 1’s complement of 0b0000 0100 is 0b1111 1011 • Hence, 2’s complement of 0b0000 0100 is 0b1111 1100 • Same as the one obtained by sign extension.

  37. Positive and Negative Numbers -6 • Four digits represent 16 different values, ranging from -8 … 0 … +7. • Hence four digits are representing values ranging from -23 ... 0 … +(23-1) • With ‘n’ bits, the range represented can be generalized to -2n-1 ... 0 … +(2n-1-1)

  38. Boolean Operators • NOT, AND, OR are the most important Boolean operators. • NOT : NOT 1 = 0 & NOT 0 = 1 (Opposite) • AND : 0 AND 0=0, 0 AND 1=0, 1 AND 0=0, 1 AND 1=1 (1 only if both are one) • OR : 0 OR 0=0, 0 OR 1=1, 1 OR 0=1, 1 OR 1=1 (1 only if any one of them is one)

  39. AND Operators • AND operator is extensively used later in the presentation. Let’s see a bit more about it. Let us represent it by ‘&’. • 1111 & 1111 = 1111 (F & F = F) • 1111 & 1101 = 1101 (F & D = D) • 1111 & 1100 = 1100 (F & C = C)

  40. Cobol Data Types and their Internal Representation

  41. EBCDIC Character Set • Before we get down to look at how data is stored in Cobol variables defined in an IBM Mainframe system, it is good to know some thing about the EBCDIC character set that is used to represent the values. • EBCDIC stands for ‘Extended Binary-Coded Decimal Interchange Code’.

  42. EBCDIC Character Set • 0x81 to 0x89 represent characters ‘a’ to ‘i’ • 0x91 to 0x99 represent characters ‘j’ to ‘r’ • 0xA2 to 0xA9 represent characters ‘s’ to ‘z’ • 0xC1 to 0xC9 represent characters ‘A’ to ‘I’ • 0xD1 to 0xD9 represent characters ‘J’ to ‘R’ • 0xE2 to 0xE9 represent characters ‘S’ to ‘Z’

  43. EBCDIC Character Set • 0xF0 to 0xF9 represent characters ‘0’ to ‘9’ • 0x4E represents ‘+’ (Plus symbol) • 0x60 represents ‘-’ (Minus symbol) • 0x40 represents ‘ ‘ (SPACE)

  44. EBCDIC Character Set • If you type a character ‘a’ on your keyboard, internally system stores 0x81. • Similarly, if you type a character ‘5’ on your keyboard, internally system stores 0xF5. • These are hexadecimal symbols for the character, in EBCDIC character set.

  45. Data Representation • Character (Alphabetic or alpha-numeric) data can be represented only in one way. That is, by storing the corresponding EBCDIC characters in the memory locations. • One character takes one byte in the memory. • PIC X(nn) & PIC A(nn) use this mechanism to represent and store data. • However, numeric data can be represented in many ways.

  46. Data Representation • Numeric data can be classified as Integer & Decimal Data. • 10, 34, 3456 are integers • 1.234, 12.34, 1234.56 are decimal numbers • Both Integer & Decimal data can be represented in many ways.

  47. Integer Data Representation • One of the ways of representing Integer data is by storing the corresponding EBCDIC characters in memory. • PIC 9(nn) uses this method to store data. • In this method - • One digit takes one memory location • To store a value 89, 0xF8 F9 is stored in memory. (If 9(2) is the PIC clause) • To store a value 89, 0xF0 F8 F9 is stored in memory. (If 9(3) is the PIC clause) • PIC 9(nn) is an unsigned data representation.

  48. Integer Data Representation • In this format how negative numbers are stored? • Assume we would like to store a negative number. We may use – • S9(02) • S9(02) SIGN TRAILING • S9(02) SIGN LEADING • S9(02) SIGN TRAILING SEPARATE • S9(02) SIGN LEADING SEPARATE • S9(02) is same as S9(02) SIGN TRAILING, as ‘sign trailing’ is the default.

  49. Integer Data Representation • Assume we would like to store, ‘89’, which is a positive number. ’89’ is represented by 0xF8F9. • In S9(02), it is stored as ‘0xF8C9’. (2 bytes). Least Significant Byte 0xF9 is ANDed with 0xCF resulting in 0xF8C9. • In S9(02) SIGN TRAILING, it is stored as ‘0xF8C9’. (2 bytes)… sign represented in the least significant byte. Least Significant Byte 0xF9 is ANDed with 0xCF resulting in 0xF8C9. • In S9(02) SIGN LEADING, it is stored as ‘0xC8F9’. (2 bytes)… sign represented in most significant byte. Most Significant Byte 0xF8 is ANDed with 0xCF resulting in 0xC8F9.

  50. Integer Data Representation • In S9(02) SIGN TRAILING SEPARATE, it is stored as ‘0xF8F94E’. (3 bytes) … sign represented separately. Sign takes additional byte in memory. • In S9(02) SIGN LEADING SEPARATE, it is stored as ‘0x4EF8F9’. (3 bytes) … sign represented separately. Sign takes additional byte in memory.

More Related