LMU ☀️ CMSI 2210
COMPUTER SYSTEMS ORGANIZATION
HOMEWORK #5 Due: 2018-04-30
  1. Write a 64-bit assembly language program, using a C library, that writes Unicode characters U+0020 through U+007E to standard output, 16 characters per line. You may assume that standard output uses UTF-8.
  2. Write a 64-bit assembly language function, in its own file, that computes the GCD of its two input arguments. Assume the arguments are unsigned numbers. Use Euclid's algorithm, which says that
    gcd(x, y) = (y == 0) ? x : gcd(y, x mod y)
    

    Your function should be callable from C as in this example:

    #include <stdio.h>
    #include <stdint.h>
    #include <assert.h>
    
    uint64_t gcd(uint64_t x, uint64_t y);
    
    int main() {
        assert(gcd(309,66) == 3);
        assert(gcd(66, 309) == 3);
        assert(gcd(3113041662, 11570925) == 462837);
        assert(gcd(427366239731, 4273650023214) == 6499);
        assert(gcd(427366239731687, 4268765973650023214) == 1);
        puts("All tests passed");
        return 0;
    }
    
  3. Write an assembly language function that reverses the byte order of a 4-byte integer, for example 0x3d744b26 would be turned into 0x264b743d. The function should accept a pointer to the integer to be converted. Here is a sample calling program in C:
    #include <stdio.h>
    #include <stdint.h>
    #include <assert.h>
    
    void byteswap(uint32_t *x);
    
    int main() {
        uint32_t x = 0x3d744b26;
        byteswap(&x);
        assert(x == 0x264b743d);
        puts("All tests passed");
        return 0;
    }
    
  4. Write a 64-bit assembly language function to return the number of 1-bits in its sole argument, a 32-bit integer. Use the algorithm given in this Stack Overflow answer. Here is your test program:
    #include <stdio.h>
    #include <stdint.h>
    #include <assert.h>
    
    uint32_t onebits(int32_t x);
    
    int main() {
        assert(onebits(0) == 0);
        assert(onebits(-1) == 32);
        assert(onebits(0x264b743d) == 16);
        assert(onebits(0x12345678) == 13);
        puts("All tests passed");
        return 0;
    }
    
  5. Write a 64-bit assembly language function of three arguments: the first is a 64-bit floating point value $x$, the second is a 64-bit floating point value $y$, and the third is an 8-bit signed integer $b$; the function returns the 64-bit floating point value $(x-y)^b$. You can test with:
    #include <stdio.h>
    #include <stdint.h>
    #include <assert.h>
    
    double power_of_difference(double x, double y, int32_t b);
    
    int main() {
        assert(power_of_difference(5, 5, 20) == 0);
        assert(power_of_difference(50, 45, 10) == 9765625);
        assert(power_of_difference(206, 204, 20) == 1048576);
        assert(power_of_difference(206, 204, -3) == 0.125);
        assert(power_of_difference(-30, -26, -4) == 0.00390625);
        assert(power_of_difference(16.5, 15, 3) == 3.375);
        puts("All tests passed");
        return 0;
    }