0% found this document useful (0 votes)
53 views

MARS MIPS Simple Userguide

Uploaded by

minhhoangng2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

MARS MIPS Simple Userguide

Uploaded by

minhhoangng2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MIPS Assembly Language

1 Simple Example Program


1 #################################################################
2 # Description:
3 # Simple example program
4 #################################################################
5

6 #################################################################
7 # Main program
8 #################################################################
9

10 # Variables for main


11 .data
12 greeting: .asciiz "Hello, world!\n"
13

14 # Main body
15 .text
16 main:
17 li $v0, 4
18 la $a0, greeting
19 syscall
20

21 # Return to calling program


22 jr $ra

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

• Block comments are simply formed from multiple line comments


1 Example:
2 ###############################################################
3 # This is a block comment
4 ###############################################################

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.

4.1 Directives example

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

13 # Initialization same value 123 for array of 5 integers.


14 ArrayInt: .word 123:5
15

16 # Uninitialized space of 1024 byte buffer


17 ArrayBytr: .space 1024 # 1 KB buffer
18

19 # String within null-terminate at the end


20 Greeting: .asciiz "Hello, world!"
21

22 # String without null-terminate at the end


23 String: .ascii "This string is without a null byte"
24 .text

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.

5.1 terminate execution

1 li $v0, 10 #terminate execution


2 syscall

5.2 print integer

1 addi $a0, $0, 100 # Assign an integer to a0


2 li $v0, 1 # Print integer a0
3 syscall

5.3 print float

1 mov.s $f12, $f3 # Move contents of register $f3 to register $f12


2 li $v0, 2 # Print float number
3 syscall

5.4 print string


reference to 1

5.5 read integer

1 li $v0, 5 # Get integer mode,


2 # $v0 contains integer read
3 syscall
4 add $a0, $0, $v0 # Move integer from $v0 to register $a0

3
5.6 read float

1 li $v0, 6 # Get float number


2 syscall # $f0 contains float read

5.7 print character

1 addi $a0, $0, ’A’ # Display character ’A’


2 li $v0, 11 # print char
3 syscall

5.8 read character

1 li $v0, 12 # Get character


2 # $v0 contains character read
3 syscall
4 add $a0, $0, $v0 # Move character to register

5.9 read string

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

You might also like