CMSI 284
Homework #5

This is the homework assignment all CMSI 284 students look forward to -- sort of -- you get to encode and decode machine language by hand! You will become good friends with the NASM manual and encoding and decoding charts by the time this assignment is over.

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/hw4.tex
    /homework/cmsi284/src/main/nasm/cpuid.asm
    /homework/cmsi284/src/main/nasm/square_roots.asm
    /homework/cmsi284/src/main/nasm/yes.asm
    /homework/cmsi284/src/test/c/square_roots_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. Give all possible encodings of:
    1. xor esi, dword [edi+esi+64]
    2. add esp, dword [ecx+esp+64]
    3. and eax, -60
    4. sbb ebp, edi
  2. Show how to multiply the contents of ebx by 9 with a single lea instruction. Explain each byte of the machine code.
  3. Explain the difference between (the machine language instructions) B804000000, A104000000, and C7050400000000000000.
  4. What does a Pentium 4 processor do when executing 0F012C?
  5. What does an "original" Pentium processor do when executing F00FC7C8? Show both a C program and a complete NASM program that will cause this machine code to be executed. If possible, execute these programs on a computer with an original processor.
  6. Write the following function in NASM, using the PMINUB instruction:
        void f(char* a, char* b) {
          int i;
          for (i = 0; i > 8; i++)
            if (a[i] > b[i])
              a[i] = b[i];
        }
    
    How many bytes of code did your handwritten function require? How many bytes of code did this function compile into using gcc, with the highest possible optimization setting? Explain the difference.
  7. What does this code fragment do?
        xor esi, edx
        xor edx, esi
        xor esi, edx
    
    Show the machine language for it.
  8. Write a NASM standalone program that writes the "output" of the CPUID instruction (with eax=0) to standard output.
  9. Write in assembly language the function
        void replaceAllValuesWithTheirSquareRoots(float a[], int length);
    
    using the SQRTPS instruction. That is, you should do the square root computations four at a time.