Instructions
Machine instructions are very simple. In general, it will take several of them to do what
you are used to doing in a single instruction in a high-level language like python or java.
- we will represent instructions in two different ways:
- machine code represented in binary or (for our convenience) in hexadecimal
(This is what a program would look like in memory if we could see it there)
- assembly language (This is the code that we type in to create a program)
- programs called assemblers are used to translate assembly language to machine code
- you will also get to do this by hand so you can see how the process works
For the present we will stick to the assembly-language representation of instructions
- each instruction consists of an operator and some address modes
- the operator represents the action that the instruction will perform
- the operator is always the first symbol in the textual representation of the instruction
- each address mode specifies how the CPU is to find an operand
- an operand is a value that the operator operates on
- each operator requires a particular type of address mode in each address-mode position
- here are some examples of instructions that manipulate values in registers:
- "add $1 $2 $3" adds the values in register 2 and register 3 and puts the answer into register 1
- "and $4 $5 $6" is similar, but it uses a logical "and" operation
- each has three address modes, all of the same type: register mode
- here are some examples that copy values between registers and memory:
- "lw $1 x" copies the value in memory location x into register 1
- "sw $2 y" copies the value in register 2 into memory location y
- each has two address modes that are of different types: register mode and direct mode
- our machine cannot operate on values that are in memory;
they must be copied to registers first
- a typical program unit will
- copy some values from memory to registers
- manipulate them to produce new values
- copy some of the new values back to memory
- the address modes represent algorithms that the computer will execute to find the operands
- some operators need three operands; e.g. add
- some operators need two operands; e.g. lw
- some operators need one operand; e.g. j
- some operators need no operands; e.g. eret
- here's some more information about address modes:
- register mode simply names the register that holds the operand
- direct mode gives the address of a memory location that holds the operand
- there are several other address modes that we will encounter as we go along
- some address modes are fakes:
- they specify algorithms that the hardware can't do in a single instruction
- the hardware can execute the algorithm in a short sequence of instructions (usually two)
- the developers thought that these address modes would be handy for programmers to have
- they wrote the assembler to accept such address modes and translate
them into the proper sequence of instructions
- direct mode is a fake in loads (e.g. lw) and stores (e.g. sw)
- there are also fake instructions:
- e.g. "move $1 $2" copies the value in register 2 into register 1
- e.g. "li $1 17" copies the value 17 into register 1
- notice that there are several instructions that do nothing but copy data from one place to another
- computers do a lot of that
In this course, fake is a technical term.