Before moving on to understand how computers process information, let's look at the different parts of a computer system.

A computer system can be broadly classified into two of its components. Do you want to guess which are these two components I am referring to?

A computer system consists of two parts computer hardware and computer software. Let's start with the computer hardware.

### Computer Hardware

A computer system is primarily composed of the following hardware components:

• Processor
• Input & Output Devices
• Memory Devices
• Main Memory
• Secondary Memory or Storage

#### Processor

Let's start with the processor. What do you think does the processor do?

A computer is a machine that can automatically carry out a sequence of arithmetic or logical operations.

An example of an arithmetic operation is adding two numbers or subtracting two numbers.  While an instance of logical operation is to check if two different quantities are the same.

The processor does the primary task of carrying out the instructions. The rest of the computer systems' function is to provide the information or instructions that need to be processed and electrical power.

A processor has a small amount of memory that it uses to hold data and instructions for processing. A computer processor accepts binary data as input, processes it according to instructions stored in its memory, and returns the output in the form of binary data. A processor does the most critical work of the computer. Therefore, we often refer to a processor as the brain of the computers.

#### Input & Output Devices

Next are input and output devices. What do you think is the role of the input and output devices?

Input & Output devices help make the computer system interact with the outside world by moving data into and out of the system. Input & Output devices are commonly referred to as I/O devices.

Table 10: Examples of input & output devices
Input Devices Output Devices
Keyboard Monitor
Mouse Printer
Webcam Speaker
Networking Networking
In table 10, why do you think that networking is mentioned in both Input and Output Devices?

When you connect the computer to the internet or a local network, you can move data into the system and move it out of the system. Therefore, the network interface acts both as an input and output device.

The third computer hardware we will look into is memory. Why don't you guess what role the memory plays in the computer?

#### Memory

Computers use memory to store data.

There are two types of memory, Main Memory and Secondary Memory.

Both the memory stores data in the form of bit patterns. Although the way they implement bit patterns differs significantly. Let's start with main memory.

##### Main Memory

Main memory is as vital as the processor chip to a computer system. Fast computer systems have both a fast processor and a large, fast memory.

A processor chip only has enough memory to hold a few instructions of a program and data they need for processing. As a processor chip has comparatively less memory, complete programs, data and instructions required by the processor are stored in the main memory.

The main memory is intimately connected to the processor, therefore moving instructions and data into and out of the processor is pretty fast. The main memory consists of silicon chips, which has several million transistors that can act as binary devices. These transistors are used to store the bit pattern of incoming data and transmit it to the processor.

The main memory consists of a very long list of bytes. In modern computers, each byte has an address that is used to locate it. Figure 15 shows a small part of the main memory.

Each box in this picture represents a single byte. Each byte has an address. In this picture, the addresses are the integers to the left of the boxes: 0, 1, 2, 3, 4, ..., and so on. Most computer memory addresses start at 0 and go up in sequence until each byte has an address. These memory cells can be accessed in any order or randomly, so the main memory is sometimes called Random Access Memory (RAM). When a computer is said to have 8 Gb of RAM, they usually talk about the main memory.

The data stored in the main memory can quickly and easily be changed. For the data to persist, it requires constant electricity. Such memory is also called volatile memory because the data stored evaporates as soon as your machine loses power.

If main memory loses all the data as soon as the machine loses power, how do you think computers can store data?

Anything you wish to keep for the long term is kept in secondary memory or storage. Let's next look at the secondary memory.

##### Storage

Secondary Memory or Storage is where data is stored for a long term basis. Examples of storage devices include hard disks, DVDs & portable hard disks. Unlike the main memory, the secondary memory doesn't require constant electricity to store the data.

Once you write some data onto the storage, it stays there even after the computer is turned off. The secondary memory, which is usually hard-disk inside a computer case, usually has larger storage than the main memory.

The reason for the persistence of data is the way bits are implemented in the secondary storage. For instance, a magnetic storage device has a large shiny, circular plate of magnetic material called a platter, divided into billions of tiny areas. Each area can be independently magnetized ( to store a 1) or demagnetized ( to store a 0). Therefore, the information stored in this magnetic disk persists even after switching the power off. All the disks and heads are sealed into a dust-free metal can.

Writing data to a hard disk is a slower operation compared to the main memory.

There are two types of memory in a computer due to the different needs they fulfill.

Table 11: Shows the difference between primary memory and Secondary memory
Primary Memory Secondary Memory
Relatively more expensive Relatively less expensive
Lower Capacity Larger Capacity
Directly connected to the processor Not directly connected to the processor

Table 11 shows an important distinction between primary and secondary memory. In both the main and secondary memory, information and data are stored as patterns of bits.

Hard disks (and other secondary memory devices) are used for long-term storage of large blocks of information, such as programs and data sets. The data stored in the secondary memory is organized as files.

A file is a collection of information that has been given a name and is stored in secondary memory. The information can be a program or can be data.

There are different types of computer files, designed for different purposes. A file may be designed to store a picture, a written message, a video, a computer program, or a wide variety of other kinds of data. Some types of files can store several types of information at once.

The form of the information in a file is the same as with any digital information — it consists of bits, usually grouped into eight bits. A group of eight bits is called byte. Files are often quite large. Therefore their size is measured in usually measured in kilobytes (kb) or megabytes (Mb).

Data is stored in a computer in the form of bit patterns. How do you think a user interacts with the data stored on the computer?

We can interact with the files by using appropriate computer software, which we will look into in the next section.

### Computer Software

A computer software is a collection of data and instructions that tell the computer's physical hardware how to work. A set of instructions is called a called computer program.

We can define computer software as a collection of data & instructions that tell the computer's hardware how to function.

As the processor processes all of the computer instructions, how do you think the instructions are represented?

The instructions are written in or compiled to a representation that the processor can understand, which is binary. Let's look into the different types of instructions in a computer.

There are two types of computer software.

Applications

Applications are a set of programs that is designed for users to use directly. Examples of such applications are Spreadsheet software, Word Processor, Web Browsers, and Computer Games.

System Programs

System Programs are a set of programs and software platforms which provide services to other software. The most important example of a system program is an operating system. Let's look at it in detail.

#### Operating System

An operating system or O.S. is a software program that enables the computer hardware to communicate and operate with the computer software. An operating system performs all the basic tasks like file management, memory management, handling input and output, and controlling other devices such as a keyboard, printers, and display.

An operating system is a vital part of any computer system. Can you name an operating system?

There are many operating systems available such as Linux, Windows, macOS, etc. Most computer systems can potentially run several OSs, such as Linux and Windows.

When a computer is started, the hardware first loads the operating system and keeps it running. Once the operating system runs, the user can begin interacting with applications and programs stored in the secondary storage.

The applications on a computer depend on the operating system to help them manage tasks. When you press a key on the keyboard ( hardware ), the operating system's job is to detect which key was pressed and provide the application's information. Upon receiving information from the O.S., the application responds to the user input.

The following is a high-level overview of what happens when a user interacts with an application.

• The user clicks an application icon to start it.
• The O.S. first determines the application name.
• Then, it determines the related files on the secondary memory / hard disk where the application and its data are stored.
• The O.S. finds an unused section of main memory that is large enough to hold the application data
• The O.S. makes a copy of the application and its data in that portion of the memory
• The O.S. arranges further resources for the application
• Finally, the O.S. starts the application running.
• As the application runs, O.S. manages background resources as well as providing input/output operations for the applications

We earlier mentioned that the main memory is directly connected to the processor. Keeping the contents in the main memory helps the processor execute the instructions written in the program faster.

Based on the above overview, can you rearrange the following operations in opening a web browser?

1. The browser starts running.
2. The OS finds an unused section large enough to hold the application related data and files of the browser.
3. The OS finds the files related to the browser application in the storage.
4. The OS copies the browser-related files onto the main memory.
5. The user clicks on the browser icon to start the application
6. The processor starts processing the instructions stored in the main memory

In this topic, we covered different components of a computer.  Let's understand how the processor executes the instruction in the next section.

### The Processor Revisited

To understand how a processor executes the instruction stored in the main memory, we will need to revisit the processor. There are different types of processors available, but the three things are common in the processors. A processor has a control unit (CU), an arithmetic logic unit (ALU) and sets of registers. These components are shown in figure 16.

#### The Control Unit (C.U.)

The control unit contains digital circuitry that uses electrical signals to direct the computer system to carry out or execute stored program instructions. The control unit does not execute program instructions itself. It merely directs other parts of the system to do so.

The control unit must communicate with both the arithmetic/logic unit and memory.

The next component that we will look into is the Arithmetic Logic Unit (ALU). Why do you think the Arithmetic Logic Unit (ALU) is called so?

#### The Arithmetic & Logical Unit ( ALU )

An arithmetic logic unit (ALU) is a digital circuit used to perform arithmetic and logic operations.

Most of the processor operations are performed by one or more ALUs, which reads data from registers. The arithmetic/logic unit (ALU) contains the circuitry to perform various arithmetic operations such as addition, subtraction, multiplication, division, and logical operations. A logical operation is usually a comparison. ALU can check for six logical relationships: equal to, less than, greater than, less than or equal to, greater than or equal to, and not equal.

The arithmetic and logical operations may seem insignificant, but these operations are the building block of modern computing.

The ALU is heavily dependent on the registers for reading or storing data.

Can you think of an instance when you need to store data in an arithmetic calculations?

When you multiply two larger numbers, you often hold some information outside and access it later.

Figure 17  shows how values are stored during multiplication. Similarly, the registers operate to hold information that is currently being processed.

Now, let's look at registers.

#### Registers

Registers are temporary storage areas for instructions or data. They are the smallest data holding elements that are built into the processor itself. However, they are not a part of the main memory. Instead, they are special storage locations that offer the advantage of speed.

Registers work under the control unit's direction to accept, hold, and transfer instructions or data and perform arithmetic or logical comparisons at high speed.

The fundamental difference between the register and memory is that the register holds the data that the CPU is currently processing. The memory contains program instruction and data that the program requires for execution.

Table 12: Different storage elements in a computer
Storage Elements Relative Access Speed Relative Capacity Relative Cost Permanent
Registers Fastest Lowest Highest No
Main Memory Fast Low/Moderate High No
Secondary Memory Slow Very High Low Yes

Earlier, we mentioned two other memory devices, main memory, and secondary memory. Table 12 illustrates how registers compare to the other memory.

Now that we covered a processor's components let's see how the processor interacts with the main memory.

How do you think the processor interacts with the main memory.

1. A processor can read bit patterns from the main memory
2. A processor can write bit patterns to the main memory
3. Both 1 and 2
4. None of the above

There are two ways in which the processor interacts with the main memory.

• It can write a bit pattern to a byte at a given memory address, which results in the previous bit pattern in that location will be destroyed. The new bit pattern is now saved for future use.
• It can read a bit pattern from a byte from a given memory address. The processor gets the bit pattern stored at that location. The contents of that location are NOT changed.
What kind of information do you think are stored in the form of bit patterns in the main memory?

The memory addresses in the main memory hold Instructions and data. The instructions are for the processor to execute while data are pieces of information required by the processor to execute the instruction.

#### Instructions

Given that you are aware that the computer executes arithmetic operations, can you think of a sample operation for a processor?

A sample arithmetic operation can be an addition or division of two numbers.

Let's take a simple arithmetic operation, say multiplication.

When you multiply two numbers, you usually write them in the following syntax:$$\text{Multiplication Operation : }a \times b$$ What is the symbol x called in the multiplication operation?

1. Operand
2. Operator
3. Operazor
4. Openheimer

In the previous exercise, the symbol $\times$ is called operator  while $a$ and $b$ are called operands.

Now that we identified operator and operand using an example, I would like you to define the terms operators and operands in your own words.

An operator is a symbol that represents a specific operation, whereas the operands are the inputs for operation.

Similarly, an instruction for a processor consists of operation code and operand.

Opcode (an abbreviation of operation code) is the portion of an instruction that specifies what operation is to be performed by the processor.
Operand usually specifies the data the operation is to be applied. The operands can represent a memory address of the value, register, or value itself.

Instructions are patterns of bits that correspond to different commands that a machine can carry out by physical design. Each type of processor is designed to understand and perform a specific group of instructions. The group of instructions is called the instruction set of the processor. Like the different languages that people understand, different processors have their own instruction set, which they can understand.

Examples of commonly used instructions that a processor are an addition, multiplication, subtraction, fetching values from memory, jumping, and moving bits.

Let's look at a generic instruction to understand how processors execute instructions. We will take an imaginary processor with the instruction set shown in Table 13.

Table 13: Sample instruction set for an imaginary processor
Instruction Set Opcode in binary Opcode in hex
Multiplication 0b10001011 0x8B
Addition 0b10001010 0x8A
Subtraction 0b10001001 0x89

We know that the instructions need to be in binary for the processor to read and understand. Instructions and data written in language consisting of binary numbers that a processor can read and interpret is called machine language, a language that the machine understands.

The first few characters of machine language instructions usually represent the opcode(s), while the rest represents the operands. Let's say we want to execute the following instruction.

Multiply the values in the memory address 0b1001000 and 0b111111 and store the result in 0b1001110. The opcode for multiplication being 0b10001011.

We can represent the instruction in machine language in the following form.
$$\text{Sample Instruction : }\overbrace{\text{10001011}}^{\text{opcode}} \underbrace{\text{ 1001000 111111 1001110}}_{\text{operands}}$$
The sample instruction's initial part represents the opcode for multiplication, while the operands represents the main memory addresses. The sample instruction is written in machine language or binary. Although we cannot make sense of it, it is something our imaginary processor will be able to perform.

Machine Language written in pure binary seems to take a lot of space for so little instruction. Can you think of a more compact representation of the instructions for the processor other than binary?

Earlier, we also read about the hexadecimal system. It turns out that Machine language can also be expressed in hex-format (hexadecimal) - the number system with base 16.

The binary system and hex are very interrelated with each other. It is easy to convert from binary to hex and restore from hex to binary. We can rewrite the above sample instruction in hex in the following way.
$$\text{Sample Instruction in hex : }\overbrace{\text{8b}}^{\text{opcode}} \underbrace{\text{ 48 3f 4e}}_{\text{operands}}$$
The instructions look more compact now but still unreadable for humans. To understand the machine language instruction, we will need to refer to the instruction set of our imaginary processor.

This is a tedious process.

In the early days of computing, all the instructions were hand-coded in machine language. Can you think of some disadvantages of coding directly in the machine language?

Machine Language instructions proved to be quite challenging for humans.

One obvious disadvantage was the need to memorize the several opcodes of the processor's instruction set.

The second was the need to carefully write the exact memory addresses as it is prone to mistakes.

Researchers developed a new language to make it easier to work with machine language, called assembly language. The assembly language replaced the numerical operation codes with Mnemonics codes that was much easier to remember and allowing symbolic address names instead of a numerical memory address.

To understand mnemonic codes, let's create some mnemonic codes for our imaginary processor in the table 14.

Table 14: Mnemonic Assembly Opcodes
Operation Numerical Opcode Assembly Opcode
Addition 8a ADD
Multiplication 8b MUL
Subtraction 8c SUB

Table 14 shows the assembly mnemonic opcodes corresponding to each numerical opcode. Mnemonic codes make it easier for humans to write instructions for a processor without memorizing the numerical opcodes. Whenever you need to use the opcode for subtraction, you can write SUB instead of writing the numerical opcode 8c.

We should note that the choice of the memory address in the above code is arbitrary. Any address that is not already filled can be used for storing or reading values.

Programmers using assembly language don't have to worry about the actual memory address because each data address is referred to by a name, called symbolic names.  You can store the memory address as a symbol or strings of character and then use it instead of writing the full memory address each time you require it. For instance, say we assign the memory address 0x48 with the symbolic address name length. Whenever we wish to refer to the address 0x48, we can use the address length instead.

Earlier I mentioned that the processor could only process machine language which binary or hexadecimal. How do you think the processor can execute the instructions written in assembly language?

The short answer is that the processor doesn't need to deal with the assembly language directly.Before the assembly language instruction is ready to be processed, it is first converted to the corresponding machine language using assembler.

There are several assembly language variations, each designed to work with a particular processor. We will rewrite our machine language instruction into an imaginary assembly language designed to work with our processor.

We can rewrite the above instruction again in an imaginary assembly code by storing the three memory addresses in the symbolic address names length, height, and area.
$$\text{Sample Instruction in Assembly : }\overbrace{\text{MUL}}^{\text{opcode}} \text{ } \text{ } \underbrace{\text{ length height area}}_{\text{operands}}$$
Now, the instruction is a bit clear. We can read the instruction as follows:

Multiply the values stored in length and height memory address and store the result in memory address area.

The main memory holds both instructions and data required for the execution of the instruction. Let's store the instruction at an arbitrary memory address, 12f. We will also store operands value in the addresses referenced by symbolic address length as 7 (0x7) and height as 20 (0x14). Table 15 shows how our main memory might look before executing the instruction.

Table 15: Memory addresses before executing instruction
0x12f 303 0x8b 48 3f 4e
0x48 72 length 0x7
0x3f 63 height 0x14
0x4e 78 area 8c

Which of the following is true for the main memory shown in the table 15?

1. The memory address 0x12f holds the instruction
2. The memory address 0x48 holds the value of the operand length.
3. The memory address 0x3f holds the value of the operand height.
4. All of the above is true.

Let's look at how a processor might execute this generic instruction stored in the memory.

Using the assembly language suddenly made our, seemingly gibberish, instruction readable. Now let's look into how our imaginary processor performs our sample instruction.

#### The Instruction Cycle

Earlier, we mentioned that when a program is executed, the operating system copies the main memory instructions. The processor executes a single instruction in the following three phases.

Fetch - Decode - Execute

#### Fetch

In the fetch step, the control unit requests that the main memory provide it with the instruction stored at a memory address indicated by the control unit's program counter. The process of fetching data is shown in figure 18.

A program counter, also called the instruction pointer, is a register that indicates where the computer is in its instruction sequence. It holds either the memory address of the instruction currently being executed or the address of the next instruction to be performed.

What do you think the control unit does after receiving the instruction from the main memory?

#### Decode

Once the instruction is retrieved from the memory address, the instruction is stored in the instruction register. Then the control unit decodes the instruction in the instruction register.

Instruction registers are registers that hold the instruction being executed by the processor. Figure 19 demonstrates the decoding step.

The control unit decodes the instruction in the instruction register into operands and opcodes and generates the ALU's necessary signal to execute the instruction. In our cases, it decodes that this is a multiplication operation that needs to happen. If any of the operands are memory addresses, the values in the memory address are fetched and stored in a register.

What do you think the ALU does after receiving the signal from the control unit?

#### Execute & Store

The arithmetic/logic unit then executes the instruction decoded by the control unit. The ALU performs the actual operation on the operand as per the operation code or opcode. This phase of execution is referred to as the Execute phase.

The control unit tells the ALU what operation to perform on that data. The ALU stores the result in an output register. This is shown in figure 20.

For our sample instruction, ALU performs the multiplication.

The ALU's result from the multiplication is stored in the memory address specified by symbolic memory address area.  The control unit moves the data between these registers, the ALU, and memory.

#### Repeat

The program counter register is automatically incremented for each instruction cycle to retrieve instructions sequentially from memory normally. The previous steps are again repeated for the next instruction provided by the program counter. The Fetch-Decode-Execute cycle is called one instruction cycle. It is one of the basic operations that a computer performs.

That's a brief overview of how the processor executes a single instruction. The processor executing a simple instruction might seem a little bit underwhelming. And most of the instruction sets are, in fact, consist of simple operations.

Then how does the computer can possess such versatile capabilities, you might think. That maybe has to do with the number of instructions a computer performs in a second.

Do you want to guess as to how many instructions does a processor performs in a second?

A modern processor executes over millions of instructions in a second. The speed of a processor is often measured in Millions of instructions per second (MIPS). A modern processor can do perform 100 thousand MIPS or 100 billion instructions per second.

#### Executable Program

A machine instruction consists of several bytes in the main memory. A collection of machine instructions in the main memory is called an executable program or machine language program.  The processor executes the corresponding operation for the machine instruction one at a time, but can perform billions of them in a second.

All the little operations based on the instructions adds up to something useful, such as playing a video or browsing the internet.

This also means that do something useful with the computer, we have to provide a huge number of machine instruction. Although assembly language is easier to work with than machine language, it is still tedious to work with assembly language.

In the next section, we will try to understand how we usually write a set of instructions or computer programs that a computer can execute without dealing with machine language.

### Summary

In this section, we covered how computers execute the instruction provided in machine language. In the next section, we will understand how we usually set instructions or computer programs that a computer can execute without dealing with machine language.