# `sh`

[MIPS Documentation](https://asm-editor.specy.app/documentation/mips.md) › [Instructions](https://asm-editor.specy.app/documentation/mips/instruction.md)

Web page: https://asm-editor.specy.app/documentation/mips/instruction/sh

**Store halfword**

Category: Load and store

Stores the lowest 16 bits of the first register at the address in the second plus an offset; the address must be halfword-aligned.

Used in: [syscall in the Playground](https://asm-editor.specy.app/learn/courses/mips/talking-to-the-outside-world/syscall.md) (MIPS assembly), [Chiptune](https://asm-editor.specy.app/learn/courses/mips/examples/chiptune.md) (MIPS assembly), [Sampled sound](https://asm-editor.specy.app/learn/courses/mips/examples/sampled-sound.md) (MIPS assembly)

## Forms

- `sh $t1, imm16($t2)`

## Pseudo-instructions

The processor does not have these forms. The assembler replaces each of them with one or more real instructions. Open a row to see them.

- `sh $t1, %lo(label)($t2)` · Write to label's address, reached through the %hi already in $t2
  - `sh $t1,%lo(label)($t2)` → `sh $t1, lo(label)($t2)`

Store the low-order 16 bits of $t1 into the effective memory halfword address

- `sh $t1, ($t2)`
  - `sh $t1,($t2)` → `sh $t1, 0($t2)`
- `sh $t1, imm`
  - `sh $t1,-100` → `sh $t1, imm($zero)`
  - `sh $t1,100` → `ori $at, $zero, imm`; `sh $t1, 0($at)`
  - `sh $t1,100000` → `lui $at, hi(imm)`; `sh $t1, lo(imm)($at)`
- `sh $t1, imm($t2)`
  - `sh $t1,100($t2)` → `ori $at, $zero, imm`; `addu $at, $at, $t2`; `sh $t1, 0($at)`
  - `sh $t1,100000($t2)` → `lui $at, hi(imm)`; `addu $at, $at, $t2`; `sh $t1, lo(imm)($at)`
- `sh $t1, label`
  - `sh $t1,label` → `lui $at, hi(label)`; `sh $t1, lo(label)($at)`
- `sh $t1, label($t2)`
  - `sh $t1,label($t2)` → `lui $at, hi(label)`; `addu $at, $at, $t2`; `sh $t1, lo(label)($at)`
- `sh $t1, label+imm32`
  - `sh $t1,label+100000` → `lui $at, hi(label+imm32)`; `sh $t1, lo(label+imm32)($at)`
- `sh $t1, label+imm32($t2)`
  - `sh $t1,label+100000($t2)` → `lui $at, hi(label+imm32)`; `addu $at, $at, $t2`; `sh $t1, lo(label+imm32)($at)`

## Operands

- `$t1`: Any general-purpose register, `$0` to `$31`, using its numeric or named spelling, such as `$8` or `$t0` (`$zero` is `$0`; `$ra` is `$31`).
- `imm16`: A signed 16-bit immediate, `-32768` to `32767`.
- `$t2`: A general-purpose register. It may be the same register as `$t1`.
- `label`: A named address in the program, such as the start of a loop or a variable in `.data`.
- `imm`: An immediate value: a signed 16-bit value (`-32768` to `32767`), an unsigned 16-bit value (`0` to `65535`) or a 32-bit value (`-2147483648` to `4294967295`). The size your value needs decides which real instructions the assembler uses. Open the row to see them for each size.
- `imm32`: A 32-bit immediate, `-2147483648` to `4294967295`. A pseudo-instruction that takes one uses the register `$at` as a temporary, so `$at` loses the value it held.

## Description

The upper 16 bits of $t1 are ignored. The address must be halfword-aligned (divisible by 2).

## Example

```mips
.data
.align 2
data: .half 0
.text
# Write initialized value 0x12345678 with sh; inspect data bytes after the instruction: [0x78, 0x56].
la $t1, data
li $t0, 0x12345678
sh $t0, 0($t1)
```
