COSC 201                            Computer Organization                            Fall, 2011

Lab 2:    Loops and Conditionals in assembly language

Purpose:                              Learn how loops and conditional expressions are implemented in assembly language.

Method:                              Write assembly code to implement a simple program given in C, using the outline supplied.

Preparation:                        Read chapter 2 of H&P

Files to use:                       posrun.cpp, main.s, posrun.s , out.txt

Hand-in:                             posrun??.sposrunopt??.s

The file posrun.cpp contains a simple C++ program which finds the length of the longest contiguous sequence of positive values in a constant array given in the program (so as to avoid using input).  The file posrun.s contains an outline of MIPS assembly code for this procedure as might be generated by a straightforward, non-optimizing compiler.  The file main.s contains a program that tests this procedure for several arrays (using an intermediate test procedure).

Your first task is to fill in the correct assembly code for posrun.s.  One instruction per commented line, as guided by the comments should do it.  You should save your assembly program as posrun.s and test it using MARS. Be sure that your files main.s and posrun.s are in a single directory and your MARS settings have the setting "assemble all files in directory" checked. Load the file main.s and assemble it; the other file will also be assembled and you can run your code.  The correct output for the data in main.s is given in the file out.txt. You may test your program with other array values by changing the appropriate entries in main.s

Your second task is to optimize the loop in this program so as to decrease the number of instructions executed on each iteration of the loop.  This may involve adding additional instructions that are executed either before or after the loop. Create a new directory with both the file main.s and your optimized code as posrunopt.s.  Include comments in your optimized code which indicate what changes you made and how they improve the code.  Test using MARS. You can get instruction counts on both your original code and your optimized code by going to the tools and selecting the Instruction coun ter and clicking "connect to MIPS" before you run the code. You will get a count of instructions executed. (10 bonus points to anyone who beats my best optimization on the given arrays.)

Hand in your two files posrun.s and posrunopt.s by emailing them to the instructor. Be sure that your files have your name or initials as part of the filename, e.g. posrunCN.s and posrunoptCN.s