A Slightly More Advanced Assembly Exercise


Exercise 1: Converting a Color Image (PPM) to Grayscale (PGM)

For this exercise, you will first write a C program to convert a color image (in PPM format) to grayscale (PGM format). Once you have thoroughly tested your C code to verify it is working correctly, you will convert it into a MIPS assembly program to do the same.

PPM and PGM File Formats

For this exercise, we will use the ASCII file formats of type PPM (color) and PGM (grayscale). For a detailed description of these file formats, please see the Wikipedia entry on Netpbm format. A short description is given below.

The format of a PPM or PGM file is as follows:

Additional Formatting Specifications for This Exercise: Since the MIPS input/output routines (syscalls) available in MARS are somewhat limited, we will put further restrictions on the format of PPM/PGM files as follows:

PPM and PGM Viewers

There are a number of free/open-source viewers available for displaying PPM/PGM image files, including:

C Program

Write a C program that reads a PPM image (on its standard input, i.e., keyboard), and outputs its corresponding PGM image (on its standard output, i.e., console). Keep in mind the following:

Name the file containing your C program ex1.c, and test it on sample inputs provided. First view the output file using GIMP. If it looks good, then you may check it against the sample output provided using the diff command.

Assembly Program

Once your C program is working correctly, write an equivalent MIPS assembly program that implements the same conversion from PPM to PGM images. Do not begin work on the assembly program until you are sure that the C program is working correctly. The best strategy would be to convert your C code (pretty much line-by-line) into its assembly equivalent. Run your program in MARS and verify that it is working correctly.

You can run your program in MARS and test it by typing (or cutting-and-pasting) sample inputs onto the console, and looking at the console output. For the larger examples, you will need to run it in command-line mode and use input/output redirection.

If you are developing your code on a Mac/Linux machine, you can use the following command on your computer within a terminal window (replace /path/to with the actual path to Mars, e.g., /Users/yourid/Desktop/Mars/Mars4_4.jar):


        java -jar /path/to/Mars4_4.jar nc mc CompactDataAtZero ex1.asm < ex1in1.ppm > ex1result1.pgm
If you are using a Windows machine, you will need to login onto classroom.cs.unc.edu, copy your work there, and then run the following command:

        java -jar /home/montek/comp411/bin/Mars4_4.jar nc mc CompactDataAtZero ex1.asm < ex1in1.ppm > ex1result1.pgm
For more information on running MARS in command-line mode, please see http://courses.missouristate.edu/kenvollmar/mars/help/MarsHelpCommand.html.

Name the file containing your assembly program ex1.asm, and test it on sample inputs provided. First view the output file using GIMP. If it looks good, then you may check it against the sample output provided using the diff command.


11 October 2013, Montek Singh, montek@cs.unc.edu