The following is a first MIPS assembly program. It prints out the string "
Hello World". To run the program, first start the MARS program. Choose the
File->New menu option, which will open an edit window, and enter the program. How to run the program will be covered in the following the program.
Program 2-1: Hello World program # Program File: Program2-1.asm # Author: Charles Kann # Purpose: First program, Hello World .text # Define the program instructions. main: # Label to define the main program. li $v0,4 # Load 4 into $v0 to indicate a print string. la $a0, greeting # Load the address of the greeting into $a0. syscall # Print greeting. The print is indicated by # $v0 having a value of 4, and the string to # print is stored at the address in $a0. li $v0, 10 # Load a 10 (halt) into $v0. syscall # The program ends. .data # Define the program data. greeting: .asciiz "Hello World" #The string to print.
Once the program has been entered into the edit window, it needs to be assembled. The option to assemble the program is shown circled in red below.
If you entered the program correctly you should get the edit screen below. If you made any errors, the errors will be displayed in a box at the bottom of the screen.
To run the program, click the green arrow button circled in the figure below. You should get the string "
Hello World" printed on the Run I/O box. You have successfully run your first MIPS program.
2.3.1: Program 2-1 Commentary
Program 2-1 was used to show how to compile and run a program. This is not an introductory programming class, and most readers probably are less interested in how to make the program work, and more interested in the details of the program. This section will go over a number of those details, and help the reader understand the program.
- MIPS assembler code can be indented, and left white space on a line is ignored. All instructions must be on a single line, The # is means any text from the # to the end of a line is a comment and to be ignored. Strings are denoted by "'s marks around the string.
- Note the comments at the start of the file. These will be called a file preamble in this text. At a minimum all program should contain at least these comments. The name of the file should be documented, as unlike some HLL such as Java, the name of the file is no where implied in the program text. It is very easy to lose files in this situation, so the source code should always contain the name of the file. The file preamble should also contain the programmer who created the code, and a short description of why this program was written.
- Assembly language programs are not compiled, they are assembled. So a program does not consist of statements and blocks of statements as in a HLL, but a number of instructions telling the computing machine how to execute. These instructions are very basic, such as
li $v0, 4(put the value 4 in
$v0). Because this is a complete change of perspective from a HLL, it is useful to explicitly make this point, and for the reader to take note of it.
- In MIPS and most assembly languages in general, a "." before an text string means the token (string) that follows it is an assembler directive. In this program the
.textdirective means the instructions that follow are part of a program text (i.e. the program), and to be assembled into a program and stored in the text region of memory. The
.datadirective means that what follows is program data, and to be stored in the static data region of memory. The
.asciizdirective tells the assembler to interpret the data which follows it as an ASCII string. Typing a "." in the MARS edit window will give you all the MIPS assembler directive.
- In MIPS assembler any text string followed by a ":" is a label. A label is just a marker in the code that can be used in other statements, as the
la $a0 greetinginstruction says to load the text at the label greeting into the
$a0register. Note that data labels are not equivalent to variables. Labels are just markers in the program, and nothing more. Variables have a type, and there is no typing of any data done in assembler.
- The label
main: does not need to be included as MARS assumes the program begins at the first line in the assembled program. But it is nice to label the starting point, and generally most runtimes will look for a global symbol name main as the place to begin execution. So in this text it will just be included.
- Any time a constant is included in an instruction, it is called an immediate value. The constant must be in the instruction itself, so the value 4 in the instruction
li $a0,4is an immediate value, but the string "Hello World" is a constant but not an immediate value.
- Only instructions and labels can be defined in a text segment, and only data and labels can be defined in a data segment. You can have multiple data segments and multiple text segments in a program, but the text must be in a text segment and the data in a data segment.
- Operators are text strings like
li, la, and
syscall. limeans load the immediate value into the register (e.g.
li $v0,4means $v0 <- 4). la means to load the address at the label into the register.
syscallis used to request a system service. System services will be covered in more detail later in this chapter. A list of all MIPS operators used in this text can be found in the MIPS Greensheet or by using the help option in MARS.
- Instructions are operators and their arguments. So
liis an operator;
li $v0,4is an instruction.
- The syscall operator is used to call system services. System services provide access to the user console, disk drives, and any other external devices. The service to be executed is a number contained in the
$v0register. In this program there are two services that are used. The services are: service 4 prints a string starting at the address of the memory contained in the
$a0; and service 10 halts, or exit, the program. A complete list of all syscall be found by using the help menu option (or F1) in MARS. This will bring up the following screen, which gives all the possible options in MARS.