Lecture 10
Advertisement
This presentation is the property of its rightful owner.
1 / 28

Lecture 10 PowerPoint PPT Presentation

Lecture 10. What will I learn in this lecture?. 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: FER Chapters 1 & 2 . Digital Computer Hardware.

Download Presentation

Lecture 10

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Lecture 10 10 2 what is the difference between machine

Lecture 10


Lecture 10 10 2 what is the difference between machine

What will I learn in this lecture?

  • 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: FER Chapters 1 & 2


Lecture 10 10 2 what is the difference between machine

Digital Computer Hardware

Input Devices

instr1

0 - 3

Control

Unit (CU)

4 - 7

instr2

.

.

.

.

.

.

Arithmetic

-Logic Unit

(ALU)

Auxiliary Storage

100 -107

101.101

108 -109

-75

110 -121

label

Output Devices

CPU

Main Memory(RAM)

Memory Addresses

System Unit


Lecture 10 10 2 what is the difference between machine

Digital Computer Hardware

Main (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)


Lecture 10 10 2 what is the difference between machine

Digital Computer Hardware

  • Central 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 x86-64, IBM PowerPC, ...) has its own specific machine language.


Lecture 10 10 2 what is the difference between machine

Machine Cycle

Instruction

Cycle

Control Unit

Main Memory

1

Fetch

Decode

2

Execution

Cycle

CPU

RAM

3

Execute

Store

4

Arithmetic/Logic Unit

Includes Cache

(very fast memory)


Lecture 10 10 2 what is the difference between machine

Programming Languages

Classified as

  • Low Level

    • Machine Language

    (binary-based code; machine dependent)

    • Assembly Language

    (mnemonic form of machine language)


Lecture 10 10 2 what is the difference between machine

Programming Languages

  • High Level

    • Closer to natural languages.

    • Generally, machine independent

    • Usually, several machine instructions are combined into one high-level instruction.

    • Examples:

    FORTRAN COBOLBASIC Java

  • Pascal AdaPL/I Lisp

  • C GPSSC++ Matlab


Lecture 10 10 2 what is the difference between machine

Language Comparison

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:

Variable Names

increment

64

Memory Addresses

value

2048


Lecture 10 10 2 what is the difference between machine

Language Comparison

Variable Names

increment

64

Memory Addresses

value

2048

• Machine Language:

0111110000000100000000100000000000

(6-bit OpCode, 14-bit address fields with values 64 and 2048)


Lecture 10 10 2 what is the difference between machine

Language Comparison

VariableNames

increment

64

MemoryAddresses

value

2048

• Assembly Language:

S increment,value

• C Language:

value = value - increment;


Lecture 10 10 2 what is the difference between machine

Processing a High-Level Language Program

Programs 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,...


Lecture 10 10 2 what is the difference between machine

Compilation

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 prompt


Lecture 10 10 2 what is the difference between machine

Problem Solving and C

Computer 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.


Lecture 10 10 2 what is the difference between machine

Software Development Method

(these vary just slightly from the method described on p.37 in FER)

1. 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 Algorithm

4. Implementation --- Write the "Program" (Code)

5. Verification and Testing --- Test and Debug the Code


Lecture 10 10 2 what is the difference between machine

C Program Example

1. 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 power

Output=real number representing resistance


Lecture 10 10 2 what is the difference between machine

C Program Example

3. Design---Develop Algorithm

Natural-Language AlgorithmPrompt user for the power dissipation

Read power

Store value in storage location called power.

Compute the resistance solving the formula

“power = (voltage*voltage)/resistance” in terms of resistance.

resistance = (voltage * voltage)/ power

Print out the value stored in location resistance.


Lecture 10 10 2 what is the difference between machine

C Program Example

3. Design---Develop Algorithm

Pseudo-code Algorithm

print “enter power in watts”

read power

resistance = (117.0 * 117.0)/power

print resistance


Lecture 10 10 2 what is the difference between machine

3. Design---Develop Algorithm

Flowchart Algorithm

start

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.

Input : power

Compute: resistance=(117*117)/power

Output::resistance

stop


Lecture 10 10 2 what is the difference between machine

C Program Example

4. Implementation --- Write the "Program" (Code) (see the next slide)

The lecture slides show the use of the Xemacseditor but the choice of editor is not critical. In labs we will use the Eclipse editor.


Lecture 10 10 2 what is the difference between machine

C Code Implementation of the Algorithm

/* C Program to compute the resistance */

/* of a light-bulb.*/

#include <stdio.h>

#define VAC 117.0

void 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.)


Lecture 10 10 2 what is the difference between machine

Open Xemacs and enter

your code..

C program compilation


Lecture 10 10 2 what is the difference between machine

C program compilation

Click the “Save” button in Xemacs to save your code but don’t close the Xemacs window.

Click the xterm button to open another window.


Lecture 10 10 2 what is the difference between machine

C program compilation

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.c

The “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 file. (continued on next slide)


Lecture 10 10 2 what is the difference between machine

C program compilation

Note that the “gcc” program noted an error on line 16 in the program and “gcc” generated a warning for line 7.

Also, note that there is no a.out file listed. Errors cause gcc to abort and you will not get an a.out file.

Go back to Xemacs line 16 and fix the syntax error.

(continued on next slide)


Lecture 10 10 2 what is the difference between machine

The error on line 16 was actually caused by a missing semicolon on line 14. In C a semicolon means “end of statement”. This differs from Matlab where a semicolon means suppress output and is optional. Since there was no semicolon after line 14, C assumed that lines 14 - 16 represented one C statement. That is incorrect syntactically, so gcc (the compiler program or compiler for short) generated an error message and terminated before producing an a.out file.


Lecture 10 10 2 what is the difference between machine

C program compilation

After adding a semicolon to the end of line 14, Click “Save” in Xemacs and go back to the xterm and type again...

Note: The dot-slash in “ ./a.out ” means “ in this working directory”. If you type “ a.out ” at the Unix prompt Unix will first search your home directory for a file “ a.out ”. Of course this would not be the same “ a.out ” file you created in another directory.


Lecture 10 10 2 what is the difference between machine

What have I learned in this lecture?

A CPU only executes machine code which consists of instructions that are specific to a particular manufacturer. The machine code is a sequence of ones and zeros.

The C language is more human-language-like than machine code and can be converted to machine code by using a compiler (gcc in CS101). C code is more portable than machine code.

In the design and development of an algorithm we can use any combination of “Natural Language”, “Pseudo-code” and “Flow-chart” .

Matlab code is executed by means of an interpreter that is running in the Matlab “environment”. Each time the Matlab code runs the interpreter converts the code into machine code.

C code is compiled into an executable file (machine code) once. We then run the program (default name a.out) by typing the name of the executable file (a.out)_at the Unix prompt.


  • Login