Our First Few Simple MIPS Assembly Programs


Your assignment must be submitted electronically by 11:59pm on Wednesday, October 25.


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, under "Reference Materials (for Labs)"). 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: Sum.asm, SumArray.asm and Fibonacci.asm.

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. First, study the C version of the program (ex1.c), and compile and run it. Your task is to convert this C program into an equivalent MIPS assembly program.

For writing the MIPS assembly version, 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.

The calculation of the gray value should be done in a separate procedure called rgb_to_gray. For this simple task, there is no need to create and use a stack. This is because there are no nested procedure calls, and the calculation is fairly trivial, so no sophisticated management of register saves/restores is needed.

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 produces the correct output (see near the top of the starter file).


Test Inputs, Due Date and Submission Procedure

There are no input files needed in this assignment. The input data is embedded within the assembly file. The expected output is also specified as a comment at the top of the file. Your assignment must be submitted electronically by 11:59pm on Wednesday, October 25.

First copy the file ex1.asm to the CS Dept server (classroom.cs.unc.edu) under the appropriate folder in your home directory (e.g., comp411lab/lab6). Here is how to transfer files between your personal computer and the server:

Then, log in to classroom, and run the self-checking and submit scripts as you normally do:

% cd ~/comp411lab/lab6
% /home/montek/comp411/bin/selfchecklab6
% /home/montek/comp411/bin/submitlab6

In case of any problems, please contact the instructor or the TAs.


18 October 2017, Montek Singh, montek@cs.unc.edu