Assignment: ASM
(Edits to this writeup:
- 6 Feb 2017 — describe
.globl
in “hints” below; - 3 Feb 2017 — ask only for “some” of the cavities; added hints below; clarify compilation directions and that it’s okay if questions are answered other than from
objdump
output;
)
Purpose
The purpose of this assignment is to get some practice writing x86 assembly code.
Task
-
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; }
-
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. -
Test your program by running
./a.out
, produced using the instructions in the comment inmain.c
shown above. - 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:- 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. - 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 ofobjdump
output (or whatever you use to find this out) to support your answer, or otherwise describe how you obtained your answer.
- 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
- Upload your completed
VolumeAndDensity.s
file and answers to the above the questions in a file calledanswers.txt
to Collab. (Please use these exact names as it will make grading easier.)
Hints
-
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
-
objdump
calls locations with an executable file “file offsets”. -
If you want to examine the contents of an binary file directly, a hex editor like
ghex
may be helpful. (You can installghex
in your VM usingsudo apt-get install ghex
.)