MIPS directives

What directives are

MIPS directives are used to define the structure of the program. They are not instructions that are executed by the CPU, but rather instructions that are used by the assembler to define the structure of the program.

.data

Declares a section for storing initialized data, such as variables.

.text

Declares a section for storing executable instructions. This is where program logic is written.

.kdata

Declares a section for storing kernel-mode initialized data.

.ktext

Declares a section for storing kernel-mode executable instructions.

.word

Allocates one or more 32-bit (4-byte) words in memory.

Example:

values: .word 1, 2, 3, 4

.half

Allocates one or more 16-bit (2-byte) halfwords in memory.

Example:

values: .half 1234, 5678

.byte

Allocates one or more 8-bit (1-byte) values in memory.

Example:

flags: .byte 0, 1, 1, 0

.float

Allocates a single-precision floating-point (32-bit) number in memory.

.double

Allocates a double-precision floating-point (64-bit) number in memory.

.asciiz

Stores a null-terminated string (C-style string).

Example:

message: .asciiz "Hello, world!"

.ascii

Stores a string without a null terminator. Useful when manually handling string length.

.space

Reserves a specified number of bytes in memory without initializing them.

Example:

buffer: .space 100   # Reserves 100 bytes

.align

Aligns the next data address to a boundary of 2^n bytes: .align 2 moves it to a 4-byte boundary, and .align 3 to an 8-byte boundary. The operand is an exponent, not a byte count.

Example:

.align 2   # Aligns to a 4-byte boundary

.globl

Marks a symbol as global so other source files or a linker can refer to it. For example, .globl main gives the symbol main external visibility.

Example:

.globl main

.extern

Declares an external data symbol and its size in bytes, so the assembler can reserve its address for references to that symbol.

.macro

Defines a macro, which allows writing reusable code blocks.

.end_macro

Ends a macro definition.

.include

Includes an external assembly file.

Example:

.include "myfile.s"

.eqv

Defines a symbolic constant, similar to #define in C.

Example:

.eqv SIZE 10

.set

Configures assembler settings, such as allowing modifications to the $at register.

Example:

.set noat  # Allows use of register $at

.bss

Declares an uninitialized data section, typically used for reserving large blocks of memory.

.frame

Describes a function's stack frame for debugging tools: base register, stack size and return register. The assembler accepts this metadata directive but does not use it to change program behavior.

.ent

Marks the start of a function for debugging tools. The assembler accepts this metadata directive but does not use it to change program behavior.

.end

Marks the end of a function for debugging tools. The assembler accepts this metadata directive but does not use it to change program behavior.

.local

Marks a symbol as local to this file for the linker. This assembler accepts the directive but does not use it to change symbol visibility.

.section

Switches to the named section. In this simulator, .rodata and .bss share the data segment with .data; use .text for instructions.

Example:

.section .rodata
message: .asciiz "Ready"

.rdata

Declares a section for read-only initialized data, such as string literals. Read-only data is not stored separately in this simulator, so it joins the data segment.

.sdata

Alias for .rdata.

.sbss

Alias for .bss.

.comm

Reserves bytes for an uninitialized global variable, the way a C compiler declares one. Takes a symbol, a size in bytes and an optional alignment, and leaves the current section unchanged.

Example:

.comm total, 4, 4

.lcomm

Like .comm, but for a symbol local to this file, as a C compiler declares an uninitialized static variable.

.zero

Reserves the given number of bytes, which read as zero. Alias for .space.

.p2align

Alias for .align: aligns the next item on a 2^n byte boundary.

.balign

Aligns the next item on the given byte boundary, written directly rather than as a power of two.

Example:

.balign 8

.2byte

Alias for .half.

.4byte

Alias for .word.

.asciz

Alias for .asciiz: stores a null-terminated string.

.string

Alias for .asciiz: stores a null-terminated string.

.global

Alias for .globl.