MIPS Assembly/Arithmetic Instructions
From Wikibooks, the open-content textbooks collection
This page is going to talk about the arithmetic instructions of the MIPS architecture.
Contents |
[edit] Register Arithmetic Instructions
|
|
add |
|
R Type |
This instruction adds the two operands together, and stores the result in the destination register. Negative numbers are handled automatically using Two's Complement Notation. This means that different instructions do not need to be used for signed and unsigned numbers.
|
|
sub |
|
R Type |
The sub instruction subtracts the first operand from the second operand, and stores the result in the destination. In pseudo-code, the operation performs the following:
rd = rs - rt
[edit] Multiplication and Division
The multiply and divide operations are slightly different from other operations. Even if they are R-type operations, they only take 2 operands. The result is stored in a special 64-bit result register. We will talk about the result register after this section.
|
|
mult |
|
R Type |
This operation multiplies the two operands together, and stores the result in rd. Multiplication operations must differentiate between signed and unsigned quantities, because the simplicity of Two's Complement Notation does not carry over to multiplication. The mul instruction multiplies and sign extends signed numbers.
The result of multiplying 2 32-bit numbers is a 64-bit result. We will discuss the 64-bit results below.
|
|
multu |
|
R Type |
The multu instruction multiplies the two operands together, and stores the result in rd. This instruction is for unsigned numbers only, and does not sign extend a negative result. This operation also creates a 64-bit result.
|
|
div |
|
R Type |
The div instruction divides the first argument by the second argument. The quotient is stored in the lowest 32-bits of the result register. The remainder is stored in the highest 32-bits of the result register. Like multiplication, division requires a differentiation between signed and unsigned numbers. This operation uses signed numbers.
|
|
divu |
|
R Type |
Like the div instruction, this operation divides the first operand by the second operand. The quotient is stored in the lowest 32-bits of the result, and the remainder is stored in the highest 32-bits of the result. This operand divides unsigned numbers, and will not sign-extend the result.
[edit] 64-Bit Results
The 64-bit result register is broken into two 32-bit segments: HI and LO. We can interface with these registers using the mfhi and mflo operations, respectively.
|
|
mfhi |
|
R Type |
Takes only 1 operand. This instruction moves the high-32 bits of the result register into the target register.
|
|
mflo |
|
R Type |
Also takes only 1 operand. Moves the value from the LO part of the result register into the specified register.
[edit] Register Logic Instructions
These operations perform bit-wise logical operations on their operands.
|
|
and |
|
R Type |
Performs a bitwise AND operation on the two operands, and stores the result in rd.
|
|
or |
|
R Type |
Performs a bitwise OR operation on the two operands, and stores the result in rd.
|
|
nor |
|
R Type |
Performs a bitwise NOR operation on the two operands, and stores the result in rd.
|
|
xor |
|
R Type |
Performs a bitwise XOR operation on the two operands, and stores the result in rd.
[edit] Immediate Arithmetic Instructions
|
|
addi |
|
I Type |
Adds an immediate 16-bit value to the destination register:
addi $1, $2, 255
|
|
subi |
|
I Type |
Subtracts an immediate 16-bit value from the register
[edit] Immediate Logic Instructions
|
|
andi |
|
I Type |
Ands the register with a 16 bit immediate value.
|
|
ori |
|
I Type |
Ors the register with a 16 bit immediate value.
|
|
xori |
|
I Type |
Xors the register with a 16 bit immediate value.
[edit] Shift instructions
|
|
sll |
|
R Type |
Logical shift left.
|
|
srl |
|
R Type |
Logical shift right.