MIPS Assembly/MIPS Instructions
Writing assembly codeEdit
An assembly language program has a few common features. These are:
In MIPS assembly, comments are denoted with a number sign ("#"). Everything after that sign on the same line is a comment and is skipped by the assembler's lexer.
A label is something to make your life simple. When you reference a piece of your program, instead of having to count lines, you can just give it a name. You use this in loops, jumps, and variable names.
Labels don't appear in your final code, they're only there for convenience, one of the few perks you'll get from the typical MIPS assembler. It also makes life easy for the assembler, because it can now easily go around relocating and linking code. Don't worry if you don't know what those are, that'll come later.
Labels in MIPS assembly, actually in most variants of ASM as well, are created by writing
where name is a name you use to refer to this label. Note that, you can't create a label with the same name as a MIPS instruction.
Labels are the same as their C cousins, the targets of goto operators.
A section is a way of dividing an assembly language program up into the instructions and the data logically. Since your assembly program is loaded into memory, all the processor needs to know is the start of execution (known as the entry point) and merely just increments the instruction pointer and continues along. If data is interspersed in the instruction code in memory, the processor needs to know what is data and what is instructions - which serves for a much more complicated processor. For simplicity, the data is set off in a different area than the instructions. Sections are used for this purpose.
There are two sections you need to be mindful of in MIPS programming: the text and data sections. The text section holds your assembly program, and the data section holds your data. One designates a text section by starting with .text and the start of a data section by .data.
The processor assembler (or your emulator) understands a few special commands that perform specific tasks to make your life easier: for example creating memory space for you to store data in variables.
If you wish to create a variable for a number in your assembly program, you may want to:
- switch to the data section
- create a label to create a memory reference for your variable
- give it an appropriate name
- allocate some memory for it
- align it properly in memory.
- switch back to the text section
From what we have learned, we can write the MIPS assembly code up to step 3:
But now how can we allocate some memory? We use the .space directive. An integer takes up 4 bytes, usually, so we write
.data variable_name: .space 4
We need to align the data in memory now (we'll explain this below), so we use the .align directive
.data variable_name: .space 4 .align 2 .text
And we've switched back to the text section.
Memory locations in MIPS assembler need to be aligned - that is, that memory locations must begin on the correct location otherwise the processor will crash. 4-byte integers must be aligned every 4 bytes, and so on. The .align n directive aligns the data to a memory cell fit for 2n bytes. Therefore, for 32-bit (4-byte) integers we use .align 2 for 22 or 4 bytes. Aligns the next variable or instruction on a byte that is a multiple of number. To align the space allocated, the 'align n' should come before the 'space x' declaration. For example, here is a sample for allocating 6 words (6 x 4 = 24 bytes) in memory and aligning them at word boundaries
.data .align 2 my_var_name: .space 24