Programming Languages

Download Report

Transcript Programming Languages

ECE 103 Engineering Programming
Chapter 5
Programming Languages
Herbert G. Mayer, PSU CS
Status 6/10/2016
Initial content copied verbatim from
ECE 103 material developed by
Professor Phillip Wong @ PSU ECE
Syllabus







What’s This Blue Code?
Introduction
Computer Architecture Overview
Machine Language
Assembly Language
High-Level Language
C Programming Language
What’s This?
// Assume non-negative args. Should be “unsigned”
int fact( int arg )
{ // fact
if ( arg <= 1 ) { // fact(0) = fact(1) = 1
return 1;
}else{
return fact( arg – 1 ) * arg;
} //end if
} //end fact
int main( /* no params */ )
{ // main
printf( “Factorial %d = %d\n”, 10, fact( 10 ) );
return 0;
} //end main
2
Introduction

What is a program?

Software that runs on an standard computer


Word processor, database engine, web browser, video
game, graphics package, simulator, etc.
Where else are programs found?
Embedded – Microwave oven, engine controller, media
player, thermostat, pacemaker, toy, etc.
 Electromechanical – Early computers (e.g. ENIAC)
 Mechanical – Jacquard loom (1801), Babbage analytical
engine (1837)
 Biological – Instinctive and learned behaviors

3
What do programs do?


Perform “useful” computation or IO
Process information
Input  Program  Output

Programs may require input (but not always)


numeric or text input, sensor values, etc.
Programs may generate output (but not always)

text, graphics, actions such as triggering devices
4

How are programs created?
1.
2.
Develop an algorithm for the solution in your head
Write a program via a computer file; textual

Programming languages provide syntax for
implementing algorithms on a computer

Traditional approaches:
 Machine language
 Assembly language
 High-level languages, generally machine-
independent
5
Classic Computer Architecture Overview
Simplified computer model:
Main Memory
(RAM, ROM)
Input
Processor
Output
(e.g., keyboard, mouse)
(CPU)
(e.g., monitor, printer)
Auxiliary Storage
(e.g., disk drives)
6

The processor executes the program's instructions.
Instruction
Decoder
Memory
Interface
To main
memory
Registers
(fast memory)
ALU
The ALU (Arithmetic Logic Unit) performs basic
arithmetic, logic, and comparison operations.
7

Program instructions and data are stored in computer
memory (e.g., RAM or sometimes ROM)

Each memory location is assigned a unique address.
Address
Stored Value
0
v0
1
v1
2
v2
3
v3
:
:
253
v253
254
v254
255
v255
8
Example: Vintage CPU (1975)
MOS 6502




64 KB main

Registers:






Die Shot
Single core
8-bit data
Memory


Pin-out
Accumulator (A)
Index (X, Y)
Processor Status (P)
Stack Pointer (S)
Program Counter (PC)
Speed: 1 to 2 MHz
Process: 8 m
# of transistors: ~3500
9
Example: Modern CPU (2013)
Intel i7-4770 Haswell



Package
Four cores
64-bit data
Memory
4x256 KB L2 cache
 8 MB L3 cache
 32 GB main (3.2x107 KB)


Registers:






Die Shot
8 32-bit
16 64-bit
Integrated GPU
Speed: 3.4 GHz (3400 MHz)
Process: 22 nm (0.022 m)
# of transistors: ~1.4 billion
10
Machine Language

Machine language (ML) is a set of very low-level
instructions that directs a computer processor to
perform specific elementary operations
 Not to be confused with the programming language ML!

Each CPU family has its own, unique ML

Deep expertise needed to write ML programs
11
How does a computer run ML programs?

A machine language program consists of processor
instructions and data that are stored in memory
Example:
6502 CPU
(start address = $600)
e.g., A9 represents the
CPU’s “Load Accumulator”
instruction
Stored Instruction or Data
Address (hex)
binary
hex
600
10101001
A9
601
01011010
5A
602
00011000
18
603
01101001
69
604
00100000
20
605
10001101
8D
606
00000000
00
607
00010000
10
12

The CPU’s program counter contains the address of
the next instruction to execute

Processor cycle:




Retrieve an instruction from memory
Decode the instruction
Act on the instruction
Increment the program counter
Example:
Visual CPU Simulator – http://visual6502.org/JSSim/index.html
13
Assembly Language

ML coding is tedious and error-prone, so more
advanced languages were developed

Assembly language programs (AL) use mnemonics
(symbolic names and keywords) instead of binary
digits

AL programs are easier to write, understand, and
modify than ML programs; but still tedious
14

Example: AL version of previous ML code
600:
602:
603:
605:
A9 5A
18
69 20
8D 00 10
LDA #$5A
CLC
ADC #$20
STA $1000
;
;
;
;
Load accumulator with number
Clear carry flag
Add $20 to accumulator w/carry
Store accumulator at $1000

An assembler translates an assembly program to
machine language

Assembly language still requires a high level of
programmer expertise
Example:
Easy 6502 – http://skilldrick.github.com/easy6502/#first-program
15
High-Level Languages

A high-level language (HLL) uses a more natural
language approach to keywords and syntax

A single HLL statement may be equivalent to many
machine or assembly instructions

Abstract algorithms are easier to implement in HLL

It is easier to write, maintain, modify, and port
programs using a high-level language; result: Higher
productivity
16

Example: HLL version of previous ML code
m = 0x5A + 0x20; /* Add hex $5A and $20 */

A compiler or interpreter translates a high-level
program to machine code

Many HLLs (several hundred) exist:
 FORTRAN, COBOL, Lisp, BASIC, …
 Pascal, C, C++, C#, Java, Python, …

An HLL increases programmer productivity (again)!
17
The C Programming Language





C was developed by Dennis Ritchie in the early 1970s
for use on UNIX systems at Bell Labs
C is “high-level” because it supports structured
programming practices
C also is “low-level”, as it permits access to
underlying computer hardware
C is an international standard (ISO)
C widely used in engineering, scientific, and business
disciplines:
 Efficient, good performance, portable
18
C Language Evolution

1969 to 1973 – C (Bell Labs initial development)

1978 – K&R C (Kernighan and Ritchie)


1989 – C89 (ANSI)
1990 – C90 (ISO)

1995 – C90 Normative Amendment 1 → "C95"

1999 – C99 (ISO)

2011 – C11 (ISO)
ANSI C89 and ISO
C90 are the same
standard.
19