Assignment 5, 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: Assume, to begin with, that register 3 holds a pointer to an object of class month.

    Also assume that each month object is made up of three consecutive one-word fields, the length of the month in days during a normal year, the length of the month in days during a leap year, and a pointer to the name of the month.

    A problem: Write SMAL Hawk code (a fragment of a main program) to print out the name of the month (1.0 points).

            LOAD    R3,R3,8 ; the first byte of the third word
            LIL     R1,PUTS
            JSRS    R1,R1
    

    Several students tried to make this a very complex problem. It does not ask you to write a complete program, only a fragment. It does not ask you to place any data structures in memory, only to use a data structure that you are told that R3 already points to.

  2. Background: Consider this bit of SMAL code for two interlaced infinite loops:
    L1:     BR      L3
    L2:     JUMP    L4
    L3:     BR      L1
    L4:     JUMP    L2
    

    a) Show the equivalent machine-language code as a sequence of 16-bit halfwords. (0.5 points)

    In the following solution, an arbitrary assembly origin is used, but the sequence of values in memory does not depend on the origin. This solution was done by hand, looking up the details in the Hawk manual and then adding comments to help understand the branch displacements

    00: 0200 L1:     BR      L3   ; displacement = (6-2)/2 = 4/2 = 2
    02: 30F0 L2:     JUMP    L4 
    04: 0002                      ; displacement = 8-6 = 2
    06: FC00 L3:     BR      L1   ; displacement = (0-8)/2 = -8/2 = -4 = 11100
    08: 30F0 L4:     JUMP    L2
    0A: FFF6                      ; displacement = 2-C = -A = -01010 = 10110
    0C:
    

    Just assembling the code does not reduce it to a stream of halfwords. Instead, it is assembled to a stream of mixed bytes and halfwords. Nonetheless, this is a useful way to check the above:

    00000000: 00  02                4  L1:     BR      L3
    00000002: F0  30  0002          5  L2:     JUMP    L4
    00000006: 00  FC                6  L3:     BR      L1
    00000008: F0  30  FFF6          7  L4:     JUMP    L2
    

    b) Why does the answer to the above question not depend on the assembly origin? (0.5 points)

    Because all of the displacements, both 8 bit branch displacements and 16-bit jump displacements, are relative to the address of the instruction. In other words, they use PC-relative addressing.

  3. A question: Explain the relationship between the LEA, LEACC and ADDI instructions. (0.5 points)

    LEA r,x,c loads register r with the effective address of the operand pointed to by register x plus the displacement c. That is, it adds the sum of the value in register x plus the constant c.

    LEACC does the same thing and also sets the condition codes to report on the sum.

    ADDI is the same instruction as LEACC, but named to emphasize that it is like the ADD, instruction but adding a constant instead of the contents of some register.

  4. A Problem from the Notes: Do exercise e) from Chapter 6 in the notes. (0.5 points)

    The binary codes for LOAD, LOADS, STORE and STORES, lifted from the appendix to the Hawk manual:

    1 1 1 1  dst  0 1 0 1   x       LOAD    dst,x,disp
    1 1 1 1  dst  1 1 0 1   x       LOADS   dst,x
    1 1 1 1  srcx 0 0 1 0   x       STORE   srcx,x,disp
    1 1 1 1  srcx 1 0 1 0   x       STORES  srcx,x
                  ^
    
    So, the distinction between indexed and short-indexed instructions is in the high bit of the second byte of the instruction, marked with a caret (^). in the above table.