1 Representing Information (1). 2 Outline Bit and Byte Understand machine representations of numbers Suggested reading –1.1, 2.1.1.

Download 1 Representing Information (1). 2 Outline Bit and Byte Understand machine representations of numbers Suggested reading –1.1, 2.1.1.

Post on 04-Jan-2016

216 views

Category:

Documents

3 download

TRANSCRIPT

<ul><li><p>*Representing Information (1)</p><p>Compilers Autumn 2002</p></li><li><p>*OutlineBit and ByteUnderstand machine representations of numbersSuggested reading1.1, 2.1.1</p></li><li><p>*Why Bit?Modern computers store and process Information represented as two-valued signalsThese lowly binary digits are bitsBits form the basis of the digital revolution</p></li><li><p>*The Decimal Representation Base-10Has been in use for over 1000 yearsDeveloped in IndiaImproved by Arab mathematicians in the 12th centuryBrought to the West in the 13th century by the Italian mathematician Leonardo Pisano, better known as Fibonacci. </p></li><li><p>*Why Bit?Using decimal notation is natural for ten-fingered humansBut binary values work better when building machines that store and process information</p></li><li><p>*Why Bit?Two-valued signals can readily be represented, stored, and transmitted, ExamplesThe presence or absence of a hole in a punched cardA high or low voltage on a wireA magnetic domain oriented clockwise or counterclockwise.</p></li><li><p>*Why Bit?The electronic circuitry is very simple and reliable for storing and performing computations on two-valued signalsThis enabling manufacturers to integratemillions of such circuits on a single silicon chip</p></li><li><p>*Group BitsIn isolation, a single bit is not very usefulIn English, there are 26(or 52) characters in its alphabet. They are not useful either in isolationHowever, there are plenty of words in its vocabulary. How is this achieved? Similarly, we are able to represent the elements of any finite set by using bits (instead of bit)</p></li><li><p>*Group BitsTo do this, wefirst group bits together then apply some interpretation to the different possible bit patternsthat gives meaning to each patterns8-bit chunks are organized as a byteDr. Werner Buchholz in July 1956during the early design phase for the IBM Stretch computer</p></li><li><p>*Value of BitsBits 01010Value 0*24+1*23+0*22+1*21+0*20 = 10</p><p>Value102(1100110)Bits102 = 51*2 + 0 (0)51 = 25*2 + 1 (1)25 = 12*2 + 1 (1)12 = 6*2 + 0 (0) 6 = 3*2 + 0 (0) 3 = 1*2 + 1 (1) 1 = 0*2 + 1 (1)</p></li><li><p>*Group bits as numbers Three encodingsUnsigned encodingRepresenting numbers greater than or equal to 0Using traditional binary representationTwos-complement encodingMost common way to represent either positive or negative numbersFloating point encodingBase-two version of scientific notation for representing real numbers</p></li><li><p>*Group bits as numbers Understanding numbersMachine representation of numbers are not same as Integers and real numbers They are finite approximations to integers and real numbersSometimes, they can behave in unexpected way</p></li><li><p>*int is not integerOverflow200*300*400*500 = -884,901,888Product of a set of positive numbers yielded a negative resultCommutativity &amp; Associativity remain(500 * 400) * (300 * 200)((500 * 400) * 300) * 200((200 * 500) * 300) * 400400 * (200 * (300 * 500))</p></li><li><p>*float is not real numberProduct of a set of positive numbers is positiveOverflow and UnderflowAssociativity does not hold(3.14+1e20)-1e20 = 0.03.14+(1e20-1e20) = 3.14</p></li><li><p>*HexadecimalBase 16 number representationUse characters 0 to 9 and A to FWrite FA1D37B16 in C as 0xFA1D37B or0xfa1d37b</p></li><li><p>*HexadecimalByte = 8 bitsBinary 000000002 to 111111112Decimal: 010to 25510Hexadecimal 0016 to FF16</p></li><li><p>*Hexadecimal vs. Binary0x173A4CHexadecimal 1 7 3 A4CBinary 0001 0111 0011 1010 0100 1100</p><p>1111001010110110110011Binary 11 1100 1010 1101 1011 0011Hexadecimal 3 C A D B 30x3CADB3</p></li><li><p>*Hexadecimal vs. DecimalHexadecimal 0xA7Decimal 10*16+7 = 167</p><p>Decimal 314156 = 19634*16 + 12 (C) 19634 = 1227*16 + 2 (2) 1227 = 76*16 + 11 (B) 76 = 4*16 + 12 (C) 4 = 0*16 + 4 (4)Hexadecimal0x4CB2C </p></li><li><p>*Hexadecimal vs. Binary0x39A7F8 -&gt;</p><p>1100100101111011 -&gt;0xD5E4C -&gt; 1001101110011110110101 -&gt;0011 1001 1010 0111 11111 000C97B1101 0101 1110 0100 11002 6 E 7 B 5</p></li><li><p>*Decimal, Hexadecimal, BinaryDecimalBinaryHexadecimal167621880011 01111000 10001111 00110x520xAC0xE70xA7101001110x3E00111110101111000xBC0x370x880xF33*16+7=558*16+8=13615*16+3=243010100105*16+2=821010110010*16+12=1721110011114*16+7=231</p></li><li><p>*C Programming Language (1)</p><p>Compilers Autumn 2002</p></li><li><p>*OutlineSample codesIntroductionCompiler driversAssembly code and object codeSuggested reading1.2, 2.1.3</p></li><li><p>*Hello world example1 #include 23 int main()4 {5 printf("hello, world\n");}</p></li><li><p>*File Inclusion and Macro Substitutionint j ;</p><p>main(){ return 100 + j ;}</p><p>a.c#include b.h</p><p>main(){ return i+j ;}b.h#define i 100int j ;</p></li><li><p>*File Inclusion and Macro Substitution#include filename#include </p><p>#define forever for(;;) /* infinite loop */#define square(x) (x)*(x)</p></li><li><p>*Integral data types in CThere are several data types to represent integers in CThe difference is the size of the integerIt reflects the implementation of hardware</p><p>C DeclarationTypical sizechar1 byteshort2 bytesint4 byteslong long8 bytes</p></li><li><p>*Formatted Output - Printfint printf(char* format, arg1, arg2, )printf(%d, %d\n, i, j) ;%d, %i decimal number%o octal number(without a leading zero)%x, %X hexadecimal number%c single character</p></li><li><p>*The Hello Program</p></li><li><p>*The Hello ProgramSource programCreated by editor and saved as a text file (Consists exclusively of ASCII characters)Binary fileNot text fileBegins life as a high-level C programCan be read and understand by human beings The individual C statements must be translated by compiler driversSo that the hello program can run on a computer system</p></li><li><p>*ASCII American Standard Code for Information InterchangeControl Characters07 \a Bell 08 \b Backspace09 \t Horizontal Tab0A \n Line feed 0B \v Vertical Tab 0C \f Form feed 0D \r Carriage return</p></li><li><p>*The Hello Program</p></li><li><p>*The Hello ProgramThe C programs are translated into A sequence of low-level machine-language instructionsThese instructions are then packaged in a form called an object programObject program are stored as a binary disk fileAlso referred to as executable object files</p></li><li><p>*The Context of a Compiler (gcc)Source program (text)hello.c</p></li><li><p>*PreprocessorObtain with commandgcc -E a.cSource file a.i</p><p># 1 "a.c"# 1 ""# 1 ""# 1 "a.c"# 1 "b.h" 1</p><p>int j;# 2 "a.c" 2</p><p>main(){ return 100 +j ;}</p><p>a.c#include b.h</p><p>main(){ return i+j ;}b.h#define i 100int j ;</p></li><li><p>*Source Code and Assembly Code</p><p>int accum = 0;int sum(int x, int y){ int t = x+y; accum += t; return t;}_sum:pushl %ebpmovl %esp,%ebpmovl 12(%ebp),%eaxaddl 8(%ebp),%eax addl %eax, accummovl %ebp,%esppopl %ebpret</p><p>Obtain with commandgcc O2 -S code.cAssembly file code.s</p></li><li><p>*Machine Code</p><p>55 89 e5 8b 45 0c 03 45 08 01 05 00 00 00 00 89 ec 5d c3Obtain with commandgcc O2 -c code.cRelocatable object file code.o</p></li><li><p>COMPILING*EcoS</p></li><li><p>*Standardization of CThe original Bell Labs version of C the 1st edition of the book K&amp;RThe ANSI C standard in 1989 The American National Standards InstituteModify the way functions are declared the 2nd edition of the book K&amp;RISO C90 (The International Standards Organization)</p></li><li><p>*Standardization of CISO C99Introduced some new data types Provided support for text strings requiring characters not found in the English languageGcc supportingUnix&gt; gcc -std=c99 prog.c -ansi and -std=c89 have the same effect</p></li><li><p>*Standardization of CACM: Association of Computing MachineryIEEE: Institute of Electrical and Electronics Engineers </p><p>C version gcc command line optionGNU 89none, -std=gnu89ANSI, ISO C90 -ansi, -std=c89ISO C99 -std=c99GNU 99 -std=gnu99</p><p>*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87W 87*W 87*W 87*W 87</p></li></ul>

Recommended

View more >