Assignment 4, Solutions

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

  1. Background: Consider the following SMAL Hawk assembly language program:
            INT     MAIN
            S       MAIN
    MAIN:   H       #01D1
            H       #02D2
            H       #1233
            H       #F4E4
            H       #FFFF
            H       #64F4
            H       #0010
            H       #FF00
            END
    

    This program consists entirely of Hawk machine instructions, but they have been coded in hex instead of using the full range of tools provided by the SMAL assembler. All but one of these instructions are documented in Chapter 4 of the notes (one is from Chapter 5). Appendix B of the Hawk manual lists the binary code for all of the instructions. If you run this program, note that the Hawk emulator includes a disassembler.

    a) Translate the H directives in the above program into symbolic assembly language. That is, replace, for example, H #F3F2 with MOVE R2,R3. What you are doing for this assignment is called disassembling the since you are undoing what the assembler normally does for you. (1.0 points)

    Here is how the HAWK disassembles this code in its memory display;

    001000: LIS     R1,#01 
    001002: LIS     R2,#02
    001004: ADD     R3,R1,R2   
    001006: LIL     R4,#FFFFF4
    00100A: LEACC   R4,R4,#0010
    00100E: BR      #00100E   
    

    Here is what might be a more appropriate version of the code:

            INT     MAIN
            S       MAIN
    MAIN:
            LIS     R1,#01 
            LIS     R2,#02
            ADD     R3,R1,R2   
            LIL     R4,-12
            ADDI    R4,R4,16
    LOOP:   BR      LOOP
    
            END
    

    b) What does the program do? That is, what values does it leave in what registers when it executes? (0.5 points)

    R1 = 1
    R2 = 2
    R3 = 3 (that is, R1 + R2)
    R4 = 4 (that is, 12 + 16)
    

  2. Background: Consider this broken fragment of assembly code.
            LIS     R3,4000
            ADDSI   R3,12
            ADDI    R3,1000000
    

    If you assemble it, you will get several error messages. Note, however, that the intent of this code is clear, it is trying to load 4000 (decimal) into R3, then increment that register by twelve and then increment it by a million.

    a) Give the sequence of halfwords that the assembler should place in memory to represent the above instructions, using hexadecimal, with question marks substituted for the hex digits that correspond to the erroneous parts. (0.5 points)

    Here is what the SMAL assembler does to the code, with -1 (that is, FFFFFFFF) substituted into the code for the problem values

    +00000000: D3  FF                2          LIS     R3,-1
    +00000002: 13  CF                3          ADDSI   R3,-1
    +00000004: F3  63  FFFF          4          ADDI    R3,-1
    

    Notice that ADDI R3,-1 is really ADDI R3,R3,-1 because the assembler is forgiving about this programming error.

    Here it is, with the bytes combined into halfwords, the halfwords arranged in a single column, and question marks where the FFFFFFFF values were:

    ??D3
    C?13
    63F3
    ????
    

    b) Replace each of the instructions above with an appropriate instruction (or instruction sequence) that does the intended operation, eliminating the error. If you need an extra register, use R4. (1.0 points)

            LIL     R3,4000
            ADDI    R3,R3,12
            LIL     R4,1000000
            ADD     R3,R3,R4