Assembly Language Lab

CS42: Principles of Computer Organization
Spring Semester, 2000

Ray Ontko
Department of Computer Science
Earlham College

The purpose of this lab is to give you a chance to examine and modify assembly language programs.

Imagine that you are working on a program with a time-critical portion which you would like to improve. Your program (written in C) is using a "trial division" approach to factoring primes, and at one point you calculate both the quotient (/) and remainder (%). You recall (from your computer organization course in college) that the 80386 has an instruction which computes both simultaneously during division and wanted to find out if the compiler is using this optimal approach, and if not, fix it.

So, you wrote a small test program, divrem.c, to see how the compiler handles the situation:

/* 
divrem.c

A test program to investigate performance improvements
in programs containing / (div) with % (rem).

Ray Ontko, 04/16/2000
*/

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i ;
int a ;
int b ;
int q ;
int r ; 

a = 23 ;
b = 5 ;

for ( i = 0 ; i < 100000000 ; i ++ )
  { 
  q = a / b ;
  r = a % b ;
  }

printf( "a %d  b %d  q %d  r %d\n", a , b , q , r ) ;

exit( EXIT_SUCCESS ) ;
}
Using this program, do the following:
  1. Connect to a system which has the Gnu C compiler (gcc) and the Gnu assembler (as) and runs on an Intel 386 processor. Copy the program listed above to a file called divrem.c.

  2. Use the following command to compile the program:
    cc divrem.c -o divrem
    

  3. Execute the program using "time" or "/usr/bin/time" to display timing statistics:
    time ./divrem
    /usr/bin/time ./divrem
    
    Make note of the different execution statistics given.

  4. Now, use the -S option of the Gnu compiler to stop the compilation and save the assembly listing. Type the command:
    cc -S divrem.c
    
    This produces a file "divrem.s" which contains the assembly language equivalent of your original program.

  5. Examine the assembly language listing. Can you explain the meaning of each line in terms of the original program?

  6. Verify that you can compile and execute the assembly language listing:
    rm divrem
    cc divrem.s -o divrem
    time ./divrem
    /usr/bin/time ./divrem
    
    Here we delete the originally compiled version of the C program, assemble and link "divrem.s" to the file "divrem", and finally re-run the program. Again, note the timing statistics. How do they compare to the initial run?

  7. Using your favorite unix editor (e.g., vi) modify the assembly language file to eliminate any redundant code that you think might affect the performance of the program.

  8. Reassemble and test your new program. Does it work correctly? How do the execution statistics compare?
    rm divrem
    cc divrem.s -o divrem
    time ./divrem
    /usr/bin/time ./divrem
    
    Show your program and explain your results to the person next to you.

When you have completed the lab, turn in your timing statistics and a brief writeup of what changes you made to the assembly listing on the Intel 386.

Copyright © 1997, 2000, Ray Ontko (rayo@ontko.com). All rights reserved.