MIPS Assembly/MIPS Details

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search


Contents

[edit] RISC vs CISC

RISC
RISC stands for Reduced Instruction Set Computer. MIPS is a RISC computer.
CISC
CISC stands for Complex Instruction Set Computer. An example of a CISC architecture is the Intel x86 Instruction Set.

[edit] Instruction Formats

There are 3 different types of instructions: R Instructions, I Instructions, and J Instructions. We will discuss all 3 varieties below.

[edit] R Instructions

R Instructions take three arguments: two source registers (rt and rs), and a destination register (rd). R instructions are written using the following format:

instruction rd, rs, rt

For example, if we want to add the contents of $3 and $4 together, and store the result in $5, we would have the following instruction:

add $5, $3, $4

An R-format instruction is encoded as

opcode rs rt rd shamt func

where opcode is a 6-bit field and gives the instruction its name, or its "operation code", the three register fields are each 5-bit wide (2**5=32), shamt is a 5-bit field used in shift instructions to specify the number of places to shift the destination register contents (again, one can specify any displacement from one to 32 places since 2**5=32), and func is a 6-bit field that specifies the operation for instructions with an opcode=0 ("simple" arithmetic and logic instructions).

[edit] I Instructions

I instructions take two register arguments and a 16-bit immediate value. The destination register (rd) is written first, followed by the source register (rs), and finally the immediate value:

instruction rd, rs, imm

For example, let's say that we want to add the value 5 to the register $3, and store the result in $4:

addi $4, $3, 5

An I-format instruction is encoded as

opcode rs rt immed

where opcode is a 6-bit field and gives the instruction its name, or its "operation code", the two register fields are each 5-bit wide (2**5=32), and immed is a 16-bit field that holds an immediate constant. Depending on the instruction, the immed field is interpreted as a twos-complement number (arithmetic instructions, loads/stores, branches), or as a logical constant (logic instructions).

[edit] J Instructions

J instructions are used to transfer program flow to a given, hardcoded offset from the current value of the IP register. J instructions are often used with labels, although the assembler will convert the label into a numerical offset value. A J instruction takes only one argument: the numerical offset to jump to.

instruction dest

for example, let's say that we have a label Label1:, and we want to jump to it:

j Label1

A J-format instruction is encoded as

opcode addr

where opcode is a 6-bit field and gives the instruction its name, and addr is a 26-bit field that specifies an address.

[edit] Registers

The MIPS registers are arranged into a structure called a Register File. The register file contains 32 registers. Also, there are a few other architectural registers that are not located in the register file. A common example is the Program Counter register (PC), that points to the current location of the program execution in memory.

Registers can be referred to by either names or numbers. This means that we can refer to a given register as $a0, or $4. In general, there are many registers that can be used in your programs: the ten temporary registers and the eight saved registers. Temporary registers are general-purpose registers that can be used for arithmetic and other instructions freely, while saved registers must be saved at procedure entry, and restored at procedure exit.

Temporary register names all start with a $t. For instance, there are $t0, $t1 ... $t9. this means there are 10 temporary registers that can be used without worrying about saving and restoring their contents. The saved registers are named $s0 to $s7.

There is a software convention regarding the use of several registers. For instance, $29 is reserved for use as a stack pointer ($sp), and $31 is used for linking the return address of procedure ($ra). $31 is always filled with the address of the instruction that follows a jump-and-link (jal) instruction.

The zero register, is named $zero ($0), and is a static register: it always contains the value zero. This register may not be used as the target of a store operation, because its value is hardwired in, and cannot be changed by the program.