Skip to content

1. Programs, Instructions, Registers

An instruction is a single, simple operation for the computer to carry out.

For example:

  • "add two numbers together"
  • "copy a number from one location to another"
  • "go to a different place in the program"
  • "search a string for a character"

A program is a series of these tiny instructions. Machine language instructions are the patterns of bits that a processor reads to know what to do. Assembly language (or "asm") is a human-readable, textual representation of machine language.

Alt text

So what can a CPU do? Well, maths, load/store things from/to memory, go execute somewhere else.

Alt text

An Instruction Set Architecture (ISA) is the interface or architecture that a CPU presents to the programmer. They define WHAT the CPU can do, WHAT registers it has, and WHAT the machine language is (machine language is the bit patterns used to encode instructions). They do not define HOW the CPU does it, or HOW to design the hardware.

Kinds of ISAs

CISC

Complex Instruction Set Computer (CISC) is an ISA designed for humans to write asm. There are lots of instructions and ways to use them. They are complex instruction to shorten and simplify programs.

RISC

Reduced Instruction Set Computer (RISC) is an ISA designed to make it easy to build the CPU hardware, make that hardware run fast, write compilers that make machine code. It has a small number of instruction, and they are very simple. MIPS is RISCy which we will be using.

MIPS

Registers

Registers are a small and fast temporary memory inside the CPU. The CPU can only operate on data in registers. MIPS has $32$ registers, and each is $32$ bits (one word). Each register is outlined below.

Alt text

Alt text

Think of registers like hands. You have a limited number and they can only hold small things. Your program's variables primarily live in memory and the registers are just a temporary stopping point for those values.

Alt text

You can't write every program using only registers. Every piece of your program has to SHARE the registers.

Saved and Temporary Registers

The registers t0 through t9 are used for temporary values.

The registers s0 through s7 are "saved" variables or local variables inside a function (kind of).

However, rule of thumb:

  • Use t register
  • Unless you need the value to presist when calling functions (like loops)

Most of the time you will use s and t registers

ISA, What Can It Do?

Command Action
li Loads a number (Immediate) (how you get numbers into the registers)
add It adds 2 numbers
sub It subtracts 2 numbers
mul It multiplies 2 numbers
div It divides 2 numbers
move It COPIES a number (copies from one register to another)

So for example normal language to machine language,

s0 = 3;
s1 = 5;
s2 = s0 + s1;
li  s0, 3
li  s1, 5
add s2, s0, s1

Ok so, li stands for "load immediate" which "immediate" means "number inside the instruction." add, well adds. And like in Java and C, the destination is on the left. Mars, our code editor, has a list of possible instructions.

Programming

Think in terms of a high level programming language then translate it to assembly with comments (comments use a #).

s4 = (s0 + s1 - s2) * s3 in assembly

add t0, s0, s1
sub t0, t0, s2
mul s4, t0, s3

Use The Smallest Amount of Registers as Possible

The Other Way

mul t0, s2, 33 # t0 = s2 * 33
div t1, s3, s4 # t1 = s3 / s4
sub s1, t0, t1 # s1 = t0 - t1

or all together s1 = (s2 * 33) – (s3 / s4)

Some More Notes

C to Assembly is the same as Compile.
Assembly to Bits is the same as Assemble.

Also, registers are convections and we can use them for what ever we want (although not recommended). The exception is the 0 register.

Next Page →