MIPS Reference


Arithmetic

<< Assembler Directives | Table of Contents | Memory Access >>
All computers must be able to perform arithmetic. The MIPS architecture is no different.

Format

All arithmetic instructions in MIPS have three operands and all operands must be registers.

::op dest, opnd1, opnd2

The indicated operation is performed on the two operands and the result is stored in the destination. For example,

   add $s0, $s1, $s2             # A = B + C

Design Principles. The design of the arithmetic instructions is derived from two basic principles: simplicity favors regularity and smaller is faster.

Example. This simplicity can complicate some things, however. Consider the following statement.

      A = B + C + D;

This one high-level language statement will require several MIPS instructions.

      add $t0, $s1, $s2    # t0 = B + C
      add $s0, $t0, $s3    # A = t0 + D

Arithmetic Integer Operations

The following table lists the most common arithmetic integer operations in MIPS. All of these operations work on registers with rdest being the destination register for the operation and rs and rt being the operand registers.

abs rd, rs
stores the absolute value of register rs in register rd.
neg rd, rs
(pseudo) stores the negative of rs in rd.
add rd, rs, rt
computes rd = rs + rt
addu rd, rs, rt
the same as add but both rs and rt are treated as unsigned integers.
sub rd, rs, rt
computes rd = rs - rt
subu rd, rs, rt
the same as sub but both rs and rt are treated as unsigned integers.
mul rd, rs, rt
(pseudo) computes rd = rs * rt
mulu rd, rs, rt
the same as mul but both rs and rt are treated as unsigned integers.
div rd, rs, rt
(pseudo) computes rd = rs / rt (integer division)
divu rd, rs, rt
the same as div but both rs and rt are treated as unsigned integers.
rem rd, rs, rt
computes rd = rs % rt (integer remainder)
remu rd, rs, rt
the same as rem but both rs and rt are treated as unsigned integers.

MIPS also provides add instructions when working with immediate or literal values.

addi rd, rs, imm
computes rdest = rs + imm.
addiu rd, rs, imm
same as addi but for unsigned integers.


<< Assembler Directives | Table of Contents | Memory Access >>


MIPS Processor and Assembly Language Reference

Print - Changes - Search
Last modified: September 06, 2009, at 05:34 PM.