Homework 4

22C:18, Spring 1996

Due Tuesday Feb. 13, 1996, in discussion

Douglas W. Jones

Notice: Machine problems should be turned in as assembly listings, so that we can examine and comment on the way you wrote your code, and on diskette, so we can test your program. The MAS source code for MP1 should be in a file called MP1 in the diskette's top-level directory. In addition to the comment containing your name, the course number, your section, and the program title, you should include this information on the label on the diskette so it can be easily returned to you!

  1. Write appropriately commented Motorola 68000 code that is as close as you can come to being equivalent to the following small Pascal program:
        program spin( input, output );
            var t: 0..65535;
        begin
            write( "enter a positive number:" );
            readln( t );
            repeat
                writeln( t );
                if odd( t )
                  then t := (t * 3) + 1;
                  else t := t div 2;
            until t <= 1;
            writeln( t );
        end.
    
    In this exercise, don't worry about detecting overflow!

  2. Consider the statement t := (t * 3) + 1 from the above example. Assuming that t is represented by D0. this could be done with the following M68000 code:
            MULS.W  #3,D0
    	ADDQ.W	#1,D0
    
    Part A: Modify this code to detect overflow! That is, make it branch to code to print an error message and stop the program if the result of either the addition or the multiplication is greater than 65535.

    Part B: The multiply instruction on the M68000 chip is a complex instruction that takes a long time to compute its result, while the add and shift instructions (see ASL, for example) are quite fast. Rewrite the above instruction sequence, without attention to overflow, so that it uses a sequence of fast instructions instead of the slow MULS instruction.

  3. Translate the following Pascalish data structure declaration to appropriately commented M68000 assembly language:
    	var x: array [1..8] of record x,y: -2048 .. 2048 end;