# 3.3: Subtraction in MIPS Assembly

- Page ID
- 27108

Subtraction in MIPS assembly is similar to addition with one exception. The `sub`

, `subu`

and `subui`

behave like the `add`

, `addu`

, and `addui`

operators. The only major difference with subtraction is that the `subi`

is not a real instruction. It is implemented as a pseudo instruction, with the value to subtract loaded into the `$at`

register, and then the R instruction `sub`

operator is used. This is the only difference between addition and subtraction.

`sub`

operator, which takes the value of the`R`

and_{s}`R`

registers containing integer numbers, adds the numbers, and stores the value back to the_{t}`R`

register. The format and meaning are:_{d}format:

`sub R`

_{d}, R_{s}, R_{t}meaning:

`R`

_{d}<- R_{s}- R_{t}`sub`

pseudo operator, which takes the value of R_{s}, subtracts the 16 bit immediate value in the instruction, and stores the result back in R_{t}. The format, meaning, and translation are:format:

`subi R`

_{t}, R_{s}, Immediatemeaning:

`R`

_{t}<- R_{s}- Immediatetranslation:

`addi $at, $zero, Immediate`

`sub R`

_{t}, R_{s}, $at`subi`

pseudo operator, which takes the value of R_{s}, subtracts the 16 bit immediate value in the instruction, and stores the result back in R_{t}. The format, meaning, and translation are:format:

`subi R`

_{t}, R_{s}, Immediatemeaning:

`R`

_{t}<- R_{s}- Immediatetranslation:

`addi $at, $zero, Immediate`

`sub R`

_{t}, R_{s}, $at`subu`

operator, which is the same as the`add`

operator, except that the values in the registers are assumed to be unsigned, or whole, binary numbers. There are no negative values, so the values run from 0..2^{32}-1. The format and the meaning are the same as the`add`

operator above:format:

`subu R`

_{d}, R_{s}, R_{t}meaning:

`R`

_{d}<- R_{s}+ R_{t}`subiu pseudo`

operator, which is the same as the`addi`

operator, but again the numbers are assumed to be unsigned:format:

`subiu R`

_{t}, R_{s}, Immediatemeaning:

`R`

_{t}<- R_{s}+ Immediatetranslation:

`addi $at, $zero, Immediate`

`subu R`

_{t}, R_{s}, $at

In addition to the real operators, there are a number of pseudo sub operators, which use 32-bit immediate values. The 32-bit values are handled exactly as with the add instructions, with a sign extension out to 32 bits.