CS 202 - Homework 4

Due: Monday 3/12, at the beginning of class

This assignment has a written part and a programming part, both relating to x86-64 assembly programming.

New: detailed notes to help with problem 2 (and 1d).

  1. Written problems

    Please type your answers to the written problems and save them in PDF format in a file hw4_written.pdf. Hint: Read BO chapter 3. Some of the problems below are similar to the practice problems you'll find there.

    1. Assume the following values are stored at the indicated memory addresses and registers:
      Address  Value     Register   Value
      0x200    0xAB      %rax       0x200
      0x208    0xCD      %rcx         0x1
      0x20C    0x15      %rdx        0x13
      0x214    0x42
      Fill in the following table showing the values for the indicated operands in hex.
      Operand          Value    
      %rax             _________
      0x208            _________
      $0x20c           _________
      (%rax)           _________
      12(%rax)         _________
      7(%rax, %rcx)    _________
      512(%rcx, %rdx)  _________
      0x1FC(, %rcx, 4) _________
      (%rax, %rcx, 8)  _________
    2. Suppose register %rax holds value x and %rcx holds value y. Fill in the table below with formulas indicating the value that will be stored in register %rdx for each of the given assemby code instructions.

      Instruction                  Result    
      leaq 3(%rax), %rdx           __________
      leaq (%rax, %rcx), %rdx      __________
      leaq (%rax, %rcx, 8), %rdx   __________
      leaq 7(%rax, %rax, 4), %rdx  __________
      leaq 0xF(, %rcx, 2), %rdx    __________
      leaq 1(%rcx, %rax, 4), %rdx  __________
    3. Assume the following values are stored at the indicated memory addresses and registers:
      Address  Value     Register   Value
      0x200    0xAB      %rax       0x200
      0x208    0xCD      %rcx         0x1
      0x210    0x15      %rdx         0x3
      0x218    0x42
      Fill in the following table showing the effects of the following instructions, in terms of both the register or memory location that will be updated and the resulting value (in hex).
      Instruction                  Destination    Value    
      addq %rcx, 8(%rax)           _________      _________
      subq %rdx, (%rax)            _________      _________
      imulq $16, (%rax, %rdx, 8)   _________      _________
      decq 16(%rax)                _________      _________
      incq %rcx                    _________      _________
      shrq $2, %rax                _________      _________
    4. Reverse-engineer the x86-64 instruction encoding for the instruction
      movq SrcReg, DstReg
      where SrcReg and DstReg each are one of the first eight 64-bit registers (%rax, %rbx, %rcx, %rdx, %rsi, %rdi, %rbp, or %rsp).

      To do this, create an assembly file, say movqtest.s, containing many instructions like

      movq %rax, %rbx
      then assemble the program into object code (gcc -c movqtest.s), then disassemble it to see the hexadecimal encoding (objdump -d movqtest.o).

      (1) Identify the bit patterns used to encode each of the 8 registers:

      %rax = _____
      %rbx = _____
      %rcx = _____
      %rdx = _____
      %rsi = _____
      %rdi = _____
      %rbp = _____
      %rsp = _____
      (2) Write down the complete opcode for "movq SrcReg, DstReg" in binary, using "S" and "D" as placeholders for the bits encoding the two registers. Group binary digits in groups of 4 for readability. For instance, if the opcode was two bytes long and each register was represented using 4 bits (hint: not true), your answer might look like this:
      11SS SS00 0DDD D101
      Answer: ______________________________________________


  2. Programming problems

    Write x86-64 assembly functions that take two long int arguments a and b and return

    1. The (integer) average of a and b.
    2. The maximum of a and b.
    3. The argument whose absolute value is larger.
      Examples: (2, 3) -> 3; (2, -3) -> -3; (-100, -3) -> -100.
      If the absolute values are the same, your function can return either argument.
    4. The sum a + (a+1) + (a+2) + ... + b, or 0 if a > b
      (Note: while this can be computed in closed form, you must use a loop.)
    5. The number of ones in the binary representation of a. Examples: a=3 -> 2; a=15 -> 4; a=16 -> 1; a=-1 -> 64.

    I'm providing a skeleton hw4asm.s. In it, complete the assembly functions "avg", "max", "maxabs", "sumseq", and "countbits". I'm also providing a driver program named hw4main.c to call your functions and print their results. You can find both files in my directory ~schar/cs202/hw4/.

Electronic submission

Upload your files hw4_written.pdf and hw4asm.s using the HW 4 submission page by the beginning of your class section on the due date. As always, make sure your files contain your name and section, and that all your code is properly documented.