Assignment 4, due Jun 21

Solutions

Part of the homework for CS:2630, Summer 2018
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

On every assignment, write your name legibly as it appears on your University ID card! Homework is due on paper at the start of class on the day indicated (Tuesday or Thursday). Exceptions will be made only by advance arrangement (excepting "acts of God"). Late work must be turned in to the TA's mailbox (ask the CS receptionist in 14 MLH for help). Never push homework under someone's door!

  1. Background: Look at the Hawk program skeleton distributed with Machine Problem 1.

    a) The program uses R8 to store the variable n. The choice of R8 was somewhat arbitrary — that is, the program would have worked just as well if any of several other registers had been used, but there are others that cannot be used to hold n. Which registers cannot be used? (Hint: Read Chapter 5, or experiment and see what happens) (0.5 points)

    By convention, Hawk monitor routines may wipe out R3 to R7, and R1 and R2 are used in calls to monitor routines. So, none of R1 to R7 are available for long-term storage of values needed by the main program.

    b) The program skeleton contains a loop. That loop ends with a CMPI instruction followed by a BLE instruction. What change to the CMPI instruction would you need to make if you changed the BLE to BLT? (0.5 points)

    Make the loop end as follows:

            CMPI    R8,20
            BLT     LOOP
    

    That is, we replaced (n <= 19) with (n < 20).

  2. Background: About 3/5 of the way through Chapter 5, three different solutions are given to the problem of loading a register with the address of the string HELLO in the hello world program.

    a) Which of these use PC-relative addressing? (0.5 points)

    The right most (using a LOAD) definitely does. and the middle one (using a LEA) arguably does, since it computes an effective address by adding a constant to the PC.

    b) Why is the ALIGN directive needed in the rightmost alternative? (0.5 points)

    Because the instructions of the program above it occupy a mix of words and halfwords and we have no idea whether the location counter is divisible by 4. Words on the Hawk (such as the value at PHELLO) need to be aligned, which is to say, the locaiton counter must be divisible by 4 before they are assembled into memory.

    c) In the discussion of PC-relative addressing, the expression X-(.+4) shows up as the displacement for the LEA instruction. Where did the +4 come from. That is, why isn't the displacement just X-.? (0.5 points)

    The assembler computes the operand values before assembling them into memory, so the expression X-(.+4) is evaluate first and then the results are formed into bytes and words in memory. The CPU increments the program counter as it fetches each halfword of the instruction, and only later does it do arithmetic. So, between the time the LOAD instruction is fetched and the time the effective address is computed, the program counter will have been incremented by 4 from the value the location counter had when the expression was evaluated by the assembler.

  3. Background: The Hawk offers two ways to do an unconditional control transfer, the BR instruction, that can go to any instruction within a 256 half-word window centered on the instruction itself, and JUMP, that can go to any instruction within a 64K byte range (32K half words).

    In contrast, the only instructions on the Hawk that test the condition codes are branch instructions. There are no conditional variants of the JUMP instruction. This is justified by the fact that most conditional control structures in real programs are small loops.

    Suppose your program had BGT THERE in it, and it used to work until you added a few more lines of code, and the next time you assembled the program you got an "out of bounds" error message because THERE was too far from the conditional branch instruction.

    A question: What code could you use to replace BGT THERE that would accomplish the same behavior. Hint: It is more than 1 instruction and one of these instructions is a JUMP instruction. (0.5 points)

            BLE     NOTTHERE
            JUMP    THERE
    NOTTHERE: