Our First Few Simple MIPS Assembly Programs


Exercise 0

For this exercise, you will download the MARS assembler, and run a few simple MIPS assembly programs from the course website.

First, download MARS (link on course website). Launch it by double-clicking the .jar file. Familiarize yourself with the menu.

Configure it as follows: Settings->Memory Configuration->Compact, Data at Address 0. This setting tells the assembler where it can expect to place data and code in memory.

Download three assembly files from the course website (under Lecture 7): Sum.asm, SumArray.asm and Fibonacci1.asm (you may skip Fibonacci2.asm for now).

Use the File->Open menu to open one of these programs. Assemble (i.e., compile) the program by hitting Run->Assemble, or by hitting the screwdriver/wrench icon, or by pressing F3. Run the program by hitting Run->Go, or by hitting the icon with the play button. You may single-step through the program by hitting Run->Step, or hitting the icon with the '1' to the right of the play button. You can also set one or more breakpoints by checking the box to the left of the instructions where you want execution to break, and then hit Run.

The bottom pane has two tabs: Mars Messages shows errors or warnings during assembly; Run I/O is the input/output.

Run each of the three programs. Make sure you understand every single line of code. Here is what to expect for each:

MARS syscall

There are several library routines provided by MARS that an assembly program can use. These are called system calls, or syscall. These services include support for printing integers and strings (similar to the printf() function in C), reading integers and strings from the keyboard (similar to scanf() in C), memory allocation (similar to malloc() in C), exiting from a program (similar to return from main() in C), etc.

An assembly program accesses those services using the syscall command. There is only one syscall command for all these services, but which service is requested is determined by the values provided in certain registers. The value in register $v0 determines which service is requested, and often parameters are passed to the service using registers $a0, $a1, $a2 and $a3. If a value needs to be returned to the program (e.g., reading an integer from keyboard), it is typically returned in register $v0.

For a full listing of system calls available in MARS, please refer to http://courses.missouristate.edu/kenvollmar/mars/help/syscallhelp.html. We will mostly be using system calls numbered 1 to 17.

For example, to exit a program, you would use syscall with 10 in $v0:

	ori $v0, $0, 10				#System call code 10 for exit
	syscall					#exit the program
Note: Sometimes the instruction ori $v0, $0, 10 is shortened to the pseudo instruction li $v0, 10, which stands for ("load the immediate value 10 into $v0"). As another example, to print a string located at location myString, you would use syscall with 4 in $v0:
	li $v0, 4				#System call code 4 for printing a string
	ori $a0, $0, myString 			#address of string to print is in $a0
	syscall					#print the string

Study all of the system calls from 1 to 17.


Exercise 1

For this exercise, you are to write an assembly program to convert red-green-blue (RGB) values for a set of pixels into a single gray value. Use the starter file ex1.asm. Your code must strictly follow these specifications:

You are given an array called pixels, each element of which is a 32-bit word representing a color value. The lowest significant 8 bits of each color value denote an unsigned integer (from 0 to 255) representing the color's "blue" value, the next 8 bits are the "green" value, the next 8 bits are the "red" value, and the most significant 8 bits are all zeroes. For example, the pixel with value 0x0001ff22 has color components: red = 1 (or 0x01), green = 255 (or 0xff), blue = 34 or (0x22).

For this programming assignment, you will read through this array of pixels, and for each pixel, convert the color pixel into a grayscale pixel using a simple formula: gray value = (red + green + blue) / 3. Note the division is integer divide and truncate (i.e., no rounding needed). For the above example, the gray value would be (1+255+34) / 3 or 96.

After calculating the gray value for a pixel, print it out to the console (only one element per line). The program keeps reading RGB values and printing the corresponding gray value, until it encounters an input of -1. The expected output format is given in the comments in the starter file on the course website.

Hint: Study and understand the divide instruction thoroughly before attempting to use it!

Name the file containing your code ex1.asm. Assemble and run your program in MARS, and make sure it runs correctly on the sample inputs provided (see near the top of the starter file).


28 September 2012, Montek Singh, montek@cs.unc.edu