Homework 2


60 pts., due Oct. 3

For problems 2 and 3, use SPIM to test your program. Turn in well-commented program source and a sample run using script.

  1. Consider the following 68000 program:
             MOVEA.L     START,A0
             MOVEA.L     N,A1
             ADDA.L      A0,A1
             MOVEA.L     A0,A2
             MOVE.B      (A0)+,D0
    LOOP     CMP.B       (A0)+,D0
             BLE         NXT
             LEA         -1(A0),A2
             MOVE.B      (A2),D0
    NXT      CMPA.L      A0,A1
             BGT         LOOP
             MOVE.L      A2,DESIRED
    1. What does this program do? (Your answer must describe the program at a high level, such as ``It's C's strlen().'' Answers which state, ``It moves this and adds that,'' merely cause the instructor to pull out his hair and earn you no credit.)

    2. How many 16-bit words are needed to store this program in memory?

    3. If the data is stored in memory like this:

      how many memory accesses (counting both data and instruction accesses) are performed in executing the program? Note that the characters are stored in memory using an ASCII encoding.

      What is the value stored in DESIRED?

  2. Write an R2000 program that generates the first n numbers of the Fibonacci series. Prompt the user for the value of n. The first few values of the series are:

    All calculations should be done in registers.

    What is the largest value of n that your program can handle?

  3. Write an R2000 program to compare two strings input by the user. Prompt the user for each of the strings, echo the strings, and then let the user know whether the strings are equal, the first is ``less than'' the second, or the first is ``greater than'' the second.

    Use a subroutine which prompts the user to enter a string and then reads the string. This subroutine will be called twice by your main program and should take two parameters: the starting address of the array into which the string is placed, and the length of the array. Use registers $a0 and $a1 for passing the parameters.

    Use the jal instruction to call your subroutine. The return address will be stored in register $31. If you don't make any nested subroutine calls (there's no need to) and you don't modify $31 (you shouldn't), you can just leave the return address there. To return to the instruction following the subroutine call use this instruction:

                jr $31

    Here's a hint as to how to structure your program:

                .data    # Declarations, constants for main.
                .globl main
    main:       ...
                jal gets # Call subroutine.
                li $v0, 10
                .data    # Declarations, constants for gets.
    gets:       ...
                jr $31

Thomas P. Kelliher
Tue Sep 24 17:13:10 EDT 1996
Tom Kelliher