GAme

tiistai 26. kesäkuuta 2012

Summary

Computer programming is composing/authoring of a process/procedure for doing something, in great detail.
    proc-ess / Noun:
    A series of actions or steps taken to achieve an end. 

    pro-ce-dure / Noun:
    A series of actions conducted in a certain order or manner.
   
Since computers do not understand English and it would be impossible for a human to write a large program as a series of binary numbers that the computer can understand, we need something in between.  High-level programming languages currently fit in this category.  Given a programming language that you have chosen, you then follow its rules for composing statements (or expressions) that get the computer to do what you want.
Because a computer is simply a very fast manipulator of bits (ones and zeros), through the the power of abstraction, computer scientists have layered levels of object representation and functionality, one on top of another.  We have now been working on refining and extending these layers for over half of a century.  Table 1.6 should give you a feel for where we stand with computer programming languages today.
In the next lesson you'll start to write programs in Logo!

Application-Specific Language(4GL)
Examples: Mathematica, SQL
High-Level Language
Examples: Logo, Python
Low-Level Language
Example: C
Assembler Language
Example: Intel X86
Machine Language
Example: Intel X86
Table 1.6

Programming Languages (The Microprocessor's Language)

So, all a computer has in it is bits.  You've seen how they are used to represent stuff, pixels, numbers and characters.  I've mentioned that computers perform operations on the bits, like move them around, add pairs of them together, etc...  One final obvious question is: how are instructions that a computer performs represented?
Well, if you instructed a computer in its native language (machine language), you would have to write instructions in the form of (yes, once again) binary numbers.  This is very, VERY hard to do.  Although the pioneers of computer science did this, no one does this these days.
Just to give you something to look at, just to compare, Table 1.5 shows what the assembler language program in Table 1.1 could look like assuming that the machine instructions are loaded into memory at addresses 100 through 107.  Also, the group of numbers starts at memory address 111 and the size of the group is in memory address 110.

 Address  OpCode Register  Memory
Address
 Index
Register
100   205   1 400000    
101   200   2 110    
102   361   2 107    
103   317   1 111   2
104   254     102    
105   200   1 111   2
106   254     102    
107   263   17    
 Address Value
110   67  
111   47316  
. . . 
177   2751  
Table 1.5

A detailed explanation of any computer's instruction set is beyond what can be presented here.  I just wanted you to see how the symbolic information in assembler language programs needs to be converted to numbers (bits) before a computer can perform it. 
If you really want more details now, here is a side lesson from one of my favorite introductory computer science books: The Computer Continuum.

Pixels

The image on your computer's display consists of a bunch of colored points called pixels.  A pixel is an object.  It has a color and a position (its coordinates) which consists of the row and column it is at.  Figure 1.1 shows an artist's rendition, a magnification of a display with a circle drawn in yellow.  The tiny black dots are the pixels and the big yellow dots are the pixels that have been colored.
Pixel Circle Figure 1.1
As an example, to display a thin vertical line, the color values of a column of pixels are set to the desired color of the line.  If you want a thicker vertical line, you set the color values of the pixels of a group of consecutive columns to the desired color.  Figure 1.2 shows a red line that's a single pixel wide and an orange line that's three pixels wide.  The orange line is actually a very thin rectangle.
Pixel Lines Figure 1.2
So, the location of each pixel is obviously specified by a pair of numbers; what about the pixel's color?
Well... a pixel's color is also specified as numbers, three of them, called RGB (Red, Green, Blue) values.  Play with the following Java applet which lets you see what number values generate which colors.  What color do you get if you set red to 170, green to 85, and blue to 255?  What's the RGB value for your favorite color?

Symbols as Bits - ASCII Characters

Ok, so numbers are simply groups of bits.  What other objects will the computer's instructions manipulate?  How about the symbols that make up an alphabet?
It should come as no surprise that symbols that make up alphabets are just numbers, groups of bits, too.  But how do we know which numbers are used to represent which symbols, or characters as I'm going to call them from this point on?
It's all about standards.  In these lessons, we will use the American Standard Code for Information Interchange (ASCII) standard.  It is so ubiquious that it even has its own web page, www.asciitable.com.
Let's walk through a couple of examples, entries in the table.  Here are some characters, their decimal value and their binary value which is then transformed into an octal number.

        Uppercase 'A' = decimal 65 = binary 01000001 = 01 000 001 = octal 101
        Uppercase 'Z' = decimal 90 = binary 01011010 = 01 011 010 = octal 132
        The digit '1' = decimal 49 = binary 00110001 = 00 110 001 = octal 061
   
Table 1.4 is a small slice from the full ASCII character set, just enough to give you a flavor of its organization.
ASCII
Character
in
Binary
in
Octal
in
Decimal
in
Hex
space
00100000
040
32
20
(
00101000
050
40
28
)
00101001
051
41
29
*
00101010
052
42
2A
0
00110000
060
48
30
1
00110001
061
49
31
2
00110010
062
50
32
9
00111001
071
57
39
A
01000001
101
65
41
B
01000010
102
66
42
C
01000011
103
67
43
Z
01011010
132
90
5A
a
01100001
141
97
61
b
01100010
142
98
62
c
01100011
143
99
63
z
01111010
172
122
7A
Table 1.4

Here's a little trivia, from days long past when computers were so slow (compared with today).
  • Look closely at the binary representations of uppercase and lowercase letters.  You can convert uppercase to lowercase by turning on a single bit.  Or clearing the bit converts lowercase to uppercase.
  • Clearing two bits will convert an ASCII digit to its numeric value.  Setting the same bits converts a number in the range 0...9 into its ASCII character representation.

Just Bits Numeric Representation With Bits

        There are only 10 different kinds of people in the world:
        those who know binary and those who don't.
                                                   - Anonymous 
   
Computers are full of zillions of bits that are either on or off.  The way we talk about the value of a bit in the electical engineering and computer science communities is first as a logical value (true if on, false if off) and secondly as a binary number (1 if the bit is on and 0 if it's off).  Most bits in a computer are manipulated in groups, so we humans need a way to describe groups of bits, things/objects a computer manipulates.  Today, bits are most often grouped in quantities of 8, 16, 32, and 64.
Think about how you write down sequential numbers starting with zero: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, etc...  Our decimal number system has ten symbols.  In this sequential series, when we ran out of symbols, we combined them.  You learned how to do this so long ago, in grade school, that today you just naturally think in terms of single digit numbers, then tens, hundreds, thousands, etc...  The decimal number 1234 is one thousand, two hundreds, three tens, and four units.
So, how does the binary number system used inside computers work?
Well, with only two symbols, we would write the same sequential numbers as above: 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011.  The decimal number 1234 in binary is 10011010010. 
Since even reasonable numbers that we use all the time make for very long binary numbers, the bits are grouped in 3s and 4s which are simple to convert into numbers in the octal and hexadecimal number systems.  For octal, we group three bits together.  Take the binary equivilent of decimal 1234, 10011010010, and put spaces in between each group of three bits - starting at the right and going left.
10011010010  =  10  011  010  010
Now use the symbols 0, 1, 2, 3, 4, 5, 6, and 7 (eight symbols, so OCTAL) to replace each group.
10  011  010  010  =  2  3  2  2  =  2322
The octal representations of the binary patterns are certainly easier to read, write, and remember than the binary counterparts.  An even more compact representation can be achieved by grouping the bits in chunks of four and converting these to hexNumerals.
When you group four bits together and use sixteen symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, A, B, C, D, E, and F) as their abbreviations, you have a hexadecimal representation.
10011010010  =  100  1101  0010  =  4D2

As you continue to explore how computers work, you'll hear more about numbers expressed in octal and hex; these are just more manageable representations of binary information - the digital world.
Table 1.2 compares the decimal, binary, octal, and hexadecimal number systems.

Decimal
Number
in
Binary
in
Octal
in
Hex
1
1
1
1
2
10
2
2
3
11
3
3
4
100
4
4
5
101
5
5
6
110
6
6
7
111
7
7
8
1000
10
8
9
1001
11
9
10
1010
12
A
11
1011
13
B
12
1100
14
C
13
1101
15
D
14
1110
16
E
15
1111
17
F
16
10000
20
10
Table 1.2

So, if the most common groupings of bits in a computer are 8, 16, 32, and 64, what kinds of numbers can these groups represent?
A group of eight bits has binary values 00000000 through 11111111, or expressed in decimal 0 through 255.  A group of sixteen bits has binary values 0000000000000000 through 1111111111111111, or decimal 0 through 65535.  I'm not going to type in binary representations for groups of 32 and 64 bits.  The range of decimal values for a group of 32 bits is 0 through 4,294,967,295.  The range of decimal values for a group of 64 bits is 0 through 18,446,744,073,709,551,615 - or almost eighteen and a half quintillion.
But wait... these numbers are all positive (Whole Numbers).  If we are going to allow for subtraction operations on numbers, which can result in negative numbers, we need Integers.  Modern computers use one bit in each of the groups to represent the sign (positive or negative) when the groups are used to represent integers.  Table 1.3 shows the range of numbers that can be represented with groups of 8, 16, 32, and 64 bits.
 Number
  of Bits
 Unsigned Maximum Value   Signed Minimum Value   Signed Maximum Value 
8   255   -128   127  
16   65535   -32768   32767  
32   4,294,967,295   -2,147,483,648   2,147,483,647  
64     18,446,744,073,709,551,615     -9,223,372,036,854,775,808     9,223,372,036,854,775,807  
Table 1.3

That's about as deep as I want to get into the representation of numbers in computers and the binary, octal and hexadecimal number systems.  Yes, computers have division operators but I am not going to cover numbers that include fractional parts, i.e., the "rational" and "irrational" numbers due to the complexity of their implementations.  If you want to read more, I googled and found what looks like a good place for you to read more.  Start at All About Circuits - Systems of numeration and read through it and continue on for a few more web pages in the series.

Inside Computers - Bits and Pieces

        Your computer successfully creates the illusion that it
        contains photographs, letters, songs, and movies. All it
        really contains is bits, lots of them, patterned in ways
        you can't see.  Your computer was designed to store just
        bits - all the files and folders and different kinds of
        data are illusions created by computer programmers.

        (Hal Abelson, Ken Ledeen, Harry Lewis, in "Blown to Bits")
   
Basically, computer instructions perform operations on groups of bits.  A bit is either on or off, like a lightbulb.  Figure 1.1_a shows an open switch and a lightbulb that is off - just like a transistor in a computer represents a bit with the value: zero.  Figure 1.1_b shows the switch in the closed position and the lightbulb is on, again just like a transistor in a computer representing a bit with the value: one.
Figure 1.1_a
Figure 1.1_b
A microprocessor, which is the heart of a computer, is very primitive but very fast.  It takes groups of bits and moves around their contents, adds pairs of groups of bits together, subtracts one group of bits from another, compares a pair of groups, etc... - that sort of stuff.
Inside a microprocessor, at a very low level, everything is simply a bunch of switches, also known as bits - things that are either on or off!  Time to expand on how this is done; first let's explore how groups of bits can be used to form numbers.

Programming Languages (Assembler Language)

One step above a computer's native language is assembler language.  In an assembler language, everything is given human-friendly symbolic names.  The programmer works with operations that the microprocessor knows how to do, they have symbolic names.  The microprocessor's registers and addresses in the computer's memory can also be given meaningful names by the programmer.  This is actually a very big step over what a computer understands, but still tedious for writing a large program.  Assembler language instructions still have a place for little snipits of software that need to interact directly with the microprocessor and/or those that are executed many, many, many times.
Table 1.1 is an example of DEC PDP-10 assembler language, a function that returns the largest integer in a group of them, named NUMARY.  The group contains NCOUNT elements.
 Label  OpCode Register  Memory
Address
 Index
Register
Comment
  GETMAX:     MOVSI   T1   400000       ; init T1 to smallest integer
     MOVE   T2   NCOUNT       ; get number of array elements 
  GTMAX2:   SOJL   T2   [POPJ P,]       ; decr idx, if -1 then done
     CAMG   T1   NUMARY     (T2)    ; skip if T1 > array element
     JRST      GTMAX2       ; continue with next number
     MOVE   T1   NUMARY     (T2)    ; T1 gets new max number
     JRST      GTMAX2       ; continue with next number
Table 1.1

I'm showing you this so that you will have a feel for how primitive computer instruction sets are.  I'm not going to go into the details of every instruction.  If you want to go through it in detail on your own, the PDP-10 Machine Language is detailed here.
A few points I want to expose you to are the general kinds of things being done.
  1. moving objects (numbers) into the computer's registers - very fast temporary storage,
  2. decrementing the value in a register,
  3. comparing the contents of a register to some value in memory, and
  4. transfering control to an instruction that's not in the standard sequential order - down the page.
So, as you've seen, higher-level programming languages provide similar functionality and in a form that is closer to the English language. 
But there is a problem with assembler language - it is unique for every computer architecture.  Although most deskside and notebook computers these days use the Intel architecture, this is only recently the case.  And... a variety of computer architectures are commonly used in game systems, smart phones, tablets, automobiles, appliances, etc...
Ok, we are almost at a point where I can show you machine language, the *native* language of a computer.  But for you to understand it, I'm going to have to explain how everything is represented in a computer.