Assignment 1, Solutions

Part of the homework for 22C:169, Spring 2011
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

  1. Find a C compiler, any C or C++ compiler on any computer system. Veryfy that you can actually compile programs under it and that it produces an executable object file that you can find in some file system. Name that compiler and identify the computer system on which it works. (1 point)

    On my old (Power PC based) apple Mac:

    [pyrite:~] jones% echo $HOSTTYPE $MACHTYPE
    powermac powerpc
    [pyrite:~] jones% cc --version
    powerpc-apple-darwin8-gcc-4.0.0 (GCC) 4.0.0 (Apple Computer, Inc. build 5026)

    On the departmental (Intel based) Linux cluster:

    [jones@serv16 ~]$ echo $HOSTTYPE $MACHTYPE
    x86_64-linux x86_64
    [jones@serv16 ~]$ cc --version
    cc (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4)

  2. Compile and run the C Hello World program from lecture 2. Change the program so that it contains a comment giving your name and so that it outputs your name instead of the string Hello World (see the C style guidelines cited in lecture 2 for a suggested commenting convention). Turn in a legible printed copy of this modified program; it should be much shorter than a page. Note to C++ programmers: Please avoid using C++ comment conventions. (1 point)

    No solution offered.

  3. Modify the program so that it outputs, in hexadecimal, the address of your string instead of outputting the text of the string. To output the pointer p in hex using C, assuming that pointers are 24 bits (they aren't on any of our machines), use printf("%6x",p) (the 6 is the number of digits per pointer). Again, turn in a legible printed copy of this short program (it ought to fit on the same page as the first one). (1 point)
    #include <stdio.h>
     * Hello World program                                           *
     * Author:  Kernighan and Ritchie                                *
     * Revised by:  Douglas Jones                                    *
     *     Description: HW1 22C:169 - prints the address of string   *
    char hello[] = "Douglas W. Jones";     /* added in revision */
    int main()
        /* printf("Hello World!\n"); */    /* original */
        /* printf("%s\n", hello);    */    /* first revision */
           printf("%lx\n", hello);   */    /* first revision */
  4. Figure out how to modify the a.out object file! For example, replace all instances of your name in the object file with a nonsense string of the same length. The worst case solution to this problem would involve writing a little program that reads, modifies and writes the object file, but any of several common UNIX editors can be made to do this. Report on your methodology, giving details of how you did it. (1 point)

    Working with the original Hello World program, modified to print my name, I used the vi editor to edit a.out, where I used the command
      :/Douglas W. Jones/s//Arnold P. Hogget/
    before saving the file. When I executed it, the output was changed to Arnold P. Hogget, as expected. Note that the changed name is the same exact length as the original.

  5. Write a C or C++ function that prints out the address of the first local variable in its activation record, in hex, with a main program that demonstrates it. (1 point)
    #include <stdio.h>
     * Program to print out the address of one local variable        *
     * Author:  Douglas W. Jones                                     *
    void routine()
           int i;
           printf("%lx\n", &i);
    int main() {