CMSI 284
Homework #4

Submit, in hardcopy, answers to the following problems generated form LaTeX source according to the usual homework submission guidelines. Also build up your CVS repository with the following:

    /homework/cmsi284/hw3.tex
    /homework/cmsi284/src/main/nasm/chars.asm
    /homework/cmsi284/src/main/nasm/reversed_args.asm
    /homework/cmsi284/src/main/nasm/gcd.asm
    /homework/cmsi284/src/main/nasm/callgcd.asm
    /homework/cmsi284/src/main/nasm/medians.asm
    /homework/cmsi284/src/main/nasm/log7.asm
    /homework/cmsi284/src/main/c/apps/callgcd.c
    /homework/cmsi284/src/test/c/gcd_test.c
    /homework/cmsi284/src/test/c/median_test.c
    /homework/cmsi284/src/test/c/log7_test.c

Make sure that you have run the setup-class script so that I can checkout and run your code from CVS while grading.

  1. Consider the function with three inputs (A, B, C) and two outputs (X, Y) that works like this:
         A  B  C | X  Y
        ---------+------
         0  0  0 | 0  1
         0  0  1 | 0  0
         0  1  0 | 0  1
         0  1  1 | 1  0
         1  0  0 | 0  0
         1  0  1 | 1  1
         1  1  0 | 1  0
         1  1  1 | 0  1
    
    Design two logic circuits for this function, one using AND, OR and NOT gates only, and one using NAND gates only.
  2. Give x86 logic instructions to perform the following operations on the ebx register:
    1. Clear bits 8, 4, 6, 22, 20 and 16
    2. Complement bits 23 through 18
    3. Replace it with the remainder of itself divided by 64
    4. Set the middle 12 bits
  3. Write an assembly language program, using a C library, that writes Unicode characters 32 through 126 to standard output, 16 characters per line.
  4. Write an assembly language program that displays its command line arguments in reverse order, one per line, to standard output.
  5. Write an assembly language function, in its own file, that computes the GCD of its two input arguments. Assume the arguments are unsigned numbers that have been pushed on the stack. Return the result in EAX. Use Euclid's algorithm, which says that
        gcd(x, y) = (y == 0) ? x : gcd(y, x mod y)
    
  6. Write an assembly language program that calls your gcd function from the previous problem with its command line arguments. It is up to you to make the program behave sensibly when presented with missing or garbage arguments.
  7. Write a C program that calls your gcd function in the previous problem with its command line arguments. It is up to you to make the program behave sensibly when presented with missing or garbage arguments.
  8. Write two versions of an assembly language function, callable from C, that takes (a pointer to) an array of three (signed) integers, and returns the median of those three values. One version of the function is to be written with absolutely no conditional jump instructions, while the other one must use them. Place both functions in the same asm file.
  9. Write in assembly language the function
        double log_base_7(double x);
    
    which returns the log in base 7 of its input argument.