Machine Problem 4, due Jul. 15

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

A function

The hawk monitor contains PUTDEC and PUTDECU routines that directly output decimal numbers, and it contains PUTHEX to output numbers in base 16. There is no support for other number bases, nor is there a conversion routine to convert a number to string format so that it can be manipulated with string-processing routines.

Consider the following interface specification:

INTTOS: ; expects R3 = buf  -- address of buffer to use for conversion
        ;         R4 = len  -- length of buffer
        ;         R5 = num  -- unsigned number to convert to string
        ;         R6 = base -- number base to use (from 2 to 32)
        ; returns R3 = str  -- pointer to null terminated string holding result
        ;         note that str points somewhere between buf and buf+len-1
        ;         except when there is an error.

The digits used for this conversion should be those from Crockford's base 32. As a result, the letters I, L and O will never be used because of the fact that they are easy to confuse with the numerals 1 (upper case I, lower case L) and 0 (upper case O). This means that your INTTOS routine will give decimal and hexadecimal results with their normal digits, while avoiding easily misread results for number bases above 18.

The routine does not prefix the string with any leading spaces.

Error conditions: All errors return a pointer to a constant null terminated string. If the number has too many digits in the indicated number base to fit in the buffer, the returned string should be a pointer to the string "-". If the base is less than 2 or greater than 32, the function should return the constant null-terminated string "?".

An assignment

Write code for the INTTOS function, and test it with a main program that prints the Fibonacci series with each member of the series printed in a successively larger number bases starting from base 1 and stopping when the base reaches 33. Separate successive numbers with a space.

Your output should therefore begin like this:

? 1 1 2 3 5 11 15 23 34 50 75 B1 129

The initial question mark is because the initial base of 1 is too small. The output 1 1 2 3 5 is the normal Fibonacci series because the base is larger than the number being printed. The next number, 15 is printed in base 8, and the values get successively stranger after that point.

Submit your work in the mp4 directory in the CS2630 submit directory. As usual, your work should be cleanly commented and easy to read.

Useful background

Note that there is no need for recursion in the subroutine because it can fill in the output string from right to left instead of left to right.

Note that you will need to use the DIVIDEU Hawk monitor routine to divide by the number base, returning both the remainder and quotient.

Conversion from integer digits to the corresponding character is best done by using the following 32-character string constant as a lookup table:

"0123456789ABCDEFGHJKMNPQRSTVWXYZ"