What does $ mean in MIPS assembly?

Learn what does $ mean in mips assembly? with practical examples, diagrams, and best practices. Covers assembly, mips development techniques with visual explanations.

Understanding the '$' Symbol in MIPS Assembly Language

Hero image for What does $ mean in MIPS assembly?

Demystify the '$' symbol in MIPS assembly, learning its role in identifying registers and how it's used in common instructions.

In MIPS (Microprocessor without Interlocked Pipeline Stages) assembly language, the $ symbol plays a crucial and fundamental role. Unlike many other programming languages where $ might denote a string variable or a special operator, in MIPS, it is exclusively used to identify and reference registers. Understanding this convention is key to reading, writing, and debugging MIPS assembly code.

The Role of '$' in MIPS Registers

MIPS architecture is a load/store architecture, meaning that arithmetic and logical operations are performed only on data held in registers. Data must be loaded from memory into registers before it can be processed, and then stored back to memory from registers. The $ prefix is a mandatory part of a register's name, distinguishing it from immediate values, memory addresses, or labels.

MIPS processors typically have 32 general-purpose registers, each 32 bits wide. These registers are named using a numerical convention (e.g., $0, $1, ..., $31) or a more descriptive symbolic convention (e.g., $zero, $v0, $a0, $t0, $s0, $sp, $ra). The $ symbol always precedes both numerical and symbolic register names.

flowchart TD
    A["MIPS Instruction"]
    B["Operand 1"]
    C["Operand 2"]
    D["Result Destination"]

    A --> B
    A --> C
    A --> D

    subgraph Register Usage
        B -- "Must be a register" --> B_Reg["$t0", "$s1", etc.]
        C -- "Can be register or immediate" --> C_Reg["$t1", "$s2", etc.]
        C -- "Can be register or immediate" --> C_Imm["10", "0xbeef"]
        D -- "Must be a register" --> D_Reg["$t2", "$s3", etc.]
    end

    B_Reg --> "Uses `$` prefix" --> E["Register Identifier"]
    C_Reg --> "Uses `$` prefix" --> E
    D_Reg --> "Uses `$` prefix" --> E

Flowchart illustrating the role of the $ prefix in MIPS instruction operands.

Numerical vs. Symbolic Register Names

MIPS provides two ways to refer to its general-purpose registers: by number or by symbolic name. Both require the $ prefix.

Numerical Names: These are straightforward, ranging from $0 to $31. While precise, they don't convey the intended use of the register, making code harder to read.

Symbolic Names: These are aliases for specific numerical registers, assigned based on common usage conventions. They greatly improve code readability and maintainability. For example, $t0 (temporary register 0) is typically $8, and $s0 (saved register 0) is typically $16.

# Using numerical register names
addi $8, $9, 10      # Add 10 to register $9, store in $8

# Using symbolic register names (preferred)
addi $t0, $t1, 10    # Add 10 to register $t1, store in $t0

# Example with a system call argument
li $v0, 1            # Load immediate value 1 into $v0 (syscall code for print_int)
li $a0, 42           # Load immediate value 42 into $a0 (argument for print_int)
syscall              # Execute syscall

Examples of MIPS instructions using both numerical and symbolic register names.

Common MIPS Registers and Their Symbolic Names

Here's a brief overview of some commonly used MIPS registers and their symbolic names:

  • $zero ($0): Always holds the value 0. Cannot be written to.
  • $at ($1): Assembler temporary. Reserved for the assembler.
  • $v0-$v1 ($2-$3): Return values from functions.
  • $a0-$a3 ($4-$7): Arguments for functions.
  • $t0-$t7 ($8-$15): Temporary registers, not preserved across function calls.
  • $s0-$s7 ($16-$23): Saved registers, must be preserved by callee across function calls.
  • $t8-$t9 ($24-$25): More temporary registers.
  • $k0-$k1 ($26-$27): Reserved for OS kernel.
  • $gp ($28): Global pointer.
  • $sp ($29): Stack pointer.
  • $fp ($30): Frame pointer.
  • $ra ($31): Return address for function calls.