Assignment 2, Solutions

Part of the homework for 22C:60 (CS:2630), Fall 2011
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

  1. Background: Consider the following randomly selected bit patterns:
    100110 010011 000011 001001 011011

    a) Give the decimal value of each of these interpreted as an unsigned binary number. (0.5 points)

    100110 = 2 + 4 + 32 = 38
    010011 = 1 + 2 + 16 = 19
    000011 = 1 + 2 = 3
    001001 = 1 + 8 = 9
    011011 = 1 + 2 + 8 + 16 = 27

    b) Give the decimal value of each of these interpreted as 2's complement binary number. (0.5 points)

    Note that only the first number is negative. All the others are positive, and positive integers in the 2's complement system have the same representation as the corresponding unsigned binary numbers. Therefore, only the first answer that follows is different.

    100110 = -(011001 + 1) = -((1 + 8 + 16) + 1) = -26
    010011 = 1 + 2 + 16 = 19
    000011 = 1 + 2 = 3
    001001 = 1 + 8 = 9
    011011 = 1 + 2 + 8 + 16 = 27

    There is an alternative way of solving this. The sign bit of a 2's complement number has the same magnitude as that bit in an unsigned binary number, but it is negated. In 6-bit numbers, the sign bit has the weight -32, so:

    100110 = 2 + 4 + -32 = -26

    c) Give the decimal value of each of these interpreted as 1's complement binary number. (0.5 points)

    100110 = -(011001) = -(1 + 8 + 16) = -25
    010011 = 1 + 2 + 16 = 19
    000011 = 1 + 2 = 3
    001001 = 1 + 8 = 9
    011011 = 1 + 2 + 8 + 16 = 27

  2. Background: Consider the following SMAL code:
    .       =       0
            W       0
    A       =       .
            B       1,0,0,0
    .       =       12
            H       B - 5, 0
    .       =       8
    B:      W       2
    .       =       . + 4
            W       A
    

    a) Show the assembly listing for this code. You may do it by hand or use the Hawk assembler. You will learn different useful things either way. Do it by hand first if you opt to do it both ways. (1.0 points)

                                     1  .       =       0
     00000000: 00000000              2          W       0
                                     3  A       =       .
     00000004: 01  00  00  00        4          B       1,0,0,0
                                     5  .       =       12
     0000000C: 0003  0000            6          H       B - 5, 0
                                     7  .       =       8
     00000008: 00000002              8  B:      W       2
                                     9  .       =       . + 4
     00000010: 00000004             10          W       A
    

    The solution shown above was lifted from the assembly listing produced by the SMAL assembler. Hand-done solutions would most likely have a less mechanically perfect layout and perhaps no leading zeros or fewer leading zeros on addresses.

    b) Show the contents of memory this produces, show the result as 32-bit words, their addresses in ascending order and with both addresses and values expressed in hexadecimal. (0.5 points)
            00: 00000000
            04: 00000001
            08: 00000002
            0C: 00000003
            10: 00000004
    

    The SMAL code above was crafted fairly carefully to produce this very non-random looking result. The solution presented here was cut and pasted from the Hawk emulator's display of the contents of memory after loading the result from assembling the above code.