MARS MIPS Simple Userguide
MARS MIPS Simple Userguide
6 #################################################################
7 # Main program
8 #################################################################
9
14 # Main body
15 .text
16 main:
17 li $v0, 4
18 la $a0, greeting
19 syscall
20
2 Memory segments
We must mark each part of the program as text(text segment or called code segment) or data(variable segment)
reside in the same memory while the program is running
1 .kdata
2 #variables for kernel
3 .ktext
4 # text for kernel
5
6 .data
7 # Variables for main
8
9 .text
10 # Main body
11 # Your code start here
The .text segments can contain only instructions, while the .data segments contain only variable definitions.
3 Instruction Format
An MIPS instruction has the following structure:
1 label: opcode/directive operand, operand, operand # comment
3.1 Label
• The label portion of a statement must begin in column 1 and must end with a ’:’. The ’:’ is not part of
the label. It only serves to visually distinguish a new label definition from other program elements.
• Each label represents a memory address in assembly language. It could be the address of data or the
address of an instruction (i.e. labels can appear in both .text and .data sections).
• A label represents the address of the instruction or data element that immediately follows it, whether it
follows on the same line or a subsequent line.
• Labels defined in a .data section are like variable names and follow the same naming rules. They must
begin with a letter or underscore, and can contains letters, underscores, and digits. Variable names cannot
be keywords.
• Labels defined in a .text section represent the address of an instruction, and are used as arguments by
jump, branch, and return instructions to "go to" that instruction.
3.2 Comments
• A comment is anything start from a “#” to the end of the line.
1 Example: # this is a comment
4 Directives
• Directives do not represent machine instructions, hence they are not executed at run-time. They direct
the assembler to do something while translating the program to machine language, such as allocate space
for a variable and give it an initial value, which it will have when the program begins executing.
• Directives can be distinguished from instructions by the fact that they begin with a ’.’.
• Directives must be indented (they cannot begin in column 1).
• Data allocation directives are somewhat like variable definitions in high-level languages, but are not quite
as meaningful. They usually, but not necessarily, follow a newly defined label (variable) and must be
followed by one or more initial values.
Directive Descriptions
.data <addr> The following data items should be stored in the data segment. If the optional argument
addr is present, the items are stored beginning at address addr.
.text <addr> The next items are put in the user text segment. If the optional argument addr is present,
the items are stored beginning at address addr.
.kdata <addr> The following data items should be stored in the kernel data segment. If the optional
argument addr is present, the items are stored beginning at address addr.
.ktext <addr> The next items are put in the kernel text segment. If the optional argument addr is present,
the items are stored beginning at address addr.
.word 32-bit integer
.half 16-bit integer
.byte 08-bit integer
.float floating point IEEE 754 single precision
.double floating point IEEE 754 double precision
.space Uninitialized memory block (byte)
.ascii Store the string in memory, but do not null-terminate it.
.asciiz Store the string in memory and null-terminate it.
.align Align the next datum on a 2n byte boundary.
1 .data
2 age: .word 30 # 32-bit word initialized with decimal
3 class: .half 0x10 # 16-bit word initialized with hex
4 grade: .byte 08 # 8-bit word initialized with octal
2
5 .align 2 # Aligns next element to multiple of 2^2
6
7 height: .word 70
8 gpa: .float 3.65 # 32-bit floating point
9
10 # Array of 6 integers
11 ArrayInt: .word 1, 2, 3, 54, 24, 120
12
5 syscall instruction
A number of system services, mainly for input and output, are available for use by your MIPS program. They
are described in the table below.
3
5.6 read float
1 .data
2 strIn: .space 100 #create 100 bytes space
3
4 .text
5 main:
6 la $a0, strIn # Get address to store string
7 addi $a1, $0, 10 # Input 10 characters into string (includes end of string)
8 li $v0, 8 # Get string mode
9 syscall