The Imperative Programming Paradigm

The Von Neumann Architecture

Imperative programs are designed to be executed by Von Neumann Machines:

The processor runs the fetch-execute cycle:

do {
   ir = ram(ip++) // fetch next command
   execute(ir)
} while (ir != halt)

Where:

ir = the instruction register

ip = the instruction pointer

ram = random access memory

The basic instruction set includes commands that:

read a word from the input file

write a word to the output file

read a word from memory

write a word to memory

combine/compare words using arithmetic and bitwise logical operations

write a word to ip

Imperative Programming 1.0

A program is a sequence of commands.

Commands are executed relative to an environment.

An environment is a collection of named variables.

Commands update variables.

Types of commands:

Variable declaration

def x = var(100)

Assignment

x = x + 1

Sequence Control

while(condition) command

if (condition) goto label1

goto label

I/O

write(x)

read(x)

arithmetic/logic

x = (x + 4)/6 * 7

t = a && (b || !c)

Also...

Programs are stored in memory, along with data.

The instruction pointer (IP) is a special variable that points to the next instruction in the program to be executed.

Control commands such as goto, if-else, and while can update this variable, thus modifying the order of execution.

It is also possible for programs to modify themselves.

Examples: Assembly Languages

Imperative Programming 2.0

The Qualification Principle: The scope of a variable should be as small as possible.

IP 2.0 program adds block commands to the basic instruction set.

A block is a sequence of commands grouped together as a single command:

{cmmd; cmmd; ...; cmmd}

Blocks can be nested.

A variable declared in a block is only visible in that block.

IP2.0 programs are trees rather than flat lists. Parent nodes are block commands. Leaf nodes are non-block commands.

Imperative Programming 3.0: Procedural Programming

The abstraction principle: the purpose of a component should be independent of its implementation

IP 3.0 adds procedure declaration, call, and return to the instruction set.

A procedure is a block with a name and parameters.

def c2f(cTemp) = {def r = var(9/5); return r * cTemp + 32}

A procedure can be called by another procedure.

c2f(100)

A procedure may compute and return a value to the calling procedure. (Such procedures are called functions.)

Procedures can be stored in libraries and reused.

Procedures blocks may contain the declarations of sub-procedures.

Example: demo.pascal

Imperative Programming 3.1: Structured Programming

Programming Goals: Programs should be correct and should not be needlessly difficult to maintain.

Structured programming is procedural programming with the added stipulation that blocks my only be executed starting with their first command. Jumping into the middle of a block can make it difficult to trace program execution and therefore difficult to maintain the program (which includes fixing bugs).

Proving the correctness of a program requires single entry blocks. (Single exits also help.)

This stipulation can be violated by unwise use of goto commands.

Imperative Programming 4.0: Modular Programming

Modularity principle: programs should be constructed from loosely coupled, coherent modules.

IP 4.0 adds module declarations. A module is a named collection of procedure, variable, and sub-module declarations.

Module members can be public or private,

The interface of a module lists all of the public members, but not their implementations.

Modules are namespaces. Several modules can have procedures with the same name.

TriangleModule.area

RectangleModule.area

Packages are examples of modules.

How are modules different from classes?

Examples: Modula-2, Ada, Areas.defs

Project

Amoeba