Computer Organization Lab #3: Arrays

  1. Starting with Program0, modify the C file so that it declares a global array of 100 integers. In the main function, write a for loop that populates the array with the integers 1 to 100. In other words if the array is named 'array' (for example), the for loop should put 1 into array[0], 2 into array[1], etc.

  2. In the C main call an assembly language function named add_elementsA that accepts a pointer to the first element of the array (pointer parameters are passed via eax) and returns the sum of all the array's elements (in eax). The function should be declared in library.h as follows:

            int add_elementsA( int *array );

    You will need to implement this function in library.asm. Don't forget to save and restore all the registers you use (except for eax) with appropriate push and pop instructions.

  3. Modify the C program so that it prints out the result. You can check your program by noting that the sum of the first N integers is N(N+1)/2. Thus the sum of 1 to 100 is 100(101)/2 = 5050.

  4. Modify the Makefile to add the debugging option to the linker command line. It should look something like this:

            wlink sys nt debug Watcom file program0.obj,library.obj

    Do a clean and then rebuild your program. Start the Watcom debugger (wdw) on the executable and experiment with how the debugger works. Note especially what happens when you step into the assembly language function. You should be able to watch the loop in your assembly language code working one instruction at a time. Notice how you can see the effect each instruction has on the registers.

  5. Using the methods of the previous lab, time how long it takes to execute add_elementsA.

  6. Modify the C program again by adding a C version of the function (call it add_elementsC). Time how long it takes to execute add_elementsC.

  7. The Open Watcom environment created by useOW by default causes the compiler to generate unoptimized code that supports debugging. This means add_elementsC is likely much slower than it needs to be. Modify the Makefile to add the options -d0 -ot to the wcc386 command line when compiling program0.c. The command should look like:

            wcc386 -d0 -ot program0.c

    The -d0 option disables debugging support. The -ot turns on optimization for time (makes the program faster). These command line options will override the settings in the environment. Rerun your timing test to see if this improves the performance of add_elementsC.

Pack both your main C program and your library.asm into a zip archive. Include, perhaps in commments (or a separate document), your timing results. Submit your zip archive to Moodle.

Last Revised: 2017-02-15
© Copyright 2017 by Peter C. Chapin <>