ECE 374: Computer Organization
More MIPS ASM InstructionsRegister/Memory Operands
Example
C Code:
f = (g + h) - (i + j);
Assignments:
$f, ..., j -> \$s0, ..., \$s4$
MIPS ASM:
add $t0, $s1, $s2
add $t1, $s3, $s4
sub $s0, $t0, $t1
Loading/Storing Instructions
Load - Memory â Registers - reg Rd, c(Rs)
Store - Registers â Memory - memory Rs, c(Rd)
Example
C Code:
A[12] = h + B[8];
MIPS ASM:
// $s0 = h
// $s1 = base address of B
// $s2 = base address of A
lw $t0, 32($s1) // Load B[8] (32 is array element size (4) * index (8))
add $t1, $t0, $s0 // Perform the addition
sw $t1, 48($s2) // Store result at A[12] (48 is array element size (4) * index (12))
More Complex Example
C Code:
B[7] = A[i-j];
MIPS ASM:
// $s0 = i
// $s1 = j
// $s2 = base address of A
// $s3 = base address of B
sub $t0, $s0, $s1 // Find index for A
sll $t1, $t0, 2 // We have to multiply index by the size of each element (4)
add $t2, $t2, $s2 // Add addresses together to get address of element
lw $t3, 0($t2) // Load A[i-j] into temporary register
sw $t3, 28($s3) // Store loaded value at B[7] (4 * 7 = 28)
Immediate Operations
Some instructions can take in constants to apply them on values.
They will typically follow the format: <operation>i <destination>, <source>, <immediate>
Example 1
C Code:
y = x + 7;
MIPS ASM:
// $s0 = y
// $s1 = x
addi $s0, $s1, 7
Example 2
C Code:
y = x - 7;
MIPS ASM:
// $s0 = y
// $s1 = x
addi $s0, $s1, -7 // There is no subi so we can just use negative numbers
Sign Extension
There are no explicit sign extension instructions in the ISA. However, other commands can be used to extend values.
addi
â Sign extend immediate value
lb
, lh
â Sign extend loaded byte/half-word
beq
, bne
â Sign extend displacement
Representing Instructions
All MIPS instructions are encoded as 32-bit instructions.
Register numbers:
$t0-$t7
are registers 8-15
$t8-$t9
are registers 24-25
$s0-$s7
are registers 16-23