Machine Problem 4, due at the end of Oct 16

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

Comment: There were so many really bad solutions submitted to MP3 that MP4 will be a repeat. Even solutions that ran correctly were pretty awful!

Top Level Specification: Your program should output the first 15 numbers in the tribonacci sequence, using a recursive function. This means that you will need to write a main program that calls your function 15 times.

That means that each solution must contain two subroutines:

Gratuitous recursion that merely recreates an iterative solution is not acceptable. Main programs that include special cases for the first few tribonacci numbers are not acceptable. Programs based on tables of pre-computed values are not acceptable.

In addition to outputting the values of the numbers, you should output the number of additions required by your recursive function to compute the numbers. Omit additions that are not part of the computing the tribonacci value. So, additions involved in loop control, stack manipulation etc are not involved.

Ouput the number of additions required for each number after each number, separated from that number by a colon. Commas should separate each result from the next. So, the sequence will begin:

0:0,0:0,1:0,1:2,2:4,4:8,7:16,13:30

Your solution must

The first three numbers are 0, 0, 1, and it takes no additions to compute these because they are part of the basis for the recursion. The 4th tribonacci number took 2 additions to add the three preceeding numbers.

Your program must be written in SMAL Hawk assembly language, and it it must be formatted so that it does not trigger warnings when you run

   [HawkID@fastx?? ~]$ ~dwjones/format mp4.a

Brief commentary: There is an example of recursive code to compute Fibonacci numbers in Chapter 6, and that example is clearly relevant, but it doesn't count how many additions it does, and it is missing a main program. If you start with that code, expect to make several significant changes!

There are at least two ways to count the additions. One is to make each function call return both a tribonacci number (perhaps in R3) and an addition count (perhaps in R4). The other is to use a global variable to count the additions. Zero that variable before each call to the tribonacci routine from the main program then output the value after the call.

Again, solving this problem in a higher level language like C is a good idea before trying to do it in assembly language.

Grading: 5 points. Correct output is worth 2.5 points. The remaining credit will be offered only for those who have reasonably correct output.

Code containing assembly errors will not earn any credit.

Stylistically clean code is important; that is why half the credit is reserved for style, but only if the code works. Bad indenting will be penalized up to 1 point from the 2.5 style points. Excessive or inadequate white space within and between lines will be penalized, again, up to 1 point. Excessive or inadequate comments will be judged similarly.

Note: Comments should not repeat the assignment. Assume that the reader of your code has read the assignment. Assume that the reader knows how to program. Using C code as commentary on assembly code is quite appropriate, so long as additional comments are added where the C code needs comments.

Do not put off commenting your code! If you ask for help with your code and it is not properly commented, you will be told to write good comments first! This is because the exercise of writing good comments frequently helps you find out what your code really does, and even when it does not, the comments tell us what you thought your code was supposed to do.

Control structures in your code that cannot be expressed in a readable high-level language style of commentary will be penalized. Comments that do not reflect the actual control structure of your code will be penalized.

Comments that explain previous versions of the code will be penalized. So if you lifted part of an old Fibonacci routine, be careful to edit your comments to reflect the new context.

If you base your code on examples from the notes, code distributed in previous course offerings, code distributed with lecture notes, or any source other than your own brain, say so! Cite your sources! Failure to cite sources will be penalized.

Submission: To submit your work, it must be in a file named mp4.a in your current directory on the CLAS Linux system, and you must know your section number. In the following, what you type is shown in bold face. Begin at the linux command-line prompt:

   [HawkID@fastx?? ~]$ ~dwjones/submit 0A0X mp4.a

Note: The ~dwjones must be verbatim, do not substitute another name. Also, use your section number (one of 0A01 to 0A05).

The submission script will copy your code and, after the copy is made, it will output a message saying "successful submission." You may resubmit as many times as you want; each time you resubmit, your previous submission of that file will be replaced, so we will only see your final submission. The completed dialogue on your screen will look like this when you are done:

In the event of insurmountable trouble, do not delete or edit files you have submitted until the graded work is returned. The operating system record of the last edit time and date and the backups maintained by the operating system are proof of what you did and when, and they allow us to investigate what happened. until your graded work is returned. This way, the time stamp marking the time of last edit is a trustworthy indicator of whether you did the work on time.


Discussion

Oct. 11, 5:15 PM

See all the discussion appended to the assignment for MP3.

Oct. 12, 3:43 PM

I see that the requirements are the same for mp4 as mp3. I got 5/5 on mp3, so I assume I can just submit the same file?

You may do so, but there is no guarantee that you'll get a 5/5 for your code. When I saw how bad many of the submissions for MP4 were, I told the TA to grade quickly even at the risk of carelessness in order to give quick feedback to everyone.

That means your code might contain things that on closer inspection, might not look so good. I recommend that you take the time to read your code and comments with a critical eye.

Oct. 16, 1:35 PM

Hi, I am trying to create a local variable, but when I use hawklink mp4.o it returns

5  value out of bounds       H RADDCOUNT
5  value out of bounds       H RADDCOUNT
5  value out of bounds       H RADDCOUNT

The code I am using is

COMMON ADDCOUNT,4
LCSAVE  =       .
.       =       ADDCOUNT
        W       0
.       =       LCSAVE

I found this code In the lecture 6 notes. Am I using it wrong?

You are declaring ADDCOUNT correctly, the problem is with how you are using it.

But a side note first: Your declaration (the COMMON statement) just declares ADDCOUNT to be the address of a 4-byte word-aligned block of RAM, the address of which will be assigned by the linker.

The code between the two uses of LCSAVE serves only to statically assign an initial value to ADDCOUNT, but that can't be relevant in your code because you need to zero ADDCOUNT before each call to your recursive tribonacci routine from your main program. Static initialization only does this once, not over and over again.

Now to explain the error message:

5  value out of bounds       H RADDCOUNT

The complaint is about an H directive in the object code. That is, some halfword from your code. The halfword was supposed to hold ADDCOUNT, but now that the code is being processed by the linker, ADDCOUNT is the address of some value in RAM. That is, a value between 1000016 and 1FFFF16. That value is out of bounds to be stored in a halfword, where the legal range of values is 000016 to FFFF16. So, the linker had no choice but to complain.

My guess about the cause is that you wrote something like this:

        LOAD    R3,??,ADDCOUNT

That cannot work. What you probably wanted to do was more like this:

        LIL     R1,ADDCOUNT
        LOADS   R3,R1

Compare this with the difference between how you call an external subroutine and an internal one:

        LIL     R1,PUTCHAR
        JSRS    R1,R1           ; call to an external subroutine

        JSR     R1,TRIBON       ; call to an internal subroutine

I hope that helps.