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:

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. The sample output provided may differ from your program's output in terms of spaces and newlines. Be sure to check that they are otherwise identical, i.e., all the numbers contained in them are correct.

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 have a Mac/Linux machine, you can use the following command on your computer within a terminal window:


        java -jar /path/to/Mars_4_2.jar nc mc CompactDataAtZero ex1.asm < ex1in1.ppm > ex1result1.pgm
If you have 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/Mars_4_2.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. The sample output provided may differ from your program's output in terms of spaces and newlines. Be sure to check that they are otherwise identical, i.e., all the numbers contained in them are correct.


5 October 2012, Montek Singh, montek@cs.unc.edu