Skip to Content

Basic Computer Operation

Basic Computer Operation

From the notes on numbers we learned that all values in a computer are in binary: just lots and lots of 1's and 0's.

This means that the data looks like "01100010101001000011110010111" And the machine instructions look like "0110001110101010111100" as well!

Recall that the memory holds both instructions and data. So, the memory holds a bunch of bits (actually lots and lots of bits!)

How do we get at the values in memory? The answer is we use addresses.

An address is a number that signifies a specific memory location, just like your apartment number signifies a specific apartment. Generally, each memory location holds one byte (8 bits) of data or instruction. On the AVR the data memory is byte-addressed while the program (instruction) memory is word-addressed (1 address per 16 bits, or two bytes).

Accessing memory is just like indexing an array in a program:

  • An address (i.e., index) is presented to the memory
  • The memory looks up that address and returns the data that is stored at that location

General Concept of Computer Operation

The CPU has an address of an instruction to be executed

  • Let's call the address the Program Counter, or PC

It fetches the instruction from memory

It decodes, or figures out, what the instruction is

It executes the instruction

A side effect of the instruction will be to increment, or change, the PC

Then we are back at step 1, because the PC has a new address (the address of the next instruction to execute)

So for each instruction the computer executes, it must get that instruction from the memory. But instructions typically use data: for example, an add instruction uses two pieces of data (the two numbers to add) and produces one piece of data (the sum of the two numbers).

Where does the data come from (or go to)? Memory, of course. But accessing memory four times to perform an add (read the instruction, read two pieces of data, and write the result back) is extremely slow, in computer speed terms.

So, all CPU's have some very special memory on chip, called registers. These registers are fast, and these are typically what instructions like add use. For example, the AVR has 32 1-byte registers, named r0 to r31. Think of registers as predefined variables that you have to use in order to perform certain operations.

But now, we need special instructions to get values from memory to the registers, and back, so that the values are in the registers when instructions like add need them.

These special instructions are load and store instructions: load a register from memory, store it back.

Suppose we have the C or Java program statement "z = x+y;". What instructions does the CPU have to do to complete this single statement?

  • load the memory value of x into a register
  • load the memory value of y into a register
  • add the two registers together, with the result stored back into some register
  • store the result register back into memory at the variable z

So, four machine instructions are needed to implement one simple C/Java statement.


Programming CPUs

Now we know about instructions, addresses, and registers. Let's get to specifics of programming.

The machine instructions look like "01101010" -- they are just a sequence of bits

  • E.g., the two bytes "00001111-00000001" mean "add registers r16 and r17 and put the result back in r16" on the AVR CPU.

But we don't want to program using 1s and 0s, do we?

  • Actually, that is how computers were first programmed. The programmer entered the machine instruction using switches that were set to 1 or 0.
  • This was awfully tedious!

Instead we use words and abbreviations for each instruction, and then use a program to assemble the words into the actual machine instructions.

  • The AVR calls the instruction 000011XX-XXXXXXXX "add", short for "Add Registers" (the X bits are filled in with the register numbers).

This "language" of words and abbreviations for the machine instructions is called assembly language, and the assembler is the program which reads a program written in assembly language, and produces the machine code.

This process is similar to compiling a program in a high level programming language, but we call it assembling to distinguish it because:

  • Each assembly instruction is exactly one machine instruction
  • The assembly instructions are specific to a type of microprocessor (CPU)

An assembly language also has other nice features:

  • It lets us use decimal, octal, and hex numbers (in addition to binary)
  • It lets us assign names, or symbols, to certain values to make our program more readable
  • It lets us put comments in our code (This is important, really!)