# 16: Beyond the basics of computers


## Review

Previously we discussed the concepts that will allow you to make any program of moderate complexity.

• Data structures, operators, and program control is all you really need
• Defined programs as programs or functions with either functions or subroutines nested in the programs
• Defined scripts
• Discussed how to write programs

In this chapter we will discuss concepts that are advanced and required for complex problems.

• These concepts are less used by the general engineer and/or scientist but are important to understand
• Because these concepts are limited in importance to the general engineer, we will only briefly outline the concepts
• A computer engineer would need to take a course to fully understand these concepts
• These concepts allow the user to do many useful things, but also take more effort to master

## Service Routines

These are control concepts that allow the user to have more control than the basic program control of input/output, looping, and conditions.

• Interrupt
• A variable that registers when some portion of the hardware has been physically interacted with, like for instance pressing a key on the keyboard OR a register that allows you to interact with the hardware, like for instance writing a letter from the keyboard onto the screen
• The idea basically is that you interrupt normal processing to do something
• Mostly used in assembly language but some languages have access to this as well
• Example: when cropping an image if you have selected the region you press a button to crop, an interrupt detects that pressing of a button and starts the routine to crop the image
• Example: you are typing in your computer and the letters appear on the screen; an interrupt detects each letter you press and starts a routine to put the corresponding letter onto the screen
•  This snippet of an assembler program is an example of how to read a character through the use of an interrupt in the old 8080/8086 assembler language. This is not a complete program. There are online simulators of this assembly language (https://www.tutorialspoint.com/compile_assembly_online.php) and a working compiler for this assembly language (nasm). NB: Some basic processors (not Arduino, however, which uses C++) have assembler as their base language. NB2: This is an old snippet and works with MS-DOS/Windows (16-bit), if you are going to use this in Linux you should use 80h, however the authors would advise you use the simulator to learn the language before you use it in reality. Since Macs now have Intel, nasm should work on them as well, however, it will be different then what you will mostly find on the web so you really should take a course if you want to go into this detailed work.
• Stream
• Data supplied continuously over time
• Standard streams: Input, Output, Error
• For a stream of data from a satellite it probably would be better to use assembler and the interrupt routine

## Data Objects

These are simple constructs to help with data usage.

• Register
• A small portion of memory in the CPU for fast access, kind of like a special variable
• Interrupt registers are for interrupts
• Program counter
• Accumulator
• Memory registers
• etc.
• In general this is used in assembler language
• Pointers
• A variable that contains an address or reference to an actual value location
• Useful in languages that have call by value and the all important link list
• Can use to manipulate your computer memory in your own program (for experts)
• Can also lead to significant errors; problems; debugging time
• Data structures
• A group of variables that are of the same type or different type
• This is basically an expression of COBOL's DATA division
•  This is an example of a data structure with different data types using Fortran. This is the data structure for database of a collection of books, magazines, and papers for a rather large and esoteric "book" collection.

These are data constructs that utilize other data concepts. For the general engineer and scientists these are not necessary but might be useful in a limited way so a brief bread crumb is presented here. The student can study these on their own though this goes very much into computer science and less into solving problems in engineering and science.

• A set of variables connected by pointers
• Pointers can go one or two directions
• For increased efficiency can be modified to include variable arrays and a host of other ideas
• Very important concept for computers
• It is very useful in general computer theory to increase speed by deleting things without really deleting them
• This is the reason things are recoverable after deleting them (can be very useful if you deleted a picture that was irreplaceably but could be not so good if a criminal is able to recover what you thought you deleted)
• Unfortunately this leaves a mess which eventually has to be cleaned up (part of garbage collection)
•
 Example of linked list where the data (say words in a file) are linked together. Deleting a word in a file by deleting the links and making a new link. This is very efficient and fast because the memory doesn't have to be reorder so that data1, data3, and data4 are together like they should be. Basically data can be in any order as long as the links maintain order. Of course eventually this becomes a problem, but is worth it.
• Stack
• Array that places each variable on a stack just like a stack of plates in a cafeteria
• Used in stack-based programming languages (LISP) and calculators (old HP calculators)
• Can be implemented in hardware making it useful in basic operations
• Queue
• An ordered array
• Useful as a buffer
• Usually implemented with a linked list
• Hash table
• Uses keys and values (think COBOL)
• Useful for looking up data
• Graphs (as in data structures)
• A set of data which consists of nodes (vertices) connected by edges
• The node contains the data the edge is the link
• Flexible data structure that goes beyond matrices
• Requires a more standard data structure and an algorithm to implement it
• List structure (like linked list)
• Con: Slow
• Pro: Low memory
• Matrix structure (like array)
• Pro: Fast
• Con: Memory intensive
• Tree (as in data structures)
• A set of nodes (leafs) connected by branches to other leafs
• A special graph data structure
• In typical format the root is up and the branches and leafs go down
• A parent node is above the child node
• Nodes can have internal nodes (children as well)
• Used for searches, workflows, anything that is hierarchical
• Can be pruned or grafted: this is what give this structure its advantage over other possible structures
• Implementation of trees can either be with linked lists or a set of matrices (in languages that don't have pointers - which is very few now)
• For Fortran (95 and above) you could define a type tree in a module to create a tree
• For C++, Objective-C, C#, or D define a class tree
• MATLAB and Octave have the ability to define class tree as well
• C and older versions of Fortran do not have the ability to define classes so instead a system of matrices would need to be defined
• A decision matrix (say T)
• Some leaf matrices (say L1,L2, etc.)

These methods are very useful in engineering and science. They are a bit more complicated for a beginning computer course so we will only briefly introduce these concepts.

• Recursion
• When a function call itself
• In mathematics the perfect example is the factorial where $$n! = n(n-1)! =n(n-1)(n-2)!...$$
• Used in AI languages like LISP
• LISP natively uses recursion but most other modern programming languages are able to use recursion now as well
• LISP Example: (defun factorial(n) (if (<= n 1) 1 (* n (factorial (- n 1)))))
• Yes the example is just one line of code, LISP is know for its brevity
• The operations here are an example of stack computing where <= n 1 means in the non-stack world n <= 1, etc.
• Assignment: Figure out the rest of this small LISP program
•  This is the LISP program described in the outline. LISP is an interactive program and to run our lisp program we have to first run clisp (which is a open source lisp program). After we start the clisp program we then load our factorial program and run in (see red arrow). For a little clarity on how stack based programming works we do some simple calculation (see blue arrow): 5 times 6 and (2+3)*(2-3)...see if you understand how this all works and then you are on your way to learning LISP.
• Vector processing
• When a program divides its data to have the same operation done on multiple data sets
• The idea is to speed up mathematical operations
• This is SIMD (Single Instruction Multiple Data) as opposed to the usual SISD (Single Instruction Single Data) that you will find on almost all non-specialized computers
• This type of processing use to be available only on a Cray (supercomputer) and Convex (mini-supercomputer) but now is available on a lot of CPUs
• Parallel processing
• When a program splits its parts into independent parts that are run on different processors
• So part of the program run at the same time
• Very useful in Monte Carlo simulations
• Technically this is MISD (Multiple Instruction Single Data) but almost all implementation include vector processing which is MIMD (Multiple Instructions Multiple Data)
• mpirun (or mpiexec) can be used to run programs in MIMD
• This program and many others are installed when you install openmpi (https://www.open-mpi.org) or mpich (https://www.mpich.org)
• There is also mpicxx and mpifort plus others that can be used to compile your parallel program C++ or Fortran programs
• iPython has ipcluster to run a program in parallel
• Event Driven Language
• A programming language whose control is dictated by the interrupts of various sensors or user actions
• Sometimes confused with Object Oriented Programming, but it is distinct
• EDL/EDX (extinct)from IBM Series/I computers a long time ago
• Currently this ability is available in JavaScript, Java and Python (and obviously assembler) which are not directly event driven languages
• Object Oriented Programming (OOP)
• "Routines" are preformed by objects
• On the surface similar to functions and subroutines but distinctly different
• Objects can directly interact with one another (different from subroutines)
• Objects can have one or more methods (or "operations")
• Uses message passing and inheritance
• Data structure is more complicated
• Operators themselves are part of the data structure
• The operators are associated with the variables they operate on
• Has a class structure
• All "contained programs" are objects
• Can have abstraction and polymorphism
• C++ is object oriented, C is not
• While C++ is object oriented it can also be run without any objects
• Learning C++ is not learning object oriented programming you must learn the object oriented part of it...
• Modern Fortran has the ability to be object oriented, but older versions are not
• Most programming languages have the ability to do object oriented programming now
•  An non-computer human way of looking at an object. Here we have a class, subclass, object, property, and method. We also take about a method speak that polymorphs bark() into moo()
• Object oriented programming has useful features but it is often not used in the engineering and science world for many "real world" practical reasons
• Better for large projects like making a program to do scientific programming with all the bells and whistles (where the scientific program would not be object oriented)
• Parallel processing does not work with object oriented programming
• Recursion does not work with object oriented programming (though it can be emulated like they use to in Fortran before recursion was implemented
• Functional programming
• This is the "new competitor" to object oriented programming using basically old ideas (which are still good ideas) with some new ideas and vocabulary
• Technically each method of programming is better suited to different task so not a real competition, but that does stop people from online wars
• Fortran and C++ can do either of these methods but some programming languages are better suited for one or the other
• The idea behind functional programming is "pure" functions are used to create a program
• Functional programming has useful features and is preferred by the engineering and science world when large projects are not an issue
• Functional programming Object oriented programming
Function is primary method of getting things done Data is the primary method of getting things done
Recursion and parallel programming is available Recursion and parallel programming is NOT available
• Functions rule
• Pure functions
• With same inputs you always get same outputs
• No side effects (as you might find in OOP)
• Higher-order functions (functions that return functions)
• Type method is used for data (basically the original method used in most programming languages before OOP)
• Evaluation can be non-strict
• No overhead like in extensively nested classes in OOP (highly nested classes are for certain projects an advantage, but not all projects; hence a good criteria of when you should use OOP or not)
• Classes for data structures
• Object for data
• Methods for "functions"
• Abstraction
• Inheritance
• Polymorphism
• Encapsulation (hides data from user that they shouldn't need)

## Octave and Object Oriented Programming

Octave and its ilk are perceived by some to not have true object oriented programming and to some extend that is true but it is vastly over exaggerated.

• Problems with OOP in Octave
• Untyped variables (this is a feature a lot of programmers actually like, however it is not good for OOP)
• Weak polymorphism (because of weak typing)
• Vector structures is useful even in OOP
• Can be made to operate as fast as any other OOP language
• Using an OOP approach gives a measure of definition which allows Octave to produce very large projects
• Octave differs from other languages that use OOP in that it fits into a development role that deals with new problems that are not easily defined
• Good for wicked problems
• Many OOP languages are only good for "tamed problems"
• Wicked Problems (as opposed to the theoretical tame problems)
• Characteristics
• No final solution or true or false solution, just a better or worse solution (theoretically the tame problem has a true or false solution)
• No final test to the solutions (can't prove the solution, can't stop development)
• Of course you can do intermediate tests
• TEST your program even if you don't have a final solution
• Problems and solutions are in general unique (at least in the details)
• Can't be solve by traditional method of adding more management; actually makes matters worse
• Work for solution before all the data is in (very typical of engineering and science; or anything new)
• Octave and its ilk are good for solving this type of problems
• Solution to wicked problems: Extreme Programming (XP)
• Extreme programming is one of many methods that might be used to solve wicked problems
• Characteristics
• Simple design
• Small sustainable working releases; adds functionality and "bug" fixes
• Pair programming (pilot; co-pilot approach)
• Collective ownership (requires programs to help development like cvs - the program not the store)
• Documentation (actually all methods require this) - see comments in any programming language (all programming languages have the ability to comment)

## Value versus reference languages

There are two type of languages those that pass arguments by value and those that pass arguments by reference. Some languages do both. This definition however is not strict as call by value and call by reference can mean different things even within the same categories. With this said...

• Normally pass by value means that a copy of the inputs are passed to a function; could make things slow for large structures
• Normally pass by reference means an address to a variable is passed to a function; error could lead to "crashed" program in certain programming languages
• Languages that pass function input/outputs by value
• Advantage is in general speed but not in memory
• C (in practice users of C pass a pointer to the variable they wish to change)
• The pointer method can lead to errors if not done carefully
• Pascal
• GDL/IDL
• Java (very different from C though)
• Octave/MATLAB/Scilab
• Languages that pass in calls by reference
• Efficient in time and space
• Fortran (not pure however which is a copy of the reference; which is better)
• Perl
• Languages that pass by both methods
• C++
• Some Fortran versions
• Basic
• LABView

So this is a quick brief bread crumb chapter to give hooks to other methods that an engineer/scientists may need, but likely not need for their career. We will end the chapter here. Next chapter will finish off some concepts on computers.

16: Beyond the basics of computers is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by LibreTexts.