CSE 351: The Hardware/Software Interface

Spring 2016 Course Website Return home »

Homework 2: Assembly Programming

Assigned Tuesday, April 19, 2016
Due Date Friday, April 29, 2016 at 11:59PM (23:59)
Submissions Submit a PDF file of your solutions using the course's Assignment Drop Box.


The purpose of written homework assignments is to get you thinking about the topics being covered in lecture and in readings in the textbook which 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.


Please write up your answers 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 even 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, 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!


Answer the following problems, some from the 3rd edition of the textbook. Notice that several of these problems are practice problems. If a practice problem is listed, try to solve the problem on your own first, then check your answer at the end of the chapter. Make sure you understand the solution provided, then complete the additional questions we ask about the practice problem below. Your write-up only needs to contain the information necessary to understand your answer to the additional questions listed here — you do not need to turn in the answer to the practice problem in its entirety.

  1. Practice Problem 3.2, p. 185.
    • What is the appropriate instruction suffix for: mov_ (%rsi), %eax?
  2. Practice Problem 3.3, p. 186. (Note that there are a few typos in the solution to this problem on p. 326)
    • Describe a problem with: movq %eax, 4(%rsi).
  3. Practice Problem 3.6, p. 192.
    • What is the result stored in %rdx for this instruction: leaq 0x10(%rax,%rax,2), %rdx.
  4. Practice Problem 3.8, p. 194.
    • Assuming the state of registers and memory after all of the instructions in the problem have been executed, describe the destination affected and the value placed there by this instruction: leaq (,%rax,2), %rdx.
  5. Homework Problem 3.60, p. 312. Notice that there is a typo in the partially filled-in C code on p. 313 - parameter n should be of type int.
  6. This problem considers this sequence of assembly that contains two functions f and g using the standard x86-64 calling convention:
    	cmpq	$-12, %rsi
    	jne	.L2
    	movq	%rsi, %rax
    	movq	%rdi, %rsi
    	movq	%rax, %rdi
    	call	f
            addq    $1, %rdi
    	movq	%rsi, %rax
    	imulq	%rdi, %rax
    	leaq	-8(%rdi), %rsi
    	leaq	(%rdi,%rdi,2), %rdi
    	call	f
    1. Fill in the blanks below such that the assembly for f could be an implementation of the C function. Each blank should be an expression with as many or as few operators as you need, but keep the expressions as simple as possible.
      long f(long x, long y) {
        if (y == _____)
          return f(___, ___);
        return ______; 
    2. Suppose f is called with -2 in %rdi and 3 in %rsi. At each step of assembly execution until this call to f returns, show the value in each of the following registers: %rsi, %rdi, %rax. Use a comment to make clear what instruction is next to execute. Here is the first line of your answer, so you can see the format we expect:
          %rdi: -2, %rsi: 3, %rax: unknown  # before cmpq $-12, %rsi
      To help you get started and make sure your formatting is correct, we have created this this template for you to start from: hw2_prob6_format_helper.txt.
    3. Repeat the previous part now supposing f is called with 4 in %rdi and -12 in %rsi. Note some instructions may execute more than once — continue until the original call to f returns. The template file (hw2_prob6_format_helper.txt) will help you with this one too.
    4. Repeat part (a) for function g, filling in these blanks and remembering to simplify your arithmetic expressions:
      long g(long z) {
        return f(_____,  _____);
    5. Find an argument to g that causes a Segmentation Fault if you execute g with that argument. Explain in English why a Segmentation Fault occurs. To confirm that you get a Segmentation Fault when (and only when) you expect to, you can put the assembly code for g and f in a .s file along with the code below. Then change the argument that main calls g with, assemble to an executable with gcc, and run the program. (Making main “global” is necessary so the linker can create an executable that calls main correctly.)
              movq    $42, %rdi
              call    g
              movq    $0, %rax
      	.globl	main
      	.type	main, @function