We will list the different hardware parts of a computer.
We will see what a program is and how it looks like.
We will understand how a program is loaded and executed.
Memory Unit, Arithmetic and Logic Unit, Input/Output, Control Unit
Central memory, Auxiliary memory
Volatile and non-volatile memory
RAM/ROM
CPU
High and low-level languages
Assembly language, assembler
Compiled and interpreted language
This book is about Go. Before jumping into our main subject, you need to know some basic knowledge about computers.
\nExperienced programmers can skip this chapter. Beginners should take some time to study it.
\nYour programs will run on hardware. Knowing how your hardware is working may improve the design of your programs.
\nFirstly we will describe the main components of a computer. Then we will see what a program is and how the machine handles it.
\n\nA computer is composed of four main parts :
\nThe memory unit (MU) where data and programs are stored.
\nThe arithmetic and logic unit (ALU). Its role is to perform arithmetic and logical operations on data stored into the memory unit. This unit can perform, for instance, additions, incrementations, decrementations, which are called operations. In general, each operation requires two operands and output a result.
\nThe input and output unit (I/OU) will be in charge of loading data into the memory unit from an input device. This unit also sends data from the memory unit to an output device.
\nAn input device is, for example, the touchpad of your computer, your keyboard, your mouse.
An output device is, for instance, your monitor.
The control unit (CU) will receive instructions from programs and will control the activity of the other units.
The four hardware components represent a schematic view of the computer’s components.
\n\nA computer is composed of two types of memory :
\nThe central memory
The auxiliary memory
Two categories of memory exist:
\nVolatile
Non Volatile
This type of memory is necessary to run the operating systems and all the other programs your computer will run. The central memory contains two types of storage :
\nRAM (Random Access Memory). This type of storage requires electric power to persist data. When you turn your computer off, the memory contained in this type of storage will be erased. The operating system and the programs you use will be loaded into this memory. This type of memory is volatile.
ROM (Read-Only Memory). This is a memory that contains data necessary for the computer to run correctly. This kind of memory is not volatile (when you turn the computer off, it will not be erased). It’s designed to be only readable and not updated by the system.
This type of memory is not volatile. When the power is going off, the data stored will not be erased. Here are some examples of auxiliary memory: Hard drives, USB keys, CD-ROM, DVD ...etc.
\nRead and writes to this type of memory is slow compared to the RAM.
\nSome hard drives sequentially access memory. The system should respect a particular sequence. Respecting this access sequence takes a longer time than a random access mode. Note that some hard drives allow random memory access.
\n\nHard drives, also denoted Hard Disk Drive (HDD), are composed of magnetic disks that are rotating. Data are read and write thanks to a moving magnetic head. Reads and writes will generate a rotation and a magnetic head movement, which consumes time.
\nSSD (Solid-State Drives) are not constructed like that. There is no magnetic head neither magnetic disks. Instead, data is stored in flash memory cells. Data access is quicker on that kind of disk. Note that SSD also costs more than traditional electromagnetic hard drives.
\n\nCPU is the initials of Central Processing Unit. The CPU is also denoted processor. The CPU contains :
\nThe arithmetic and logic unit (ALU)
The control unit (CU)
The CPU will be responsible for executing the instructions given by a program. For instance, the program can instruct to perform an addition between two numbers. Those numbers will be retrieved from the memory unit and passed to the ALU. The program might also require performing an I/O operation like reading data from the hard drive and loading it into the RAM for further processing. The CPU will execute those instructions.
\nThe CPU is the central component of a computer.
\n\nTo make computers do something, we have to feed them with precise instructions. This set of instructions is called “program”.
\nFollowing a more official definition, a program is “a combination of computer instructions and data definitions that enable computer hardware to compute”
Let’s take an example. Imagine a program that asks the user to type two numbers. The program adds those numbers, and the result is then displayed on the monitor. The instructions that have to be written are :
\nOutput “Please type your first number and press enter” on the monitor.
When a number is typed and the “Enter” key is pressed on the keyboard, store the number into memory. Let’s denote A this number.
Output “Please type your second number and press enter” on the monitor.
When a number is typed and the “Enter” key is pressed on the keyboard, store the number into memory. Let’s denote B this number.
Send to the ALU the two numbers (A and B) and the addition opcode and store the result into memory.
Output the result on the monitor
Two types of instructions are performed :
\nI/O operations : retrieve numbers stored into memory, store numbers into memory from an the input device (the keyboard), load data from memory, and display it to the user.
An arithmetic operation : add two numbers.
We have here a set of instructions that are written in plain English. The machine does not understand English sentences. Those sentences need to be translated to a language that the machine understands. What is this language?
\n\nInstructions that are given to the machine are written with programming languages. Programming languages are formal languages.They are composed of words that are constructed from an alphabet (a set of distinct characters). Those words are organized following specific rules. Go is a programming language, like x86 Assembly, Java, C, C++, Javascript...
\nThey are two types of programming languages :
\nLow level
High level
Low-level programming languages are closer to the processing unit’s instructions. Higher-level languages provide constructs that make them easier to learn and to use on a day-to-day basis.
\nSome high-level languages are compiled, others are interpreted, and some are in between. We will see in the next sections what those two terms mean.
\nTo speak to the processing unit of a computer, we can use a machine language. Machine languages are composed exclusively of zeros and ones. An instruction written in a machine language is a suite of 0 and 1. Each processor (or family of processors) will define a list of instructions called the instruction set. There is an instruction to add to the number, increment by one, decrement by one, copy data from one location in memory to another place...etc.
\nIt’s possible to write computer programs directly into machine language. However, this is not easy.
\n\nThe assembly language is a low-level programming language. The instructions of a program written in an assembly language correspond to machine instructions. Assembly languages use mnemonics which are small words that will correspond to a machine instruction. For instance MOV Will instruct the machine to move data from one location to another location. Developers can also comment the code (which is not possible with machine language).
\nTo create a program in assembly language, the developer will write instruction to one or several files. Those files are named source files.
\nHere is an example of an instruction written in x86 Linux assembly :
\n// assembly code\nmov eax,1\nint 0x80
\nThose two lines will perform a system call that will close the program (the “1” represent the system call number that means “exit the program”). Note that assembly language is different from machine to machine. We say that it’s machine-specific.
\nAn assembler is used to convert the source files written in an assembly language to object code files. We say that we assemble the program. The linker will then transform those object code files into an executable file. An executable file contains all the computer’s necessary instructions to launch the program.
\nThere are plenty of high-level languages on the market, like Go. Those languages are not closely bound to machine architecture. They offer a convenient way to write instructions. For instance, if we want to make a system call to exit our program, we can write in go :
\nos.Exit(1)
\nWith the C language, we can write :
\n// C Code\nexit(1)
\nWith Java, we can write :
\n// Java Code\nSystem.exit(1);
\nIn this example, we do not have to move a number into a register; we use the languages constructs (functions, packages, methods, variables, types ...). The objective of this book is to give you precise and concise definitions of those tools to build Go applications.
\nHigh-level programs are also written to files. Files are named “source files”. Generally, programming languages require adding a specific extension to the filename. For Go programs we will add “.go” At the end of each file that we will write. In PHP the extension is “.php”.
\nWhen source files are written, the program that they define cannot be executed immediately. The source file needs to be compiled by using a compiler. The compiler will transform source files into an executable. The compiler is also a program. Go is part of the compiled language family.
\nNote that some programming languages are interpreted. When the source files have been written, the programmer does not need to compile the sources. With the source files ready, the system can execute the program thanks to an interpreter. Each instruction written into the source file is translated and executed by the interpreter. In some cases, interpreters are storing in a cache a compiled version of the program to boost performance (the source files are not translated each time). PHP, Python, Ruby, Perl are interpreted languages.
\n\nWhere are programs stored?
Reading data from a hard drive is slower than reading data from RAM. True or false?
Can you write into the ROM? True or false?
What are the two types of memory?
What is the definition of “volatile memory”?
Which program transforms code written assembly language into object code?
Which program transforms object code into an executable file?
Give two advantages of high-level languages compared to low-level languages?
Go is an interpreted language? True or false?
Where are programs stored?
\nReading data from a hard drive is slower than reading data from RAM. True or false?
\nA computer user can write ROM? True or false?
\nWhat are the two types of memory?
\nThe central memory
The auxiliary memory
What is the definition of “volatile memory” ?
\nWhich program transforms code written assembly language into object code?
\nWhich program transforms object code into an executable file?
\nGive two advantages of high-level languages compared to low-level languages?
\nThey offer high-level constructs that are easier to use.
The code will not be specific to the technical architecture of a machine. We say that the code is portable.
Go is an interpreted language? True or false?
\nAt the macro-level, a computer is composed of :
\nA memory unit (MU): to store data and programs
An Arithmetic and Logical Unit (ALU): to perform computation
An Input and Output Unit (IOU): to manage input devices and output devices.
A Control Unit (CU) will manage the MU, ALU, and IOU following the instruction given by the program executed
CPU means Central Processing Unit (also called processor or microprocessor) is composed of the ALU and CU.
A program is a set of instructions.
Developers write programs with a programming language.
Programming languages are composed of words and characters that must be ordered following specified rules.
They are high and low-level programming languages.
Machine language and assembly language are low-level. The instructions written in those languages are closely linked to the hardware organization and capabilities. They provide little abstractions.
Go is a high-level programming language that is compiled.
Previous
\n\t\t\t\t\t\t\t\t\tForeword
\n\t\t\t\t\t\t\t\tNext
\n\t\t\t\t\t\t\t\t\tThe Go Language
\n\t\t\t\t\t\t\t\t