Title: Programming Languages
1Programming Languages
2Review
- What kinds of functions belong in an OS?
- What factors might influence which functions to
include? - Give an example of a function that belongs in an
OS? - Give an example of a function that doesnt belong
in an OS?
3Concepts So Far
- What course concepts have we covered so far?
- Have you started to recognize these concepts
outside of this class?
presnick Representation Notification
(interrupts) Indirection (virtual
memory) Abstraction (file system, other useful
illusions)
4Learning Objectives
- Understand and explain
- Source code vs. machine (object) code
- General purpose vs. special purpose languages
- Programming concepts
- Data types
- Local and global names
- Subroutines
- APIs
- Compiler vs. interpreter (virtual machine)
- Understand some basic software engineering
practices
5First Generation Programming Language
- Machine language
- Binary codes corresponding to CPU instructions
analogous to 3-digit codes in LMC - Each CPU has a unique machine language
- Different instructions for Intel 486, Intel
Pentium, Motorola 68K, PowerPC, etc. - Object code/Executable
- Platform-specific machine language program
6Challenges of Machine Language Programming
- Unintuitive way to refer to instructions
- We think of instructions as words
- Machines use numbers
- Cumbersome
- To perform even simple tasks often requires
execution of many instructions - E.g., theres no machine language instruction to
display a character on the screen this takes
many instructions
- Do these challenges limit what you can program?
- Are there any programs that you could only write
in a language without these limitations? - Are there any programs that would be easier in a
language without these limitations?
7Second Generation PL Assemblers
- More intuitive instruction names
- Assembly language uses words to represent
instructions instead of numbers - In an LMC assembler you might write Get, Store
99, Get, Add 99, Put - We call program code in this higher-level
language the source code - Translate with an assembler
- From source code in assembly language to object
code in machine language of target machine - A direct translation each instruction has a
single numeric equivalent - Example an assembler for LMC
- From source Get, Store 99, Get, Add 99, Put
- To object code 901, 399, 901, 199, 902
8An Assembler for LMC (download)
- while (line ltSOURCEgt)
- line m/(\S)\s(\d)/
- command 1 loc 2
- if (command eq "Add")
- print OBJ "1"
- print OBJ "loc\n"
- elsif (command eq "Subtract")
- print OBJ "2"
- print OBJ "loc\n"
-
- elsif (command eq "STOP")print OBJ
"000\n" - else die("Error, couldn't parse line")
9Third Generation PLsHigh-Level Languages
- More intuitive instruction names
- Instructions specify more abstract operations
- E.g., complex but commonly used tasks are often
represented with a single instruction - E.g., most high-level languages include an
instruction for displaying a character on the
screen - Other capabilities intended to make programming
easier and more efficient - Translation from source to object code is more
complex - Well talk about how to make high-level source
code executable next time - E.g., JavaScript
10High-level Language Example (click here)
- English-like instructions
- E.g., for, document.write
- But not English is an instruction, too
- Instructions for complex tasks
- E.g., document.write displays text on screen
ltscriptgt for (i0 ilt20 i)
document.write(Count " i .ltbrgt)
lt/scriptgt
11Two Kinds of High-Level Programming Languages
- General Purpose Languages (GPL)
- Intended to support any kind of programming task
- Special Purpose Languages (SPL)
- Trade some of the flexibility of a general
purpose language for capabilities that are more
tailored to a specific task
12General-Purpose High-Level Languages
- Processing
- Built-in complex commands
- Conditional statement
- Looping statement
- Ability to define new named procedures/subroutines
- Named variables
- Data Handling
- Basic data types (numbers, strings, etc.)
- Ability to create compound data structures
13Programming Concepts Subroutine/Method
- Section of code
- From other code, branch to beginning of
subroutine - Automatically branch back to other code when done
- Can pass parameters (inputs)
- Can return value (outputs)
14Subroutine Example (click here)
- ltscriptgt
- function padNumber(the_number)
- if (the_number lt 10)
- padded_number "0" the_number
-
- else padded_number the_number
- return padded_number
-
- for (i0 ilt20 i)
- document.write(Count "
- padNumber(i) .ltbrgt)
-
- lt/scriptgt
Subroutine
Note is string concatenation here
Calling the subroutine, passing i as an input
parameter
15Programming Concepts Named Variables
- Values assigned to named variables change during
program execution - Local variables
- Local namespace for each subroutine
- Values only preserved within subroutine
- Global variables
- Global namespace
- Values preserved throughout program
16function minimum(x, y) assume x is
expressed in bits y in bytes global count
count count 1 increment counter y
8 y convert y to bits print "count is
count x is x y is y inside minimum
functionltbrgt" if (x gt y) return y
else return x
Inputs local by default
Declared global
Because count is global, increment applies
everywhere
Because y is local, conversion only applies
within function
Value returned at end
17Programming Concepts Compound Data Structures
- Organized collections of simple data elements
- Variable can refer to entire collection
- Common examples
- Arrays
- Linked lists
- Queues
- Stacks
- Binary trees
18Complex Data Structure Example
- A variable containing nested arrays
data array(array("first" gt 350, "second" gt
200), array("first" gt 800, "second" gt
50), array("first" gt 1000, "second" gt
90), array("first" gt 25, "second" gt
10))
data first second Row 1 350 200 Row 2
??? 50 Row 3 1000 ??? Row 4 25 10
19Combining The Concepts
count 0 while (list(, pair) each (data))
x pairfirst y pairsecond
echo "Before calling minimum, Count count
x was x bits y was y bytesltbrgt" smaller
minimum(x, y) echo "After calling
minimum, Count count x was x bits y was y
bytes smaller is smallerltbrgtltbrgt"
As long as there are more values in the array
data, set pair equal to next row
Invoking the minimum function already defined
(subroutine example)
20Program Output
Before calling minimum, Count 0 x was 350
bits y was 200 bytes count is 1 x is 350 y is
1600 inside minimum function After calling
minimum, Count 1 x was 350 bits y was 200
bytes smaller is 350 Before calling minimum,
Count 1 x was 800 bits y was 50 bytes count
is 2 x is 800 y is 400 inside minimum
function After calling minimum, Count 2 x was
800 bits y was 50 bytes smaller is 400 Before
calling minimum, Count 2 x was 1000 bits y
was 90 bytes count is 3 x is 1000 y is 720
inside minimum function After calling minimum,
Count 3 x was 1000 bits y was 90 bytes
smaller is 720 Before calling minimum, Count
3 x was 25 bits y was 10 bytes count is 4 x is
25 y is 80 inside minimum function After calling
minimum, Count 4 x was 25 bits y was 10
bytes smaller is 25
Conversion only applied within function
Increment applied throughout program
Produced by subroutine
21Download Code for This Example
- Run it
- http//www.si.umich.edu/Classes/540/Readings/Code/
test.php3 - Download it (try editing it and putting in your
php-enabled webspace) - http//www.si.umich.edu/Classes/540/Readings/Code/
testphp.txt
22Special Purpose High-Level Languages
- Complex commands built in as primitives
- May not have ability to define new named
procedures - Data structures tailored to some task
- May not have ability to create compound data
structures - Potential advantages
- Simplicity easier to learn and read
- Security cant express unsafe commands
- Potential disadvantages
- Specialized learning users may know
general-purpose language already - Specialized implementation need the translator
program - Has anyone in the class worked with one of these?
23Example SQL
- Complex built-in commands
- Select rows and columns
- Aggregation across rows
- Joins across tables
- But very little flexibility
- No looping construct
- No named procedures
- Complex built-in data structure
- Tables
24Example Robot Exclusion Language
- Disallow is the (only) built-in command
- Data structures file path names
- Later well see Apaches authorization language,
which is similar in structure, but with more
commands and data types
25Hybrids
- Specializing General Purpose Languages
subroutine libraries - Collection of Subroutines
- Usually written by someone else
- Your code calls those subroutines
- Using the specified programming interface
- Generalizing Special Purpose Languages
- Add looping, named procedures, complex data types
- SQL ? PL/SQL
- TCL/TK, AWK, Perl, PHP, (ColdFusion not quite
there yet)
26Language GenerationsWith Examples
- Machine language
- Assembly language
- Analogous to Get, Store, etc. in LMC
- General-purpose high-level languages
- C, C, PASCAL, COBOL, Java
- Special-purpose languages
- SQL, Robot exclusion language, Apache server
configuration language - Some hybrid general and special-purpose
- Visual Basic, ColdFusion, PHP, Perl
27Programming Concepts Programming Interfaces and
APIs
- API Application Programming Interface
- Dont confuse this with a user Interface
- Defines a set of functions that may be called by
application programs - Like a library
- But application developer may not have access to
the code implementing the functions - E.g., being an eBay developer means you can use
the API, not that you can see how it is
implemented - And the functions may even be executed on a
different computer! - E.g., eBay developers are using functions located
on eBay servers, not their own computers
28Example eBays API
- Alternative to interacting with the web site
- Functions provided for
- Listing items
- Tracking a particular users auctions
- Leaving feedback
- Everything eBay users can do
- Allows application developer to provide custom
interface to users, with extra features - Built on top of eBay functionality
29An eBay API (click here)
LeaveFeedback Call LeaveFeedback to post
positive, negative, or neutral feedback
information about a user after the conclusion of
an auction.
- What input?
- User identified by a unique userid
- Feedback positive, negative, or neutral
- What output?
- Success or error
- Write it
- LeaveFeedback(userID,feedback) ? Success
ErrorCode
30API exercise
GetBidderList Call GetBidderList to retrieve a
list of all of the items on which a user was a
bidder.
- What input?
- What output?
- Write it
- What input?
- User identified by a unique userid
- What output?
- Array of items or error
- Write it
- GetBidderList(userID) ? ItemArray ErrorCode
31Conclusion
- Machines execute machine language code
- Machine language code is hard to write, so
programmer have created many other languages to
make programming easier - These have to be translated somehow to allow
execution - There are both general purpose and specialized
languages - Several common programming concepts
- Data types
- Local and global names
- Subroutines
- APIs
- Next time well talk about some of the tools and
techniques that programmers use to write software
32Review
- Whats the difference between an SPL and a GPL?
- Whats HTML?
- Given a function, add(x,y), that sums the input
parameters and returns the result, what would you
expect document.write(add(1,2)) to do? - What is an API?
- Who develops it? Why
- Who uses it? Why?
33Learning Objectives
- Understand and explain
- Source code vs. machine (object) code
- General purpose vs. special purpose languages
- Programming concepts
- Data types
- Local and global names
- Subroutines
- APIs
- Compiler vs. interpreter (virtual machine)
- Understand some basic software engineering
practices
34Summary ofProgramming Terminology
- Machine language Binary codes that correspond
to the machines instruction set - Source code Program code in a higher-level
language - Object code Program code in machine language
- Executable A file containing all the relevant
object code for an application
35Compilers and Interpreters
- Programmers have developed two tools for allowing
the execution of high-level language source code - Compiler
- A program, called a compiler, converts an entire
set of source code to platform-specific machine
code - Once compiled, this executable runs without the
compiler - Interpreter (also called an emulator or virtual
machine) - Source code executes directly on a virtual
machine, a machine implemented in software,
whose instruction set includes the high-level
operators - The interpreter must be running in order to
execute the source code
36Compilation
- Compiler
- Converts source program to platform-specific
compiled program (the executable, in machine
language) - Once compiled, executable runs without compiler
37Assembler A Very Simple Compiler
- LMC Assembler translates LMC English to LMC
machine code - One machine language instruction for each
assembly language instruction - Compilers for higher-level languages have a
harder task - Sometimes read several lines before knowing how
to translate - E.g., read entire for loop instruction
- Can output many machine language instructions for
a single line of code - E.g., many instructions to do a loop, as you did
for computing min of all inputs
38SI540 Players human language analogy for
compile/interpret
- Im going to give some instructions in English
- I need two pairs of volunteers who speak a common
non-English language - For example, two Korean speakers and two Spanish
speakers - One member of each pair will write a program, the
other will be a compiler or an interpreter - Compiler translates whole program to English
- Interpreter- interprets lines of program and
executes them directly, without translating to
English
39Compilation and Portability
- Source program can work across platforms
- As long as the program doesnt make OS-specific
calls - Machine language program (executable) is specific
to a machines instruction set, and to operating
system routines to which the machine language
program branches
40Cross-compilation
- Cross-compilation is when
- Compiler runs on one kind of machine, but
- Generates machine code (object code) for some
other machine - E.g., using a Wintel PC to produce code for an
embedded CPU used inside a car - In the example
- What instruction set does the compiler program
use? - What instruction set does the compiled program
use?
41Compiling the compiler
- Compiler can be written in a high level language
- Some other compiler program, in machine code for
the machine it runs on, can convert the first
compiler program into machine code for some other
machine - Check your understanding exericse
- Add the compiler as source program that gets
compiled into the previous slide - Do you need a separate compiler program
(executable object code) for each - Source language?
- Machine language that the compiled programs will
run on? - Platform that the compiler will run on?
- Which do you need a separate compiler source code
program for?
42Understanding Interpreters The Virtual Machine
Metaphor
- Machine
- Can directly execute instructions in a language
- Virtual
- Machine may be emulated with another computer
program - That program is called an interpreter or
emulator or sometimes virtual machine - In our SI540 Players exercise
- Instructions in foreign language were the
interpreted program - The human interpreter was the virtual machine
43Interpreter Diagram Shorthand
Data inputs
We diagram like this
Virtual Machine
Program
Output
But this is what is really happening
Original program original data inputs
Interpreterprogram
Output
Machine
44Language Choice And Compilation vs. Interpretation
- Any high-level language can be interpreted or
compiled - By tradition, some are normally interpreted
- LISP, perl, PowerScript, SmallTalk, DOS .bat
files - Others compiled
- C, PASCAL, Ada
45Pros And Cons OfCompilation vs. Interpretation
- Compiled code
- Faster and smaller
- Object code is compiler independent
- Object code is optimized for platform
- Resultant executable is platform specific
- Difficult debugging
- Changing code requires recompilation
- Interpreted code
- Slower and bigger
- Execute the interpreter and the interpreted code
simultaneously - Works on any platform for which there is an
interpreter - Easier debugging
- Easier to change dont have to recompile to run
it - Errors linked directly to high-level instructions
46Hybrids? Sure!
- The two techniques for making code executable
both have advantages - Java is a hybrid system that tries to realize key
benefits from each - Reduced file size (compilation)
- Platform independence (interpretation)
47Executing JAVA
- Java source code is compiled to produce Java byte
code (.class) - Reduces size, saving network transit time
- Some intellectual property protection prevents
others from seeing source code
- Java byte code is interpreted on the end-users
machine by the Java Virtual Machine - Allows platform independence any platform with
a Java VM can execute Java byte code
JAVAsource
Data inputs
JAVA compiler
JAVA byte code
Java VirtualMachine
Output
Machine
48Without the Shorthand
Data inputs
JAVAsource
JAVA byte code
Output
Java VMprogram
Developers Machine
End-Users Machine
49Java Applet Scribble
- public class Scribble extends Applet
-
- private int last_x 0, last_y 0 //
Fields to store a point in. - // This method displays the applet.
- // The Graphics class is how you do all
drawing in Java. - public void paint(Graphics g)
- g.drawString("Click near here to scribble",
25, 50) -
- // Called when the user clicks.
- public boolean mouseDown(Event e, int x, int y)
- last_x x last_y y //
Remember the location of the click. - return true
-
50Scribble (cont)
- // Called when the mouse moves with the button
down - public boolean mouseDrag(Event e, int x, int y)
- Graphics g getGraphics() // Get a
Graphics to draw with. - g.drawLine(last_x, last_y, x, y) // Draw a
line from last point to this. - last_x x last_y y // And
update the saved location. - return true
-
51Scribble Byte Code Excerpt
- Display generated by a byte code viewer
- Instructions represented with words
- Actual instructions are numbers
- This is only one of five subroutines/methods
- Program also declares 55 constants and 2
variables
- 0 aload_1
- 1 ldc 1 ltClick near here to scribblegt
- 3 bipush 25
- 5 bipush 50
- 7 invokevirtual 8 ltjava/awt/Graphics.drawStringgt
- 10 return
52Check Your Understanding
- Is it possible to create a compiler that would
take C source programs and create Java
bytecode? - Is it possible to create an interpreter program
(VM) for Java source code (not Java bytecode)?
53Some Software Engineering Techniques
- Requirements Documents
- Design Documents
- Revision Control Systems
- Test Suites
- Alpha/Beta Testing
54Revision Control System
- Store multiple versions of (code) files
- Comment stored with each version documents
programmers motivation for changes made - Compare versions (e.g., Unix diff command) and
merge changes - Rollback to earlier version
- Coordinate among developers
- Associate an author with each version
- Prevent authors from overwriting each others work
55Test Suites
- Test
- Set of input parameters to pass to a subroutine
or program, and the correct output for comparison - Test suite
- Set of tests that executes important paths
through the program (instruction sequences) - Even paths that wouldnt be executed under normal
conditions (e.g., error handling routines) - But you cant test every path, because there are
too many possibilities
56Test Suite Exercise
- Suppose youre creating a program that allows a
user to automatically create separate listings
for ten identical items on eBay - E.g., the user wants to sell ten copies of a
valuable trading card, each card in its own
auction - The user provides a userID, a password, and the
information about the item (title, description,
starting bid) - What would a test suite look like?
- What kind of inputs?
- What kind of outputs?
57Alpha/Beta Testing
- Ship program to users
- Warn them there might be bugs!
- Alpha
- First release to users may be very buggy
- Ideally, users within the organization
- Beta
- Second release to users- fewer bugs
- Incentives for Alpha/Beta testers
- Get software sooner
- Tech support
- Be important
- Lower price (sometimes)
58Conclusion
- (APIs)
- High-level language translation
- Compilers
- Interpreters
- Developer tools
- Revision Control Systems
- Test Suites
- Alpha/Beta Testing