What s a Computer Program? Descrpton of algorthms and data structures to acheve a specfc ojectve Could e done n any language, even a natural language lke Englsh Programmng language: A Standard notaton for wrtng programs Eamples: C, Java, Intel assemly language An etreme eample: Machne language Hence the need for program translators Eample: gcc Hgh level vs Low Level Englsh: To calculate the smple nterest, multply the prncpal amount y the rate of nterest y the numer of years C: W = X * Y * Z; Even etter C statement: SmpIeInterest = Prncpal * Rate * Years; 6 Hgh level vs Low Level Machne language: (transformed to a human readale form) movss (%rp), %mm movss y(%rp), %mm mulss %mm, %mm movss z(%rp), %mm mulss %mm, %mm movss %mm, w(%rp) C program s made up of statements Machne language program s made up of machne nstructons What does gcc do for you? % gcc hello.c hello.c source : Program that you wrote n the C language and typed nto the fle hello.c gcc a.out eecutale : Fle generated y gcc. The a.out fle contans an equvalent program n machne language that can e eecuted on a computer system 9 Basc Computer Organzaton Processor or CPU Memory The `rans of the computer system Program = Instructons + Data We wll net learn more aout data I/O Bus I/O I/O Where thngs are rememered program, data
There are dfferent knds of data How does one pece of data dffer from another? Constant vs Varale Basc vs Structured Of dfferent types Character Integer (unsgned, sgned) Real Others (oolean, comple, ) Data dfferng n ther lfetmes Lfetme: Interval etween tme of creaton and end of estence How long can the lfetme of a datum e? We wll consder 3 possle lfetmes 3 Program Data: Dfferent Lfetmes.. Lfetme = Eecuton tme of program Intalzed/unntalzed data Must e ndcated n eecutale fle The space n memory for all of ths data can e assgned when program eecuton starts (Statc Allocaton) Program Data: Dfferent Lfetmes.. Lfetme = Eecuton tme of program. Lfetme = Tme etween eplct creaton of data & eplct deleton of data Dynamc memory allocaton In C you create new data usng a functon lke malloc() The memory space for ths data s managed dynamcally when the malloc/free s eecuted (Heap allocaton) 4 5 Program Data: Dfferent Lfetmes... Lfetme = Eecuton tme of program. Lfetme = Tme etween eplct creaton of data & eplct deleton of data 3. Lfetme = Durng eecuton of a functon (.e., tme etween functon call and return) Local varales, parameters of the functon The memory space for ths data s assgned when the functon s called and reclamed on return from the functon (Stack allocaton) Stack: Lke a ple of ooks on a tale 6 Stack allocated: Functon Local Varales When the program starts eecutng Top of Stack Ponter Local Varales of man What f man() then calls functon func()? 7
Stack allocated: Functon Local Varales. Whle eecutng n functon func() Stack allocated: Functon Local Varales.. Eecutng n man() once agan Local varales of func Local Local Varales of of man Top of Stack Ponter Local Varales of man Top of Stack Ponter What happens on return from the call to func()? 8 9 Recurson vs Iteraton Eample: Compute N!, N > j fact N { nt N, fact, j; j++) for (fact=, j=; j <= N; fact = fact * j; J= J= J=98 J=99 N J= nt factoral (nt j) { f (j == ) return(); )); vod man() { nt N; return (j * factoral(j- factoral(n); Durng program eecuton Code (machne language program) Data (ntalzed and unntalzed) Code and Data don t change n sze whle the program s eecutng Heap (for dynamcally allocated data) Stack (for functon local varales) Heap and Stack change n sze as program eecutes Code Intalzed Unntalzed Heap Stack Dgtal Computers As opposed to mechancal computers or analog computers Baage s Analytcal Engne Mechancal computers used mechancal parts lke gears and levers to calculate wkpeda 3
Analog Computer Dgtal Computers.. In current dgtal computers, values vary etween dscrete values and hgh and low Volts and 3 Volts Analog computers used contnuously varyng quanttes lke voltage In dgtal computers, dscretely varyng values are used nstead http://userwww.sfsu.edu/~hl/c.heath.analog.html 5 Dgtal Crcuts Electrcal crcuts n whch voltages only take on a dscrete numer of values There are dgtal crcuts that can do calculatons, others that can e used to rememer values, etc 6 Eample: Bnary Addton Bnary: Base numer system You can generalze the decmal numer system, whch uses dgts (,,,9) to work wth any rad or ase ( d n n nd n... ddd ) ten d n dn... d d d n d Bnary dgts (called ts): and ( n n nn... ) two n n... n Eample: Decmal 47 s Bnary Bnary Addton.. t adder A crcut that can add one t to another t The dfferent cases + + + + = = =??? = = = = One t adder has nputs and outputs a t adder s c Bnary Addton.. t adder A crcut that can add one t value (a a ) to another t value ( ) s s Full t adder a a + s s s a s c t adder Ths dea can e etended to desgn a 3 t adder or a 64 t adder a 4
How s Data Represented? On a dgtal computer Bnary Base numer system Two values: and Bt (Notaton: ); Byte (Notaton: B) 8 ts Other notaton: K, M, G, T, P etc K:, M:, G: 3, etc G =,73,74,84 GB of RAM, TB hard dsk drve Character Data Typcally represented usng the ASCII code ASCII: Amercan Standard Code for Informaton Interchange Each character s represented y a unque 8 t ASCII code word Eample: a s represented y, s represented y 3 3 How s Data Represented? Character data: ASCII code Integer data In computer systems, you usually fnd support for oth sgned ntegers and unsgned ntegers e.g., C programmng nt ; Can take +ve or -ve whole numer values unsgned nt y; Can take on +ve whole numer values Unsgned Integer Data Representaton: Bnary numer system ( n n nn... ) two n n... n e.g., Decmal s represented as.e. n 6 ts n 3 ts 3 33 Asde: << C << operator e.g., y = << 3; Left Shft y 3 ts Shfts each t to the left y 3 ts The 3 ts on the etreme left go away 3 Zero s come n at the rght etreme end e.g., n 8 ts, << 3 s Clam: Shftng an unsgned nt left y one t s the same as multplcaton y Qualfcaton: If the product can e computed.e., all of the ts that go away are Zero Asde: <<.. Proof: Consder n t value I = ( nn... ) Shfted left y t and n- = we get ( nn3... ) n I n two two 34 5
Sgned Integer Data s Complement Representaton The n t quantty n n... represents the sgned nteger value n n n least sgnfcant t Eample: In 8 ts -8 + 64 + 3 + 6 + + 3 s represented as -3 s represented as 36 How s Data Represented? Character data: ASCII code Sgned Integer data: s complement Real data 37 Real data Real numers: ponts on the nfntely long real numer lne There are an nfntely many ponts etween any two ponts on the real numer lne Real Data: Floatng Pont Representaton IEEE Floatng Pont Standard (IEEE 754) 3 t value wth 3 components ( s, e, f ). s ( t sgn). e (8 t eponent) 3. f (3 t fracton) represents the value s ( ). f e7 38 39 Eample: IEEE Sngle Float Consder the decmal value.5 Equal to. n nary. s ( ). f s:, e: 6, f: e7 In 3 ts, 4 Basc Computer Organzaton Man parts of a computer system: Processor: Eecutes programs Man memory: Holds program and data I/O devces: For communcaton wth outsde Machne nstructon: Descrpton of prmtve operaton that machne hardware s ale to eecute e.g. ADD these two ntegers Instructon Set: Complete specfcaton of all the knds of nstructons that the processor hardware was ult to eecute 4 6
Basc Computer Organzaton Processor or CPU Memory Bus I/O I/O I/O 4 Asde: Aout Memory What s memory? Devces that can rememer thngs There are dfferent knds of memory n a computer system Some rememer y the state an electrcal crcut s n e.g., SRAM Others rememer y the amount of electrcal charge stored n a capactor e.g., DRAM Memory Yet others rememer y magnetc or optcal propertes e.g., Hard dsk drve/mag Tape, CD/DVD They can vary sustantally n ther speed and capacty 43 Man Memory. Holds nstructons and data Vew t as a sequence of fed szed locatons, each referred to y a unque memory address In many computers, the sze of each memory locaton s Byte,73,74,83 (f memory s of sze GB) 8 ts 3 Man Memory.. Values that occupy more than 8 ts would occupy more than one, neghourng memory locatons e.g., 3 t sgned nteger would occupy 4 neghourng memory locatons, maye as shown 3,73,74,83 8 ts 44 + + +3 char A[4] A[] A[] A[] A[3] Data Structures: Arrays rows char B[4][] columns B[][] B[][] B[][] B[][] B[][] B[][] 3 B[3][] B[3][] + + +3 +4 +5 +6 +7 B[][] B[][] B[][] B[][] B[][] B[][] B[3][] B[3][] struct node { char data; Data Structures: Lnked Lsts struct node *net; / a ponter: 3 ts n sze struct node *head; / a character: 8 ts n sze / a ponter: 3 ts n sze / n memory at address < z z > 7