Title: Whats a Computer Program
1Whats a Computer Program?
- A computer program is a sequence of instructions
to the computers central processing unit (CPU).
- A program may also include instructions which are
executed on other components, i.e., instructions
to query or control some device attached to the
computer.
2Instructions to the CPU
- A CPU executes instructions, and uses data, in
binary format. An executable computer program
may look like this
0010110010100010 0001011110000000 0010110000011000
1101100001011001 0011001010001011 000001110100111
1 1101100001100001 0010110100010001 00010111000011
10 etc
First instruction to CPU Second instruction an
address for 2nd instruction 3rd instruction 4th
instruction an argument (data) for 4th
instruction more data for 4th instruction 5th
instruction 6th instruction more instructions
3Running A Program
- The program and data is loaded into main memory.
- The address of the program's first instruction is
placed in a special register in the CPU the
program counter. - The CPU is told to execute that instruction.
program counter
0010110010100010 0001011110000000 0010110000011000
1101100001011001 0011001010001011 000001110100111
1 1101100001100001 0010110100010001 00010111000011
10 0011001010001011 0000011101001111 1101100001100
001 0010110100010001
loader loads program instructions into memory and
prepares it for execution
cpu copies instructions into a cache for
execution the program counter points to current
instruction
file on disk
program in memory
4What instructions does a CPU understand?
- CPU instructions implement logic (operations)
that is designed or "hardwired" into the CPU. - Most CPU instruction are very simple, such as
- LOAD memory_address TO some register
- SAVE some register TO memory_address
- MOVE some register TO another register
- ADD some register TO another register
- MULT some register TO another register
- COMPARE register TO another register
- TEST some registers value, jump to new
instruction if true - JUMP TO new instruction (unconditional branch)
5Generations of Computer Languages
- Machine language program as series of binary
instructions (0 and 1's) - Assembly Language
- "High level" or procedural Languages
- this is what we will study
- C, C, C, Fortran, Java, Lisp, Pascal, ...
- 4GL - application specific languages
6Machine Language
- These simple, binary instructions are called
machine language instructions. - Writing machine language is slow and difficult,
so people invented a symbolic notation for
machine language, called assembly language.
Machine Language 0010110010100010 0001111110000000
0010110000011000 1101100001011001 001100100001100
0
Assembly Language MOV AX,1F80H PUSH AX INT 21H
POP AX
7Assembly Language
- Simple assembly language instructions have a
1-to-1 translation to machine language. - A program called an assembler converts assembly
language into machine languageUNIX as -o
output_program input_program.s
Machine Language 0010110010100010 0001111110000000
0010110000011000 1101100001011001 001100100001100
0
Assembly Language MOV AX,1F80H PUSH AX INT 21H
POP AX
assembler
8Viewing Assembly Code on a PC
- Find a small .exe or .com file, e.g. diskcopy.com
- Open a Command Prompt (DOS) window.
- Enter debug filename.
- Enter u to unassemble and view the program.
- Enter q to quit debug.
C\WINDOWS\system32gt debug diskcopy.com -
u 0B300000 0E PUSH CS 0B300001 1F
POP DS 0B300002 BA0E00 MOV
DX,000E 0B300005 B409 MOV
AH,09 - q
machine code(in hex)
assembly language
9Macro Assembly Language
- Simple assembly language instructions are still
very tedious and time consuming to input
(code). - Macro assembly language adds symbolic variable
names and compound instructions. - A compound instruction is an instruction that is
converted into several machine level instructions.
10Macro Assembly Example
- Here is a simple Hello, World program in macro
assembly language
.stack .data message db "Hello world, Im a
program., "" .code main proc mov ax,seg
message mov ds,ax mov ah,09 lea
dx,message int 21h mov ax,4c00h int 21h main
endp end main
11Problems with Assembly Language
- Programming in macro assembly language is still
difficult - language does not match the way people think
- programs are long and difficult to understand
- logic errors are common and difficult to correct
- hard to divide a large problem into small
problems using assembly language (bad for team
work) - assembly language is machine dependent -- wont
run on a different type of computer (not portable)
12High Level Languages
- Higher level languages (also called third
generation languages) provide these features - English-like syntax
- descriptive names to represent data elements
- concise representation of complex logic
- use of standard math symbols
- total quantity price (1 taxrate)
- conditional execution and looping expressions
- if ( total gt 0 ) then writeln(The total is ,
total) else writeln(No charge.) - functional units, such as "functions" or
"classes" - radius sqrt( xx yy ) // call sqrt
function
13High Level Language Benefits
- programming is faster and easier
- abstraction helps programmer think at a higher
level - standardized languages
- programs can be machine independent
- reduce training effort
- problems can be divided into smaller pieces, each
coded and tested separately. - code re-use
- standard programming interface for hardware
interactions, such as input and output.
14Some higher level languages
- There are 4 broad categories
- Imperative FORTRAN, COBOL, BASIC, C, Pascal
- Functional Scheme, Lisp
- Logic Languages Prolog
- Object-oriented
- Pure object-oriented Java, Python
- Object-oriented imperative C, Perl, Visual
Basic
15Fourth Generation Languages
- Fourth Generation Languages (4GL) are application
specific. They are tailored for a particular
application. - SQL (structured query language) for databases
- Postscript page description language for printers
- PDF (portable document format) for online
documents - HTML and PHP for World Wide Web content
- Mathematica for symbolic math on a computer
- Mathematica is a proprietary language the others
are industry standards.
164GL Example SQL
- SQL is one of the most widely used 4GL.
- Examples of SQL statements
- Insert data into a database table
- INSERT INTO employees (id, lastName, firstName,
Job)VALUES (1445, John,Smith',manager) - Choose data from a table based on criteria
- SELECT id, lastName, salary FROM employees
WHERE ( Job manager ) - Learn more about SQL at the Open Directory
Projecthttp//dmoz.org/Computers/Programming/Lan
guages/SQL
17Language processing Interpreted
- Interpreted BASIC, Postscript, SQL, Matlab
- The interpreter reads the source program and
executes each command as it is read. The
interpreter knows how to perform each
instruction in the language.
Source Program
Interpreter
Execution
18Language processing Compiled
- Compiled C/C, Pascal, Fortran
- The compiler converts source code into machine
language to create an object code file. - A linker combines object code files and
pre-compiled libraries to produce an executable
program (machine language).
19Compiling a Program
Source Code
file.obj .sym printf FE048C7138 029845AAAF ...
Object Code
Compiler
file.c main() printf("hello") exit(0)
Executable Program
Linker
printf.obj ltobj. code for printf functiongt
Libraries (of object codes)
file.exe lthardware instructionsgt
20Interpreted versus Compiled
- Interpreted
- Flexible
- More interactive
- Rapid development
- Can run program immediately after writing or
changing it
- Compiled
- More efficient
- Extensive data checking
- More structured
- Usually more scalable (can develop large
applications) - Must (re-)compile program each time a change is
made
21Java A Hybrid Strategy
- Java Compiler compiles program to create a
machine independent byte code. - Java Virtual Machine (interpreter) executes the
byte code.
Libraries
Hello, World!
javac
Hello.class
java
compiler
byte code
Java source program
Java VM - byte checker - class loader -
interpreter
Program execution
Java Runtime Environment (JRE)
22How to Get Java
- Download the "Java Development Kit" from
java.sun.com. - JDK contains
- java compiler (javac command)
- java virtual machine (java command)
- java development and runtime libraries
23Where to Install Java?
- Default location
- c/Program Files/java
- Better
- c/java/jdk