Homework 1: Numerical Representations and Disassembly of Object Files

Assigned Wednesday, January 18, 2017
Due Date Monday, January 30, 2017 at 5:00pm (17:00)
Submissions Submit a PDF file of your solutions to the course's Assignment Drop Box.

Introduction

The purpose of written homework assignments is to get you thinking about the topics being covered in lecture and in readings in the textbook that are not represented in the hands-on, programming lab assignments. These written assignments also better prepare you for exams. It is worth noting that the book contains many practice problems similar to the problems we ask on these written assignments! The solutions for those practice problems are located at the end of each chapter and should give you a feel for the kind of answers we expect you to turn in for these kind of assignments.

Logistics

Please write up your answers one page per problem and submit them as a PDF file in the online dropbox. This simplifies our grading and ensures we read your answers clearly. We recommend that you type up your answers (Word or just plain-text is probably fine) as it ensures we can read them easily, but you may also scan a hand-written assignment and submit it as a PDF, provided it is legible.

We will provide solutions to all of the problems in the written homework assignments in a timely fashion after the assignment is due. This may be around 4 or 5 days after the due date, in general, because some students may use late days.

Make sure you are using the 3rd edition of Computer Systems: A Programmer's Perspective. If you're not using the right book, you might be doing the wrong problems!

Questions

Answer the following problems, some from the 3rd edition of the textbook.

  1. Suppose we are given the task of generating code to multiply integer variable x by various different constant factors K. To be efficient, we want to use only the operations +, -, and <<. For the following values of K, write C expressions to perform the multiplication using as few operations as you can: Hint: You can do all three multiplications with a total of 11 operations. Note: You may find Practice problem 2.40 (p. 103) useful to get started on this problem, it also goes into more detail on how to choose which approach will be more efficient.
  2. Homework Problem 2.82 (pp. 135-136), parts A, B, and C.
  3. Consider this Java method, which uses the Java standard library's Math.abs for computing the absolute value of an int:
    public static void interesting(int x) {
        if (Math.abs(x) < 0) {
            System.out.println("absolutely strange");
        }
    }
    
    1. There is one argument to interesting that causes absolutely strange to be printed. What is that argument in binary (base-2) and decimal (base-10)?
    2. Give code (in C or Java) for how to implement absolute value in a straightforward way (do not use shifts or masks or anything like that).
    3. Explain how your code in part (b) leads to the behavior observed in part (a). Explain both in terms of “numbers” (you can use base-10) and in terms of “bits” what happens when the code executes.
    4. What is the C library function for computing the absolute value of a C int and what is its definition when given your answer to part (a)? Feel free to use web-search when answering this part.
  4. Recall that a floating point number is of the form: (-1)S x Mantissa x 2Exponent. The mantissa and exponent refer to the VALUES (not the binary encoding). Also recall that the mantissa ranges from 1.0 to (almost) 2.0. Tell us the decimal values of the mantissa and exponent for the (decimal) number 13.6875 and how this would be encoded in IEEE 754 representation (32 bits), including the binary encodings M and E of the representation. (Recall M = Mantissa - 1.0 for normalized numbers.) Your answer should contain these parts and you should show how you calculated each value:
  5. Homework Problem 3.58 (pp. 311). You may use any number of temporary variables in your solution; your code does not have to match up exactly line-by-line with the given assembly code. It just needs to be C code that accomplishes the same thing.
  6. For this problem you will use the jack-of-all-trades tool objdump to examine executable programs on Linux. Be sure to work on this problem on the CSE home VM or attu. You will need a copy of the code that you turned in for Lab 1 (your code does not have to be entirely correct or complete, but at least some of your puzzle functions should be filled in).
    1. Run make in your Lab 1 directory to build your code. Run the command objdump -t btest | grep text and examine the output. What type of thing do some of the strings in the rightmost column of the output represent? (you do not need to identify what all of them refer to)
    2. Use objdump to disassemble your btest code (review the lecture slides or check the man page to find the right flag). Find the labels in the assembly code corresponding to your bit puzzle functions. What x86 assembly instructions appear to perform right-shift and left-shift operations?
    3. Most Linux distributions include several other useful programs for examining binary files containing machine code. Try out commands such as ldd, file, nm, strings and readelf, and try other flags with objdump. Report something neat, unusual, or unexpected that you find. Don't forget to use the man pages to learn more about all of these programs.
    4. Extra Credit (a little): run the objdump -t command on a different program on the system - choose one in the /usr/bin/ directory. You will likely see the output "SYMBOL TABLE: no symbols". Why does objdump -t appear to work on your program, but not on others?