This is based on:
D.A. Patterson and J.L. Hennessy (1994) Computer Organisation and Design: The Hardware Software Interface, Morgan Kauffmann Publishers Inc., San Francisco.
The instruction set is based on the MIPS architecture. This is a 'modern' instruction set architecture, based on RISC (reduced instruction set computer) principles. It is used, for example, on DEC alpha machines, and Silicon Graphics workstations.
The processor has 32 registers labelled $0, $1, ..., $32. Some are reserved and cannot be changed by the programmer ($0 is always 0). The word length is 32 bits. Main memory has 2^30 (2 to the power of 30) words.
a = b + c
A first approximation to this in the MIPS instruction set is:
In fact although in a high level programming language such as C++ we can use arbitrary variable names representing places in main memory, in the MIPS instruction set, all arithmetic operations operate only on registers.
Assumi , b and c as $16 and $17 respectively, then
All such instructions have exactly 3 arguments: the location to place the result (here it was $8) and the operands ($16 and $17). Here are some more examples:
x = (a+b) - (c+d) //C++ code
Data structures such as arrays or classes are in main memory. Therefore to carry out operations on their elements, they must be shifted to the register bank, and then written back to main memory. These data transfer instructions must supply a memory address and a register.
Consider the following C++ operations:
g = h + A[i]
This becomes:
In the MIPS architecture, a memory address refers to a byte. A byte is 8 bits, and so 4 bytes = 1 word. Therefore a memory address x actually means the xth byte from the start of memory (ie, 0). Hence if we want the ith word this is byte address 4*i.
In the example above it is assumed that $19 actually contained 4*i.
A[i] = h + A[i] //C++ code to overwrite an array location
add a,b,c
#a t.
add $8,$16,$17
#a = b+c
Simplicity of Design
add $8,$16,$17
#t1 = a+b
add $9,$18,$19
#t2 = c+d
sub $20,$8,$9
#a = t1 - t2
Load: Reading from Main Memory
lw $8,Astart($19)
#load word: $8 gets A[i] where Astart is start of array and $19=i
add $17,$18,$8
#g = h + A[i] assuming that h is in $18
Memory Addressing
Store: Writing into Main Memory
lw $8,Astart($19) #assumes $19=4*i, places A[i] into $8
add $8,$18,$8
#overwrites $8 with $18(=h) + A[i]
sw $8,Astart($19)
#store word: A[i] = $8
Machine Instructions
Instructions are always 32 bits in length, and for R-type instructions
(arithmetic/logical) they are broken up into 6 fields, whereas for
I-type (data transfer) into 4 fields. The
following table gives these for add and sub.
Comment op rs rt rd shamt
funct
Field size
6 bits
5 bits
5 bits