bit 100: basic hardware programming

algorithm poster.jpg

bit 100: basic hardware programming

how the CPU and RAM use a cycle of fetch, decode, and execute working with the ALU to perform arithmetic and logical computations

As we have already covered in previous sections, the CPU is the main essential processing system of the computer which performs arithmetic functions, fetches and stores data in conjunction with the RAM, and uses signals on the bus to coordinate input and output values. The accessories connected to the bus including the mouse, keyboard, headphones, etc. are able to function due to the CPU processing signals and comparing data to decide which function to perform next. Modern CPU’s operate at billions of functions per second, and are able to perform faster depending on the size of the RAM and local cache memory.

how programs operate

Any program, like Safari, Microsoft Word or Adobe Photoshop, consists of a mixture of instructions and data items stored in the RAM. When the program is launched, the CPU starts at a RAM location and performs a rapid cycle of fetch, decode, execute, repeat. It fetches instructions on how to load and display the program, as well as how it should run given other computational factors. It is important to note that these instructions are sequential and must make logical sense, otherwise the program will malfunction. The image below depicts a the basic cycle of processing, beginning with instructions from high-level programming languages all the way down to the instructions carried out by the CPU:

Screen Shot 2019-02-18 at 12.28.52 PM.png

At the core of this dialogue between the CPU and other hardware is a machine code called assembly language. Whereas assembly language is understood by human beings, machine code is the binary version of these commands understood by the machine. Since the CPU only understands binary, the assembler translates these instructions into bits which are then loaded directly into the RAM. The instructions consist of basic commands such as GET, PRINT, STORE, LOAD, ADD, etc., and the sum of these individual processes allows the program to run. Commands are based off the number in the part of the CPU called the accumulator, which works similarly to a calculator in the sense that it can only display the most recent ‘sum’ or value, and will be cleared out once a new value is entered.

Memory cells in the RAM store these single instructions using a numerical code. For instructions that require two parts, such as IFZERO or ADD, the memory location will occupy two cells. To access this information quicker, data locations are given a name which the program keeps track of in the RAM. It replaces this name by the proper numeric location. For example, GET=1, PRINT=2, STORE=3, etc. Each RAM location holds one number or one instruction, so a functioning program will consist of a mixture of instructions and corresponding data items stored in the RAM. The cycle looks something like this:

FETCH: get the next instruction from RAM (print, load, store, etc.)

DECODE: CPU decodes instruction to figure out what it does

EXECUTE: do the instruction by arithmetic or logic, store information if necessary, go back to fetch

When the program is about to begin, RAM contents are shown as a memory cell corresponding to a specific location, and the instructions and addresses that correspond to the individual commands.

algorithms

So how exactly are these functions carried out? Part of the CPU called the arithmetic logic unit (ALU) is a digital circuit which performs arithmetic and logical computations based on instructions given from the RAM. A single CPU may contain multiple ALU’s for faster computations, but all basic ALU’s are made up of three parallel data buses. Each data bus is a group of signals conveying one binary number, and when they work in conjunction, these buses consist of two input values and a resulting output value. The ALU is a combinational logic circuit, meaning that its outputs will change asynchronously in response to input changes. The commands performed by the ALU include basic arithmetic (ADD, SUB, MUL, DIV) and logical computations with a basis of >, <, =, AND, OR.

We discussed the cycle of fetch, decode, execute, which is carried out in part by the ALU. The instructions are fetched from the RAM or cache memory, decoded in the control unit, and then executed in the ALU before the results are stored back in the memory. This sequence of steps is called an algorithm, or the computer science version of a careful, precise, and unambiguous set of instructions. Each step is expressed in terms of basic operations. For example, the ADD instruction would perform an algorithm where the two integers are added together, and the sum is the basis for the next instruction.

For a more comprehensive explanation of algorithms, this crash course video sums up their basic function in regard to computation:

More info on the digital world coming soon…. Let me know if you are enjoying this series!