Lecture Overview
Computer Architecture
The following is an idealized picture of how a computer works. The reality is much more complicated, but the following model provides a good foundation to understanding the computer.
- The main components of a computer are:
- CPU or Central Processing Unit - the circuits that actually carry out computation.
- Memory, which is a place to store data and instructions. Memory is fast but the contents are lost when the computer is powered down.
- Storage, a place to store data and instructions which is much slower than memory but is permanent.
- Input/Output devices.
- System Bus - the circuits that connect all the pieces of the computer together.
- The computer operates on the fetch-decode-execute cycle.
- Programs are stored as a long list of instructions in memory. Memory is divided into cells, each cell can store a single piece of data. In this case, each cell in memory stores an instruction.
- During operation, the CPU maintains a program counter, which is the address in memory it is currently working on. The instruction at the program counter is fetched from memory into the circuits inside the CPU.
- The CPU then decodes the instruction to determine what the instruction is.
- The CPU then executes the instruction.
- The CPU then moves the program counter to the next address and repeats
This is enough for now. Later we will fill in more details in this picture like how the data/instructions are actually stored, what kind of instructions are available, where the operating system fits, and so forth.
Python
Our task as programmers is to set up a list of instructions so that when the CPU goes through our instructions with its fetch-decode-execute cycle, the CPU carries out the task we have in mind. The CPU can only execute what are called machine instructions. Directly writing machine instructions is time consuming and difficult. Instead we write instructions in what is called a high-level programming language of which Python is just one example. Python then translates the instructions we have written into the format expected by the CPU. Despite this, we are still thinking of our program as a list of instructions we set up before hand so that when the CPU goes through its fetch-decode-execute cycle the task we have in mind is accomplished.
The tool which carries out this translation is called the Python Interpreter. The python interpreter has two modes: it can read instructions one by one as you type them in which is called interactive mode or it can read them from a file. We will be using both methods of entering instructions this semester.
Data Storage in the computer
Binary numbers
Hexadecimal
- The memory in the computer consists of a large collection of cells. Each cell stores 8 bits which we call a byte.
- Bytes, Kilobytes (KB), Kibibytes (KiB), megabyte (MB), mebibyte (MiB), see byte and binary prefix.
- If a data value is larger than 8 bits, more than one cell must be used for storage. Thankfully, python takes care of this for us. For example, for integers a single cell can store only numbers zero through 28 - 1 = 255. To store larger numbers requires multiple cells. If we enter a command like
x = 16323632125663321
to python, python will see that the number requires more than one cell and will store it in however many cells are required.
- CPUs from the 70s and earlier could only work on one cell in memory (8 bits = 1 byte) at a time so large numbers which use multiple cells took longer to process. Modern processors like AMD, Intel, and ARM can work on either 32-bits = 4 bytes at once or 64-bits = 8 bytes at once. Thankfully, as a high level language Python takes care of everything for us. During the instruction translation, Python will pick the best CPU instructions depending on how many cells a given piece of data takes and what instructions the CPU supports.
Exercises
For Problems 1 through 3, there is no python programming. Instead, you should type the problems and your answers into a text file and submit that.
- Problem 1: Convert each of the following binary numbers to decimal and hexidecimal by hand (maybe using a calculator for the multiplication and addition). Show your work.
- 0101
- 1001
- 10000
- 101101100
- Problem 2: Convert each of the following decimal numbers into binary by hand (maybe using a calculator for the division). Show your work.
- Problem 3: Convert each of the following hexidecimal numbers into decimal and binary by hand (maybe using a calculator for the multiplication and addition). Show your work.
- Problem 4: Search Google (or your favorite search engine) for a python program or python statement which converts a binary number to decimal. Try it out on the numbers in Problem 1.