Assignment 4, due Sep 19

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

  1. A Problem: Consider this SMAL code, and compare it with the code given in the assignment for MP1; you may need to poke into sections 2.2 and 2.6, and chapters 5 and 6 of the SMAL manual to get a handle on it how this works:
            TITLE   "mp1.a by Douglas W. Jones, Sept. 12, 2014"
    ; -- yet another solution to MP1 using a clever macro
                                            ;  3
            MACRO RECORD =x,=y,text         ;  4
              IF LEN(text) > 0              ;  5 (an error was here, now fixed)
                W x, y, TEXT                ;  6
                LCSAVE = .                  ;  7
                . = TEXT                    ;  8
                ASCII text,0                ;  9
                TEXT = .                    ; 10
                . = LCSAVE                  ; 11
              ELSE                          ; 12
                W 0, 0, 0                   ; 13
              ENDIF                         ; 14
            ENDMAC                          ; 15
                                            ; 16
            INT     ARRAY                   ; 17
    ARRAY:  RECORD  1,1,"Hello"             ; 18
            RECORD  1,2,"World"             ; 19
            RECORD  1,3,"-----"             ; 20
            RECORD                          ; 21
    TEXT    =       .                       ; 22
            END                             ; 23
    

    a) Does this code load anything in memory that differs from the data loaded by the version given in the assignment for MP1? If so, what? (0.3 points)

    The data is the same. Only the way it was stored in memory has changed.

    b) In the original code from the assignment for MP1, the programmer had to explicitly include a null terminator on every string. Here, the programmer just typed RECORD 1,1,"Hello" with no following ,0. What part of this code puts the null terminator on the string? (You can refer to parts of the code by the line numbers provided in the comments.) (0.3 points)

    Line 9 in the macro body puts the null at the end of string.

    c) How does this code keep the text strings separated from the array of records? In the original code, all the records were given first and then the text strings were all given later. Here, they seem to be mingled. (0.4 points)

    The macro uses the symbol TEXT (initialized on line 22) as a location counter for placing the text of all the strings after all of the records.

  2. Background: The mp1test program operated by calling the PUTAT and PUTS routines in the Hawk monitor. Here is some code that could be fixed to serve this purpose:
            TITLE   "mp1test.a by Douglas Jones, broken on Sept. 12, 2014"
            USE     "hawk.h"
            USE     "monitor.h"
    ARSIZE  =       4
            INT     MAIN
            S       MAIN
    MAIN:
            STORES  R1,R2
            ADDI    R2,R2,ARSIZE
    ; ------- start application code
            EXT     ARRAY
            LIL     R8,ARRAY        ; R8 points to an array entry
    LOOP:                           ; do {
            LOADS   R3,R8           ;   load R3 with the X coordinate
            ADDI    R9,R8,4         ;   make R9 point to the Y coordinate ***
            LOADS   R4,R9           ;   load R4 with the Y coordinate
            LIL     R1,PUTAT
            JSRS    R1,R1           ;   putat( X, Y );
            ----    -------         ;   make R9 point to the text pointer
            LOADS   R3,R9           ;   load R3 with the text pointer
            CMPI    R3,0
            BEQ     DONE            ;   if (text == NULL) break
            LIL     R1,PUTS
            JSRS    R1,R1           ;   puts( text );
            ADDI    R8,R8,12        ;   make R8 point to the next array entry
            BR      LOOP            ; } until break
    DONE:
    ; ------- end application code
            ADDI    R2,R2,-ARSIZE
            LOADS   R1,R2
            JUMPS   R1
            END
    

    a) An instruction is missing, it has been replaced with ---- -------. What is it (give the opcode and operands in SMAL Hawk format). (0.5 points)

    Here is a fairly obvious solution:

            ADDI    R9,R8,8         ;   make R9 point to the text pointer
    

    But since this is a prgramming problem, there are other solutions; consider the following, which uses the fact that, at this point in the code, R9 points to the word just before the one we want:

            ADSI    R9,4            ;   make R9 point to the text pointer
    

    b) The instruction on the line marked with *** uses an ADDI to add 4 to the pointer in R8. Why not use an ADDSI instead? (0.5 points)

    The instruction ADDI R9,R8,4 adds a small constant to R8 and also puts the result in R9; an ADDSI cannot do this. Another way of saying this is that if we wanted to use an ADDSI we would also have to use a MOVE instruction. We could substitute the following code for the line marked with ***:

            MOVE    R9,R8
            ADDSI   R9,4
    

    c) List all of the instructions in the above code that have 32-bit (long) formats. You don't need to list multiple occurances of each, so if the instruction GREPL is used twice, just say GREPL, don't mention the operands. (0.5 points)

    LIL, ADDI, and CMPI are long format. If somene left out CMPI from this list, they could get full credit if they added a note saying that ADDI and CMPI are really the same instruction.

    d) If this program used the opcode BZS instead of BEQ, would the result be any different? Why? (0.5 points)

    No difference (BEQ is just an alias for BZS).