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 to see how the compiler handles the situation:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int i ;
int a ;
int b ;
int q ;
int r ;
a = 23 ;
b = 5 ;
for ( i = 0 ; i < 1000000 ; 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:
cc test.c -o test
time ./testMake note of the different execution statistics given.
cc -S test.cThis produces a file "test.s" which contains the assembly language equivalent of your original program.
rm test cc test.s -o test time ./testHere we delete the originally compiled version of the C program, assemble and link "test.s" to the file "test", and finally re-run the program. Again, note the timing statistics. How do they compare to the initial run?
rm test cc test.s -o test time ./testShow your program and explain your results to the person next to you.
cc test.c -o test time ./testIs the program faster or slower on the DecStation?
cc -S test.cHow does the resulting "test.s" compare with that produced on the Intel machine? Are you able to explain the new assembly listing line by line? Do you notice any obvious improvements to the div/rem logic?
Copyright © 1997,
Ray Ontko
(rayo@ontko.com).
If you're curious about why I copyright, see
Peter Suber's
Why
Copyright Web Documents?.