How a Computer Works

Most people have no idea how a computer actually works so I will attempt to provide the basic ideas in as simple a way as possible. Obviously I cannot explain everything here so I have left out a lot to keep things simple.

I am sure that all of you know that computer data is stored in bits. The usual unit of bits is the byte which is 8 bits long. It also has a sub unit called the nybble or nibble which is half a byte. You may also come across "octet" as an alternative to byte.

The bits can be referred to in binary code. Binary is just a way of counting which uses only two symbols. We use decimal code which has 10 symbols - 0 to 9. The binary bits represent or can be represented by voltage levels inside the computer.
0 volts = 0 and 5 volts = 1.
8 bits can be used together to represent any decimal number between 0 and 255

A micro processor works in two steps for each instruction. The instruction sets the microprocessor up to deal with the data that follows. Here, I show a simple program using the 6502 8-bit processor.

The program will add two numbers and store the result. Normally such a program would obtain the two numbers from a user input or some other source. Here, to make things simple, I have included the two numbers within the program. It therefore gives a pretty useless result but it shows the prinicples that are used in simple addition.
This processor selects memory locations in sequence. This is done by a program counter. The program counter has been shown in decimal for simplicity.

When doing addition it is necessary to tell the processor to clear the Carry Flag first. Otherwise it will remain in the state that it was in when the last program ended. I will explain the Carry Flag in more detail later.

After the Carry Flag has been cleared, the first number is loaded into the processing register. On the 6502 it is called the accumulator but other processors sometimes have several registers that can be used as processing registers or accumulators. The instruction in this case is LDA - LOAD immediate. This means that the number to be loaded is in the next memory location after the instruction.
The next instruction is ADC - ADD with carry immediate. This tells the processor to add the number following the ADD instruction to the number in the accumulator. If the resulting number is greater than 255 decimal or FF hexadecimal it cannot be stored in 8 bits. This is where the Carry comes in.

If you added 197 to 115 you would do it in stages. You would add 5 to 7 and get 12. You would write down the 2 and carry the 1. The 1 would be added to the next step of adding the 1 to the 9 giving 11. You would then have another carry so that you would now add the carry 1 to the other two ones giving three. The result is 312.
When we do arithmetic like this we can use as many carrys as we like. The 6502 can only work with 8 bit numbers between 0 and 255. That means that the addition of two numbers can never exceed 510. 510 can be written in 9 bits in binary so the carry acts as the 9th bit in the addition.
After doing the first addition the program can check if the carry flag has been set or not. If it has not, the processor can jump (branch off) to the next bit of the program. If the carry flag has been set, the addition has not been completed so the next step is to load zero in the accumulator and add zero with carry to the accumulator and store the result in another memory location. The addition of two zeroes to the carry gives a 1 so the addition of the two numbers will end up with the correct answer. The usual rule is to use successive locations to store numbers that are greater than 8 bits.

The instructions in order
320  CLC            (Clear Carry Flag)
321  LDA  #197  (Load immediate 197 = Hex C5  = Binary 1100 0101 in address 322)
325  STA    41     (Store the first part of the sum in address 41. 41 is in address 326 )
327  BCC   335   (If the carry is clear continue with the program starting at address 335. If not, do next instruction.)
329  LDA   #0     (The carry is set so load 0 to make sure that there is no number in the accumulator)
333  STA    42     (Store the result with the carry in address 42. The address number 42 is in address 334)
The number is now saved in two bytes in addresses 41 and 42. as Hex 38 and Hex 01 = Binary 0011 1000 and 0000 0001
Most computers store numbers with the lowest part in the lowest address if more than one address is needed.

The carry flag is just a bit in a part of the processor called the STATUS register. The status register keeps a track on the result of the instructions that the processor has dealt with. If a zero is loaded or is the result of a process, the zero flag is set. Other 6502 STATUS register flags indicate a negative result, an overflow, a software interrupt called a Break, Decimal computation status, and the presence of a Non Maskable Interrupt.
An interrupt is caused if something wants to interrupt what the computer is now doing because something else has to be done first. There are two sorts of interrupt. The first says stop what you are doing and do what I want you to do now. When you have done what I want you to do, you can go back and carry on where you left off. The second interrupt is: When you are free, will you do this job for me, then go back and carry on with your current job. The first is called a non-maskable interrupt - NMI. It always takes precedence over everything else. The other interrupt is called an interrupt request - IRQ. When the computer is processing the work involved with an NMI, another bit is set in the status register. This bit says: Do not answer any IRQs. This is to prevent an NMI from being interrupted. IRQs can be interrupted by NMIs because NMIs take priority. AN NMI is used when reading from or writing to a disk. The transfer of data to or from a disk cannot wait so the processor must deal with it first.
In a sense an interrupt is like a traffic light. A stream of traffic can be travelling along a road when a pedestrian presses the button on a traffic light at a pelican crossing. The stream of traffic is interrupted to let the pedestrian cross the road. When the pedestrian has had time to cross the road, the traffic can continue as it did before
The 6502 has a decimal mode for those who cannot work in hexadecimal. It is very wasteful of memory because the maximum number that can be held in a byte is 99 instead of 255.
Some of you may say that this description says what a processor does but not how it does it.  I cannot give a full explanation here but I can give you an indication of how a part of the processing works.

A processor like the 6502 uses a lot of logic gates. A logic gate is an electronic switch which gives one output or another according to the inputs it receives. A simple example is the principle of the AND gate. This gives a true output if all the inputs are true. Otherwise it gives a false output. True is usually a 1 and false is usually a 0. On a computer keyboard you are used to the shift key. If the shift key is pressed AND another key is pressed at the same time, the shifted key output is true. e.g. 4 AND shift give \$. Other gates work in different ways. An OR gate works if either input is true. On the keyboard you see the same result if you press the left shift key OR the right shift key (while pressing another key of course). An EXCLUSIVE OR gate gives a true output if any one input is true but not if there is more than one true input. I cannot think of a keyboard equivalent but perhaps you can. Another type of device is called a latch. The Caps Lock, Scroll Lock and Number Lock keys activate latches. A latch is set to give a true output or reset to give a false output. The bits in a processor's status register are set with latches. The registers in a processor are very much like memory units. Anything can be stored in them like memory although some of them like the status register can only have data stored in them in single specified bits.
A processor instruction consists of binary bits. The bits are used in combination to operate gates and latches to cause things to happen. If you look at the program I have provided I can tell you that there is a similarity between two instructions. LOAD Immediate is A9 and ADD Immediate is 69. The 9 part (1001) sets two bits and leaves the other two unset. This causes the processor to use the next memory location as the source of data. If you study the 6502 code sheet you will see that most of the instructions can be classified into sets. The LOAD instructions generally start with A (1010). The STORE instructions mostly start with 8 (1000). One bit makes the difference between input and output. An ending 5 (0101) means use a one byte address and a D (1101) means use a two byte (16 bit) address.
The processor usually processes an instruction in several steps. The processor's clock provides the stepping pulses that enable the processor to progress from one step to the next. If the clock pulses are applied in a certain way, one lot of gates will work when the clock is activating the start of an instruction and these gates will produce outputs that stop all the other gates from working at the time. Then, when the clock starts the next cycle, the first lot of gates is de-activated and the second lot is activated. This process continues until the instruction is completed.
Processor chips come in two sorts. One is the Complicated Instruction Computer (CISC) and the other is the Reduced Instruction Set Computer (RISC). The 6502 is close to the RISC end but it was not designed as such. The 86 series of processors are very much at the CISC end.
Acorn, who made the BBC Micro, found that the 6502 was a very efficient chip. They saw its advantages and its disadvantages. They saw that most computer instructions were fairly simple ones and the complicated instructions could be made up from simple ones. That meant that it was not necessary to include any complicated instructions within the chip. When they analysed the instructions to see what ones had to be included they saw that it would be possible to make a processor that did one instruction per clock cycle. (The 6502 uses three clock cycles per instruction on average.) A CISC processor for comparison has to use a sort of look-up table to work out how to do its instructions. These complicated instructions are dealt with by what is generally called a nano-processor in simple steps according to inbuilt microprograms. A CISC processor typically takes 7 or 8 cycles to do a simple instruction and up to 20 cycles to do a more complicated one. All CISC instructions have to be decoded before they can be executed so simple instructions take much longer with a CISC processor.
Roughly 80% of all programs are made up of simple instructions so a RISC processor will be faster than a CISC processor with the same clock speed doing the same job.
The 8088 processor used in the PCXT was designed to be used in mainframe computers when memory was horrendously expensive. It was better then to have a processor that could do complicated things inside itself than to have to use a lot of memory for a program. All new PCs have to be compatible with the old XT so that they will still run 8088 programs. The IBM clone PC is locked in to using a CISC processor.
Acorn used their knowledge to design a RISC chip set which was used in the Archimedes computer. The chip set was also used by Apple to run their laser printer faster than anyone else's. The chip set became the main reason for Acorn's existence so the company was renamed to the name of the Acorn RISC Machine or ARM Holdings. It will work faster than any Pentium becuse it can run at more that 100MHz. Every cycle is used for an instruction while the Pentium typically needs 10 to 12 cycles on average for its instructions. The ARM is designed as a set of four chips. One is the main processor, the second is the memory manager. the third is the input/output sound/video driver and the last is the numeric processor. The memory manager deals with everything connected with the memory including getting data from and sending data to the other chips and the disk system. The input/output chip spends most of its time dealing with video and sound. There is no need to have a high performance video card with an ARM. The main processor does not have to do any video processing at all. All video instructions are directly processed by the video chip and all numeric computations are dealt with by the numeric chip.
The ARM introduced the idea of pipelining. This means that a series of instructions enter the processor before being processed. They are checked to see if they will affect the status register and any test and branch instructions. Then the processor skips any instructions that will not be used because of the branch decision. This speeds up the way the processor works. Intel copied it in the Pentium.
An ARM computer can work like a PC or a Mac with the right interpreter software. However, because it has to do so much processing to provide the emulations, it will never be fast enough to compete directly in emulation mode with the latest PC or Mac.

A computer processor has to have a way of starting up without any instructions from outside. All processors include a very simple instruction that is effectively a reset. The reset signal tells the processor to set the address counter to zero and to load and process the instruction that is found there. The reset signal is produced either by the reset button or a simple timer. The timer starts working as soon as the power is switched on. Then, when the power supply has stabilised, the timer times out and sends a reset signal to the processor. The processor then starts working.

The term BIOS means Basic Input Output System. It is a program stored in a ROM - Read Only Memory. The first instruction that the processor gets is to change the address counter to another address and to then continue processing. This enables the BIOS ROM to have a start address above the basic user memory area. The BIOS ROM contains a series of instructions to test the functions of the computer to make sure that they are all working. On an ordinary start up a successful conclusion to the set of tests is marked by the production of a single bleep from the computer's sound device. This is usually a miniature loudspeaker. If some things do not work during these tests a signal is generated to indicate what has gone wrong. For example, most BIOSes produce eight bleeps if a video card is not plugged in or doesn't work at all. If the video card is plugged in OK and works, but there is no monitor connected, some BIOSes generate a long bleep followed by three short bleeps. If the RAM memory is faulty there will usually be a continuous tone or a series of long bleeps. There are other possible startup fault bleep signals but these vary a lot from company to company.
After the successful startup single bleep, any other problems are indicated visually if they occur because it is presumed that the video card and monitor are working and can give more information to the user than a bleep code. The startup test procedure is known in the business as the Power On Self Test or POST.

Once it has been settled that the computer electronics are working OK the next step is to prepare the processor and associated chips to work with a keyboard and a disk system. During this stage the BIOS provides the processor with instructions to try to activate the keyboard and disks connected to the motherboard. The BIOS then produces a message on the screen "Press Del (or Delete) to enter setup". Proprietary corporate computers made by Dell, HP, IBM etc usually have a different way to access the BIOS setup. Check with the manuals that come with these computers for more information.

The BIOS Setup is a programe that presents a series of screens to the user that are designed to accept inputs from the keyboard to modify the absolute basic settings. These include the Date and Time and a range of other settings that tailor the computer to the hardware fitted. A menu item on modern PCs allows the BIOS to run a program to test the hard disk system that is connected to the motherboard. This reads data recorded on the first track of each hard disk that is connected. The data found can be used automatically to configure the hard disk system for future use. When the BIOS setup has been completed the computer is re-booted in order to make use of the configuration data provided. The next step that takes place if the BIOS settings have been set up correctly is to to try to read either the floppy disk or the hard disk. The instruction is to look for a file called CONFIG.SYS and to process the instructions contained in the file. These instructions include setting up a CD ROM and the ways the keyboard and screen will work. When the list of instructions in CONFIG.SYS has been completed the BIOS instructs the system to look for and process the instructions in a file called AUTOEXEC.BAT. These instructions personalise the computer's settings for the user. With all versions of Windows there are more lists of setting up instructions to be processed. These include WIN.INI, SYSTEM.INI, PROGMAN.INI and several others. These tell the computer how the screen should look, what programs are available on the hard disk system that are ready to run, and what programs should always be running in the background or held in memory for immediate access.

When all these lists of instructions have been processed you will see a mouse ponter and not an hourglass symbol. Then you can run the programs of your choice.

If any of the explanations I have given seem unclear to you, please contact me and I will try to make them clearer.

Look for Linux wherever you see a fat penguin!

Miscellany Page