Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 2810-2•What is the difference between machine code and C?•Why use C?•How do execution of C and Matlab programs differ?•What are three methods of expressing a program algorithm?•Related Chapters: ABC Chapters 0 & 1 & 11.3Main Memory(RAM)Input DevicesOutput DevicesAuxiliary StorageCPUControlUnit (CU)Arithmetic-Logic Unit (ALU)Memory Addresses0 - 3 4 - 7100 -107108 -109110 -121..instr1instr2 -75label....101.101System Unit10-4Main (Internal) Memory:• All data and instructions are stored in main memory as a sequence of 0’s and 1’s called Bits (Binary Digits)• Byte (smallest addressable element of memory) Storage size for one character of information (ASCII-8 8 bits). Every (single) address refers to a byte in memory.•210 bytes is defined as 1 kilo-byte (1KB)220 bytes is defined as 1 mega-byte (1MB)230 bytes is defined as 1 giga-byte (1GB)10-5Central Processing Unit (CPU):• Transfers information into, out of, and between memory locations.• Executes instructions stored in memory.•Set of instructions for a CPU is known as a machine language.•Each CPU (Intel Core i7, IBM PowerPC, ...) has its own specific machine language.Main MemoryMain MemoryControl UnitArithmetic/Logic Unit1234Instruction CycleExecutionCycleFetch DecodeExecuteStoreCPURAMIncludes Cache(very fast memory)10-7Classified asLow Level• Machine Language(binary-based code; machine dependent)• Assembly Language(mnemonic form of machine language)10-8High Level• Closer to natural languages.• Generally, machine independent• Usually, several machine instructions are combined into one high-level instruction.• Examples:FORTRAN COBOL BASIC JavaPython Ada PL/I LispC GPSS C++ Matlab10-9 To illustrate differences in syntax for language levels, consider how a computer could be instructed to subtract two numbers stored in memory address locations 64 and 2048 and put the result in location 2048:incrementvalue642048Memory AddressesVariable Names10-10• Machine Language:0111110000000100000000100000000000(6-bit OpCode, 14-bit address fields with values 64 and 2048)valueincrement642048Memory AddressesVariable Names10-11• Assembly Language:S increment,value• C Language:value = value - increment;valueincrement642048Memory AddressesVariable Names10-12Programs written in high-level languages must be converted to machine language.Two approaches:(1) Compilation (see p. 28 FER Figure 2.4)Used with C, C++, Fortran,...(2) InterpretationUsed with Matlab, Visual Basic,...Step 1) Use editor to create a “source” file.We will name source files with a suffix “.c”Step 2) Run the gcc compiler on the source file to create anexecutable or object file with the default name a.out . For CS101 we will only create executable files. Step 3) Check to see if gcc caught any syntax errors , if so go back to step 1)Step 4) Run the program by typing> ./a.outat the Unix prompt10-14Computer programming is the art/science of transforming a “real world” problem into a finite sequence of instructions(statements) in a computer language.The method stated in the following slide will be used throughout the rest of the semester. Follow this method in your Labs and for any Machine Problem.10-151. Requirements Specification (Problem Definition)2. Analysis---Refine, Generalize, Decompose the problem definition(i.e., identify sub-problems, I/O, etc.)3. Design---Develop Algorithm(processing steps to solve problem)Use one of the following:Natural-Language AlgorithmFlowchart AlgorithmPseudo-code Algorithm4. Implementation --- Write the "Program" (Code) 5. Verification and Testing --- Test and Debug the Code10-161. Requirements Specification (Problem Definition)Given a light-bulb with a pre-measured power consumption(in watts), compute the resistance (in ohms) of the bulb.2. Analysis---Refine, Generalize, Decompose the problem definition(i.e., identify sub-problems, I/O, etc.) Input = real number representing powerOutput=real number representing resistance10-173. Design---Develop AlgorithmNatural-Language AlgorithmPrompt user for the power dissipationRead powerStore value in storage location called power.Compute the resistance solving the formula“power = (voltage*voltage)/resistance” in terms of resistance. resistance = (voltage * voltage)/ powerPrint out the value stored in location resistance.10-183. Design---Develop AlgorithmPseudo-code Algorithmprint “enter power in watts”read powerresistance = (117.0 * 117.0)/powerprint resistancestartCompute: resistance=(117*117)/powerOutput::resistancestop 3. Design---Develop AlgorithmFlowchart AlgorithmInput : power Output : “enter power in watts”Flow is assumed down unless otherwise specified with an arrow. Trapezoid used to designate I/O.Rectangle used to designate one or more statements in a block.Circle used as continuation symbol for transfer to another page.10-204. Implementation --- Write the "Program" (Code) (see the next slide) The lecture slides show the use of the gediteditor but the choice of editor is not critical.C Code Implementation of the Algorithm/* C Program to compute the resistance *//* of a light-bulb.*/#include <stdio.h>#define VAC 117.0void main(void){ /* Declare variables. */ float power, resistance; /* request user input power of */ /* light-bulb in watts. */ printf(”Please enter power(watts) :”); /* read value power */ scanf("%f", &power); /* Compute resistance assuming VAC = 117. */ resistance = (VAC * VAC) /power; /* Output the calculated resistance. */ printf(”Resistance is %f (ohms)\n", resistance);}(Note indentation scheme in above code.)C program compilationOpen gedit and enter your code. Note the & .C program compilationClick the “Save” button in gedit to save your code but don’t close the gedit window. Click on the xterm window and type the command to compile your code.Use the “gcc” program to compile your C source code in the file “resistance.c”.> ls resistance.c resistance.c~Note: backup files begin and/or end with a ~ or a # symbol. Do not edit or compile these files!> gcc resistance.cThe “gcc” program will not alter the file “resistance.c”. The output of “gcc” is by default contained in the file “a.out”. The file “a.out” is called an executable
View Full Document