Retro prof in the lab University of Washington Computer Science & Engineering
 CSE 378 Spring 2007
  CSE Home   About Us    Search    Contact Info 

Homework #2 - Programming in ASM and ASM Optimization

Assigned: 4/2/2007
Due:4/10/2007 at 5pm

Introduction

Now that you have had a chance to learn the basics of ASM programming, you will try a more advanced exercise involving converting a program from C to ASM without the help of a compiler. This will allow you to get an understanding of how to write programs for your processor in the first lab as well as help you understand what is going on when you actually run a program. Additionally, this assignment will introduce you to the concept of optimizing ASM. Unlike with higher-level programming languages, you can normally pull off a few more tricks with ASM code in order to make it run faster, mostly by using faster instructions or taking advantage of the instruction set available to you to accomplish more than one thing at once. While not our primary emphasis, this will give you some ideas about how to write better ASM code.

Converting C to ASM

For this portion of the exercise, you will convert three C programs into the corresponding assembly for those programs. The programs that you will convert are:
  • forloop.c - A for loop
  • funccall.c - A function calling example
  • strcat.c - C string concatenation function. This function has heavy pointer usage, if you've forgotten what pointers are, click here or here for a refresher.
To assist you in testing your code, you will be able to use SPIM to simulate execution of your code. In order to utilize SPIM for purposes of the simulation, please download the template file and fill it in with your code. Once you have completed this, you will be able to load that file in SPIM and simulate the execution of your code to verify that your code is correct. Here are some useful links for using SPIM:
  • You can download SPIM from the SPIM homepage
  • Appendix A of your textbook contains instructions on how to use SPIM. This section of the book is available on the included CD or online.
  • More detailed documentation on SPIM (including the command line version) is available here
  • If you want to create ASCII strings in SPIM for testing your programs, put them in the .data section and declare them using .asciiz.
Also, note that SPIM is already installed on most lab machines, so you will be able to use the installed version to test out your code if you don't want to install it at home.

Here are some things to keep in mind when you write your code:
  • Comment your code well. Someone who has never seen the assignment should be able to read your code and figure out from the code and comments what it does.
  • Use sensible label names. It makes your code much more readable and probably easier for you to debug if you use labels with names like "startReadLoop" rather than "L1".
  • Always get working code before trying to optimize. While it would be nice if all the code you turned in was as efficient as it could be, we are more concerned about your code working. If you manage to get a working version of the code and then want to optimize it before turning it in, feel free to do so, but keep the original working version somewhere so that you can turn that in if your optimizations don't go as well as you hoped.
  • Even with the point above, please don't write a program that uses a lot of extraneous and unnecessary code. If your program is unreasonably large for the assignment, we may choose to take points off. We will be reasonable about what we consider acceptable, but the three programs that you are converting should not be particularly large ASM programs.
Convert all three of the programs that are provided, then save them as forloop.s, funccall.s, and strcat.s.

Optimizing ASM

You will now practice optimizing ASM code that has been provided to you. For this part of the exercise, we have taken the output from the cross compiler that you used for your last exercise and provided it to you along with the original C code that produced the ASM. This code was produced by a compile with optimizations turned off, so there should be plenty of room for you to improve it. Here are the files that you will need: The goal of this exercise is to rewrite parts of the ASM provided so that the program executes faster. To help you out with code optimization, here is a short and very non-exhaustive list of optimizations that you might want to try:
  • As you may have learnt in 370, operations such as multiply are very expensive to perform even in hardware. You can sometimes replace these expensive operations with less expensive operations. (Hint: shifting left and right are much faster ways to perform certain multiplications and divisions)
  • You can sometimes rewrite loops so that they have only a branch instruction rather than a branch and a jump. The applicability of this point varies, so be sure that doing so will not alter the program.
  • Keeping memory synchronized with the register contents associated with a particular variable is not always necessary. In most cases, you may not have to do it at all.
Keep the following points in mind when optimizing the code:
  • Your optimizations must preserve the original functionality of the code. That is, no matter what you do to it, the code must produce the same end results as the original under all circumstances.
  • Comment your optimizations. This is especially true if you find a particularly obscure way to optimize the code. You should explain exactly what you are doing and how it is an optimization to the code.
  • Your optimizations should NOT remove function calling conventions. Do not remove any code used to allocate/deallocate space on the stack while optimizing.
  • You can rename labels as you wish to make it easier to read, as long as your renaming is done correctly and doesn't change the functionality of the program.
  • You are free to start from scratch if you have a better way to write the program, as long as the functionality is preserved.
Save the optimized version of your program as strstr.s.

Turnin

Once you have commented all the files, submit them for grading via turnin. Place the three .s files in a location accessible via UNIX, and log into attu via SSH. Change your directory so that you are in the same directory as the files that you wish to submit. Once there, run "turnin -c cse378 -p hw2 forloop.s funccall.s strcat.s strstr.s" to submit the files for grading. You do not need to zip the files up or compress them in any other way, as this will be handled by turnin.

A few notes about turnin:
  • You may submit your assignment as many times as you wish until the due date. Only the latest version of what you have turned in will be kept and graded.
  • If you are using a late day, run "turnin -c cse378 -p hw2_late forloop.s funccall.s strcat.s strstr.s" to turn in your assignment. The late turnin will be available for 24 hours starting at the end of the regular turnin.


CSE logo Computer Science & Engineering
University of Washington
Box 352350
Seattle, WA  98195-2350
(206) 543-1695 voice, (206) 543-2969 FAX
[comments to Shen]