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

• View
216

3

Embed Size (px)

Transcript

• *Representing Information (1)

Compilers Autumn 2002

• *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

• *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.

• *Why Bit?Using decimal notation is natural for ten-fingered humansBut binary values work better when building machines that store and process information

• *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.

• *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

• *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)

• *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

• *Value of BitsBits 01010Value 0*24+1*23+0*22+1*21+0*20 = 10

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)

• *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

• *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

• *int is not integerOverflow200*300*400*500 = -884,901,888Product of a set of positive numbers yielded a negative resultCommutativity & Associativity remain(500 * 400) * (300 * 200)((500 * 400) * 300) * 200((200 * 500) * 300) * 400400 * (200 * (300 * 500))

• *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

• *HexadecimalBase 16 number representationUse characters 0 to 9 and A to FWrite FA1D37B16 in C as 0xFA1D37B or0xfa1d37b

• *HexadecimalByte = 8 bitsBinary 000000002 to 111111112Decimal: 010to 25510Hexadecimal 0016 to FF16

• *Hexadecimal vs. Binary0x173A4CHexadecimal 1 7 3 A4CBinary 0001 0111 0011 1010 0100 1100

1111001010110110110011Binary 11 1100 1010 1101 1011 0011Hexadecimal 3 C A D B 30x3CADB3

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

1100100101111011 ->0xD5E4C -> 1001101110011110110101 ->0011 1001 1010 0111 11111 000C97B1101 0101 1110 0100 11002 6 E 7 B 5

• *C Programming Language (1)

Compilers Autumn 2002

• *OutlineSample codesIntroductionCompiler driversAssembly code and object codeSuggested reading1.2, 2.1.3

• *Hello world example1 #include 23 int main()4 {5 printf("hello, world\n");}

• *File Inclusion and Macro Substitutionint j ;

main(){ return 100 + j ;}

a.c#include b.h

main(){ return i+j ;}b.h#define i 100int j ;

• *File Inclusion and Macro Substitution#include filename#include

#define forever for(;;) /* infinite loop */#define square(x) (x)*(x)

• *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

C DeclarationTypical sizechar1 byteshort2 bytesint4 byteslong long8 bytes

• *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

• *The Hello Program

• *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

• *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

• *The Hello Program

• *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

• *The Context of a Compiler (gcc)Source program (text)hello.c

• *PreprocessorObtain with commandgcc -E a.cSource file a.i

# 1 "a.c"# 1 ""# 1 ""# 1 "a.c"# 1 "b.h" 1

int j;# 2 "a.c" 2

main(){ return 100 +j ;}

a.c#include b.h

main(){ return i+j ;}b.h#define i 100int j ;

• *Source Code and Assembly Code

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

Obtain with commandgcc O2 -S code.cAssembly file code.s

• *Machine Code

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

• COMPILING*EcoS

• *Standardization of CThe original Bell Labs version of C the 1st edition of the book K&RThe ANSI C standard in 1989 The American National Standards InstituteModify the way functions are declared the 2nd edition of the book K&RISO C90 (The International Standards Organization)

• *Standardization of CISO C99Introduced some new data types Provided support for text strings requiring characters not found in the English languageGcc supportingUnix> gcc -std=c99 prog.c -ansi and -std=c89 have the same effect

• *Standardization of CACM: Association of Computing MachineryIEEE: Institute of Electrical and Electronics Engineers

C version gcc command line optionGNU 89none, -std=gnu89ANSI, ISO C90 -ansi, -std=c89ISO C99 -std=c99GNU 99 -std=gnu99

*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