GML Blog‎ > ‎Fundamentals‎ > ‎

What a computer does (Actually)

 

This man, John von Neumann, started it all.  To this day, computers still use what is called the "von Neuman design".  It is the act of dismantling information or a problem into the most basic and reliable form achievable, then working at those pieces one at a time until the information or solution is complete.  All computers, and I do mean ALL computers, use this design.  Have you ever heard of a little thing called binary?  I call it little because it is.  Binary is a word that we attach to anything that is made up of two _____(Insert noun here).  In computer speak, binary means two states.  As long as those two states can be distinguishable, a computer can interpret a long series of them to form an image file of a dog, that song you listened to yesterday, or some pixels on your screen saying "You just got fragged the **** out!".  First person shooter jokes aside, binary can be mysteriously elegant as well as beautifully simple.  Spiders have 8 legs (2*2*2).  Ostriches have 2 eyes.  Ants count their steps (They are believed to be counted in binary).  Even the human brain is beginning to give researchers the idea that our memories are stored as binary.  You see, binary, although novel and technological in appearance, has had it's place in nature since the very beginning of life as we know it.

Storage and Interpretation

Binary, in a computer, is stored by either transistors or metal plates.  Think of transistors as tiny little buckets that can hold electricity or not hold any.  The metal plates have billions (Many have even more) of spots that can be magnetized or not magnetized.  By holding a charge or being magnetized, the transistor or spot on a hard disk is interpreted as a 1.  This is called a bit, it can be a 1 or a 0.  Lets look at the some places where bits are stored. 

  • Hard Drive:  The platter(s) inside a hard drive spin(s) very quickly (from 5,400 to 10,000 RPM).  Your hard drive is what remembers what level you were on in that platformer game, so that when you play tomorrow you don't have to start all over.  If you don't save your game, the next time you turn your computer on, the hard drive will only have what was saved to it.  Transistors are tricky, they only remember things if electrical current is passing through them.  Let's take a look at some places in a computer that house transistors.

  • Memory  Processor  Motherboard  Chipset  Graphics Processing Unit  Audio Card  Network Interface Card  Etc...

Now your probably wondering "Yeah, that's nice, but who cares if there are just 0s and 1s floating around". Ill tell you.  Everything can be represented by numbers.  The alphabet has a numbering standard called ASCII (Pronounced ask-E).  65 for example is A in ASCII.  Even written numbers have an ASCII counterpart.

If there is a way to represent EVERYTHING in numbers, why pick only 1 and 0? Because it is very reliable.  It's easier to make certain that a transistor is 1 or 0 rather than having the chance of it being mistaken for 3 or 4 or 268.


Execution

Lets get away from the physics/math part of things and indulge in a little programming.  How do you make a sandwich?  You do these things:

  1. Place slice of bread on plate

  2. Apply mayonnaise or mustard (Or both if you're feeling lucky)

  3. Place assorted meats, cheeses, and vegetables on "condamented" bread

  4. Place last slice of bread on everything else

See, when you program, you don't just make a game where there's a dude racing other dudes....dude.  You have to make every little thing to make it work.  The wheels, the cars, the dudes.  But what about the next race.  Do you really have to do it all over again.  NO, that's what sub-procedures are for.  Programming has many names for sub-procedures.  Methods, scripts, libraries, externals, are all considered sub-procedures.  When you know you're going to be doing a lot of one thing all the time, make that one thing ONE THING.  Example:  In Game Maker you can get the distance of two points by using point_distance().  The inner workings of point distance probably look like something out of a middle school math class.  You don't want to go through math class every time you want to check the distance between the missile and the no-fly-zone in your game, so a sub-procedure was made by Mark Overmars to help you get the point_distance() of two points.

How do sub-procedures tie into learning about the inner workings of a computer?  In order to make a language like Game Maker work, or a program such as VLC media player to play music, the only operations involved are addition, transportation, and comparing.

Yeah right!  You mean to tell me that when I play the latest rpg or watch a movie, the only thing going on inside my computer is ADDITION, COMPARING ONE THING TO ANOTHER, AND MOVING STUFF!!!

Exactly.  Multiplication is just adding stuff a bunch of times.  Division is only subtraction a bunch of times until your at zero, and when you've gone too far, just add one back.  Subtraction is just an addition but with a 2s compliment (Well get to this soon).  And comparing is just subtracting the first thing from the other thing.  If the subtraction results in a zero, they are equal.  Greater than if it is positive, less than if it's negative.
How does this addition and transportation happen?
I cannot explain exactly how since I don't know yet.  It also does not really pertain to higher level languages.  But later, during the data flow section, I will explain when and why this happens.  For now, I leave you with some lessons in binary.  If you know binary, or believe you have no use for it, just skip the lessons and head straight for the data flow section.  I suggest however, that you do not skip any part of the Fundamentals section as I believe each part will have it's use at some point in time.

How to Convert Binary to Decimal

How does the computer know that 0000 0101 is five or that 0000 1010 is ten?

128 64  32  16   8   4   2   1
[ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]

We will call this "the line". It is composed of 8 bits (1 Byte).
Every digit on "the line" marks a predefined number
The predefined numbers or "digit numbers" start at the right as the number 1 and double as you go left. The next number after 128 is 256 and the ones after that are 512, 1024, 2048, 4096 etc...

When a 1 is placed in a digit, it's "digit number" is considered to be added into the pile.  If there is a 0 placed in a digit, it is excluded.  When you go to each digit with a 1 in it and add it's contents to your pile
you end up with the decimal (Human readable) value of that binary number.

  8    4    2    1
[1]  [1]  [1]  [1]

1 1 1 1 in binary is 15 in decimal

But why?
Well, for starters, all digits are filled by 1s.
Sooo...?
So we count all the numbers together 8, 4, 2, and 1 to make 15.
So what you're saying is that 0 0 0 0 is 0 because there are no 1s to be seen?
Yes.
Lemme get this straight, a random binary number, say 1 1 0 0, is going to be 12 in decimal, since 8 + 4 + 0 + 0 = well you know...?
Exactly.
Cool :) WAIT!  How do I know which numbers are binary and which are decimal?
Just attach a b or d and most people will know what you mean.
Ooh.......OK!

How to Convert Decimal to Binary

To convert a decimal number ("your number") into a binary number you simply:

  1. Go from left to right along "the line"

  2. If the "digit number" is smaller or equal to "your number" Subtract the "digit number" from "your number" and put a 1

  3. If the "digit number" is too big to subtract from "your number" put a 0

How to Add Binary Numbers (The way CPUs do it)

Let's assume an addition of 6 to 5.
First we convert the numbers to binary

5  =  0101
6  =  0110

Next we add them column by column, right to left.
We do this in a special way though.
or a 1+0 or 0+1 we add normally. 
For a 1+1 we place a 0 and carry a 1.
For a 1+1 with a carried 1 (Three 1s) we place a 1 and carry a 1.

0  1  0  1
0  1  1  0

|  |  |  |
|  |  |  | 
|  |  |  +---> Step One:   1 + 0     = 1
|  |  |
|  |  +------> Step Two:   0 + 1     = 1
|  |
|  +---------> Step Three: 1 + 1     = 0  carry the 1
|
+------------> Step Four:  1 + 0 + 0 = 1


Answer is:   1 0 1 1   which is 11 in decimal

Let's do another addition in order to explain double carries.
Double carries will be shown in purple.
       11 1111    Carries
0110 1101 1111 1100  First number   28156
0000 0000 1100 1111  Second number  207
-------------------
0110 1110 1100 1011  Answer         28363


How to Add Negative Binary Numbers (The way CPUs do it)

To negate a binary number you just reverse all the bits (Make 1s 0 and 0s 1).  After that you just add 1 to that and your done.  You now have a signed binary number.  All signed negative binary numbers have a 1 in the left most bit.  A signed positive binary number will have a 0 in the left most bit.  A side effect of using negative numbers is that you lose a bit to show the sign. This means that instead of having a range of [ 0 to 255 ] in decimal, a signed Byte has a range of [ -128 to 127 ].

Here is an example of a positive 7 in binary turned into a negative 7 using 2's Compliment (That's what the method is called)

0000 0111 Normal 7
1111 1000 Reversed bits
1111 1001 Added 1

1111 1001 = -7 in signed binary

As a check to make sure this is correct we will add a +7 to the -7. If we are correct, the answer should be 0.

1 1111 111   Carries
  1111 1001  First Number  -7
  0000 0111  Second Number +7
  ----------
  0000 0000 Answer          0  CHECK!!!

The 1 at the far left might go into what is called a carry flag.  It's a register (Small amount of memory right on the CPU itself).  This flag exists to let programmers know that they used two numbers that were too big to fit in one operation.  That's what it means to be a 32bit processor or 64bit.  It's simply the largest number the processor can process in one operation.  If you want to do a mathematical operation that requires more bits, the programmer must store some of the problem in memory, and work on the other parts the same way until an answer is derived.  This is called double (or more) precision and is automatically included in many high level languages.

Here are some exercises you can do to help you remember these binary techniques

  1. Convert decimal number 11 to binary

  2. Convert decimal number 32 to binary

  3. Convert decimal number 2   to binary

  4. Convert binary number 1001 to decimal

  5. Convert binary number 1111 to decimal

  6. Convert binary number 0000 to decimal

  7. Add binary number 1110 to binary number 1101

  8. Add binary number 0000 to binary number 1111

  9. Add binary number 1010 to binary number 0101

  10. Add signed binary number 1111 to signed binary number 0110 and explain what sign (- or +) the answer is

When writing binary, you don't have to write the leading (leftmost) zeros but it looks neater that way.  Also, try to round up the amount of digits to multiples of 4 and separate every group of 4 by a space (Example: 0100 0011 0010 1111)
Exercise Answers

Data Flow

Data flow, explained here, may not be what you expected it to be. I just gave this section such a title because I think it is the best representation of what programs generally do.  They move data, calculate it, and move it some more.  The movement is flow, the data is data, eh...so here we go.

Just as the title to this section implies, data flows.  It is very important to know what data moves, when it does, and where it goes.  Generally speaking, especially if your working with Game Maker, all game data is loaded  into memory.  The memory on your computer can be thought of as a work table.  You can do work quite quickly on a work table, but even faster in your mind.  Think of a computer as three places.  Hard drive (Slow warehouse), RAM (Quick worktable), and CPU (Very small but fast mind).


Now that we have set the stage for what is going to be a VERY long list of actions that explain what happens to data in your program.


Data Flow =

  1. Load program into RAM

  2. Load first (or next) instruction from RAM into CPU

  3. CPU performs instruction (Either mathematical operation or moving one piece of data from one point in memory to another or loading a file from the hard drive into RAM)

  4. Return to step 2

"Long list my ***."
Yes, I know, it's not very long.  But it does happen very quickly and many, many times a second.  It is something that you have to pay very close attention to. 

Say you have a game that stores every variable of every object, every time something changes.  Of course I'm talking about a replay system of some sort.  Now you may be thinking "That's not a bad way to do it".  ITS A VERY BAD WAY TO DO IT.  Not only does all that data take up all that space, you practically have to double it because you need to address that space.  Addressing is just the way that computers keep track of what is where.  Hard drives have addresses (Tracks sectors etc...) that are masked for the end user in the form of folders.  Memory has addresses (stored as binary numbers) masked for programmers as variables.  All of this takes space.  Now a better approach to a replay system might be to store what buttons the user presses, and only store them when the user presses those buttons.  This will trigger all the right variables but only WHEN they are needed.  This will save you a ton of space. 

Another good example to illustrate the importance of knowing what your data is doing is the use of scripts.  Scripts are a good way to save space AND speed up your game.  In GML scripts have a function called var.  You can look it up in the Game Maker documentation but it essentially creates variables that are stored and used on the CPU cache itself.  This makes vars very fast and useful for complicated equations that have to be done in a for loop style.  You can also read about for loops in the GM docs. 

The last example I will leave you with is complex computations.  Addition, as you may have seen, is simple for a CPU to pull off.  Subtraction is slightly more complicated but still within the realm of "high speed".  Dividing a very large number by a very small number is not.  Sine, cosine, random number generating functions, point_distance(), are all sub-procedures.  They are basically scripts.  They take more CPU cycles than just plain switching two variables or adding 1 to 382.  Make sure to take care when using sub-procedures, as they appear to be small and insignificant, when in fact, they are composed of many additions and data moving operations (Sometimes in the hundreds) and can be very hazardous to the performance of your program.

Well, that's it for now. You now know the basics of how a computer does its thing. You can research more if your interested but you know enough to safely navigate this website. If you have any questions about a sub-procedure or function, just check the Game Maker documentation.

[Attention - This page uses a mono spaced font: Courier New.  If your OS/browser does not have this font, some texts may not be aligned correctly]

Affiliates