Masthead

How Computers Work

1. Overview of computer hardware

Before we learn more about programming it will help for us to know a little more about how computers work.  You may have seen problems when you multiply two images together and the resulting image has bands of color instead of is continuous. The information below will help you avoid these problems and give you a greater understanding of what you can do with scripts and how to avoid problems.

Each computer contains; 1) a central processing unit (CPU), 2) Read-only memory (ROM), 3) Random access memory (RAM), and 4) an input and output system.  The CPU is where the computer does all computational tasks and is used to move data between the other components.  ROM is memory that cannot be changed and contains the program that the computer uses when it is first turned on (booting up).  RAM is where all the other programs and data reside.  The important characteristic of RAM is that it goes away when you turn off your computer.  This is why you need to save your work to a file before turning off your computer.  Everything else in a computer is part of getting data into and out of the computer.  This includes serial ports (RS-232), parallel ports (pretty much obsolete now), universal serial bus (USB) ports, disk interfaces for floppy and hard disks, network interfaces, sound interfaces, and others. 


Diagram of the 4 major groups of components in any computer. 

All a CPU can do is read and write to the other components and perform computational functions.  When we talk about the speed of a computer we are typically talking about the “clock cycle” of the CPU.  There is a clock that goes into the CPU that determines how fast it runs.  This is similar to someone beating a drum in time for dancers.  The faster the drummer beats, the faster the dancers dance. 

We mentioned that all the CPU can do is read and write to the other components.  This is how all the input and output works.  If we play a music file the CPU is reading from the music file and writing the data into a music interface which converts the data into electrical signals. These signals make a speaker vibrate which creates sound.  Most of the other output devices are actually simpler.  When we move a mouse or type on a keyboard the computer receives an interrupt and jumps to a special function that handles the keyboard or mouse input.  This input is then read by the current application or the operating system.

Inside the CPU is some RAM called a “cache”, some registers, and an “arithmetic-logic unit” (ALU).  Since it takes longer to load data from RAM external to the CPU the manufacturers place a small amount of very fast RAM into the CPU where code near the current point of execution and data that has been used recently will be stored temporarily.  The registers are used to hold data while it is being worked on by the ALU.  The ALU is where all the computational functions occur and they only include arithmetic and logic functions, thus the name ALU.  Think of the ALU as a hand-held calculator within the CPU.


Simplified diagram of a CPU.

The operation of a computer is fundamentally very simple.  When the computer is turned on it started executing the first “code” in the ROM.  The ROM then reads a program from the hard disk and begins executing it.  This program then runs the operating system.  Whenever the power is turned on to a computer it is getting the next instruction “code” from memory, executing it and getting the next instruction and so on.  The next instruction is always pointed to by a special register in the CPU called the “Program Counter”.  When we create a “For” loop or call a subroutine, we are changing the value of the Program Counter to move the point of execution to loop or go into the subroutine. 

When we add two numbers together the computer first loads each number from RAM into one of the registers.  Then it adds them together using the ALU and puts them back into one of the registers.  Then it stores the new number back to ram.

This information applies to pretty much all modern computers and will help us to work with them in the future and understand why they can be so good at something’s and so very bad at others.

2. Computers have only one finger

Long ago in grammar school we used to count with our fingers.  It is believed that our current base-ten number system evolved from folks counting to ten on their fingers and then placing a rock to represent 10 and counting on their fingers again.  When they used up all their fingers they would add another rock to the pile.  This pile represents the “tens-place” today.  Later on someone realized they could create keep the 10s-place pile from getting to big by creating a pile for the “100s-place” and so on.  If we add an initial pile to represent our fingers and then replace the numbers of rocks in each pile with digits, we have our modern system of counting.

Image with the words, there are only one zero types of people in the world: those who understand binary and those who don't

Computers are just simple switches.  Each element, or “bit” in a computer can only be on or off, that’s it!  In other words when a computer starts counting it turns on a bit for one.  There are no twos so when another 1 is added the computer has to place a rock in what we call the “10s-place” but which is actually the “2s-place” in computer math.  This creates “10” (which is pronounced one-zero).  If we add another 1 we have “11” (one-one).  If we add another one we don’t have a 2 so we have to move to the next place or “100” (one-zero-zero).  This process continues until we have a full set of binary numbers as represented below.  This system is known as “binary” because each digit can have only one of two values.

Decimal

Binary

Hex

0

0

0

1

1

1

2

10

2

3

11

3

4

100

4

5

101

5

6

110

6

7

111

7

8

1000

8

9

1001

9

10

1010

A

11

1011

B

12

1100

C

13

1101

D

14

1110

E

15

1111

F

Early in the development of computers programmers used binary to program computers and enter data into them.  If you watch old movies you may see computers with rows of switches on the front of the computer and lines of lights.  The switches were used to “input” binary numbers and the lights were the output.  Things have gotten easier.

It did not take long for folks to recognize the limitations of the binary system but since computers are fundamentally based on a different numeric base, the industry has converged on base-16 for computers.  Base 16 allows us to have 4 binary digits (bits) represented by each digit.  Because these numbers range from 0 to 15 (16 digits), we use the letters A through F to represent the values 10 through 15.   This gives the hexadecimal number system.

Note
If you have created HTML web pages you may have already been using hexadecimal numbers.  The colors in HTML are often represented with values such as #ffffff for white and #000000 for black.  These are hexadecimal numbers with the first 2 digits for the amount of red, the second two for the amount of green and the last two for the amount of blue.

Before we go further we have to talk about the most important mathematical concept for programmers 2^N (drum roll here).  Because we only have 2 digits (0 and 1) the amount each digit is worth goes up by a factor of 2 each time we move to the left in binary system.

Number of binary digits

Equation

Number of possible values

Minimum Value

Maximum Value

1 (bit)

2^1

2

0

1

2

2^2

4

0

3

3

2^3

8

0

7

4 (nibble)

2^4

16

0

15

5

2^5

32

0

31

6

2^6

64

0

63

7

2^7

128

0

127

8 (byte)

2^8

256

0

255

9

2^9

512

0

511

10

2^10

1024

0

1023

12

2^12

2048

0

2047

15

2^15

32768

0

32767

16 (word)

2^16

65536

0

65535

20

2^20

1048576

0

1048575

24

2^24

16777216

0

16777215

31

2^31

2147483648

0

2147483647

32

2^32

4294967296

0

4294967295

48

2^48

281474976710656

0

281474976710655

64

2^64

18446744073709551616

0

18446744073709551615

 

You have probably heard that your computer has X Mega or Giga bytes of RAM.  The byte is an 8-bit value.  If you check the table above to see have many values a byte can have and you’ll find it can have one of 256 values from 0 to 255.  You may have seen the value 255 as a maximum value in the past on computers.  Check the table and see if there are any other familiar numbers.  2^10 can represent 1024 values and one “K” byte is 1024 bytes (not 1000, no metric system here!).  2^20 is 1048576 and is one “Mega” byte.  2^32 is just over 4 billion and is a gigabyte.  You don’t have to worry about memorizing these values just reference the table when you need to.  There is an expanded table in the appendix which you will want to refer to as we work more with numeric variables.

Somewhere along the way the computer industry picked up a “byte”, or 8-bits, as being the fundamental “chunk” of data that we would use to represent just about anything.  Bytes will pop up over and over again and when we talk about larger numbers we will talk about how many bytes they contain.

© Copyright 2018 HSU - All rights reserved.