Assignment: ASM

(Edits to this writeup:

)

Purpose

The purpose of this assignment is to get some practice writing x86 assembly code.

Task

  1. Examine the C file main.c. It contains the following code:

     // main.c : Defines the entry point for the console application.
     // use "gcc -O2 -o a.out main.c VolumeAndDensity.s" to compile
     #include <stdio.h>
    
     void VolumeAndDensity(int length, int width, int height, int mass, int *volume, int *density);
     int main() {
             int volume, density;
             int length, width, height, mass;
             printf("Enter length, width, height, and mass\n");
             scanf("%d %d %d %d", &length, &width, &height, &mass);
    
             VolumeAndDensity(length, width, height, mass, &volume, &density);
             printf ("Object dimensions: %d x %d x %d. Mass: %d\n",
              length, width, height, mass);
             printf ("Volume is %d\n", volume);
             printf ("Density is %d\n", density);
             return 0;
     }
    
  2. Write an implementation of the function VolumeAndDensity that this code calls. The function should store the computed values of volume and density into the pointers passed as its last two arguments.

  3. Test your program by running ./a.out, produced using the instructions in the comment in main.c shown above.

  4. Use objdump (or a similar utility) to examine the ./a.out file you produced. You may wish to refer to the objdump manual. Answer the following questions:
    1. Are there any “cavities” – unused space, such as sequences of unreachable nops – in the executable? We are interested in these because a virus could use them to store code or data in the executable without changing its size. If there are cavities, where are some? Provide snippets of objdump output (or whatever you use to find this out) to support your answer.
    2. At what location in the executable file does your VolumeAndDensity function appear? (Give the location within the file itself, not the location at which the machine code will appear in memory when the executable runs.) Provide snippets of objdump output (or whatever you use to find this out) to support your answer, or otherwise describe how you obtained your answer.
  5. Upload your completed VolumeAndDensity.s file and answers to the above the questions in a file called answers.txt to Collab. (Please use these exact names as it will make grading easier.)

Hints

  1. To make the function VolumeAndDensity usable by other object files, you may need to explicitly indicate that it needs a “global” symbol, such as using the .globl directive:

     .globl VolumeAndDensity
    
  2. objdump calls locations with an executable file “file offsets”.

  3. If you want to examine the contents of an binary file directly, a hex editor like ghex may be helpful. (You can install ghex in your VM using sudo apt-get install ghex.)