Homework 5

22C:18, Spring 1996

Due Tuesday Feb. 20, 1996 in discussion

Douglas W. Jones

For this homework assignment, consider the following Pascal procedure, also shown in C for comparison:

 procedure zip( a: integer; var b: char ); (void) zip( a, b )
   var c: integer;                           int a;
       d: packed array[1..10] of char;       char * b;
   begin                                   {
     if a < 0                                int c;
       then c := -a;                         char d[11];
       else c := a;                          if (a < 0) {
     if (c > 0) and (c < 11) then begin        c = -a;
       d := 'abcdefghij';                    } else {
       d[c] := b;                              c = a;
       b := d[(c mod 10) + 1];               }
       writeln( d );                         if ((c>0) && (c<11)) {
     end;                                      strcpy( d, "abcdefghij" );
   end;                                        d[c-1] = *b;
                                               *b = d[(c mod 10)];
                                               puts( d );
                                             }
                                           }
In responding to the following questions, please confine yourself to "simple brute force" translation of Pascal (or C) to assembly code. Don't try to optimize! Don't try to make effective use of registers! Pass parameters on the stack, allocate local variables on the stack, and allocate global variables statically in memory. Assume that type integer is -32768..32767.
  1. Write appropriately commented M68000 code that is as close as you can come to being equivalent to the following small main program:
        program main( output );                char e;
        var e: char;                           
        begin                                  (void) main()
          e := 'x';                            {
          zip( 5, e );                           e = 'x';
          write( e );                            zip( 5, &e );
        end;                                     putc( e );
                                                 exit(0);
                                               }
    

  2. Write appropriately commented M68000 code for the procedure itself!

  3. The M68000 does not have a 32 bit multiply instruction, although later members of the family do have them. Write code to compute the product of two 32 bit numbers taken from longword locations A and B in memory, and store the 64 bit product in locations C (the high longword) and D (the low longword). Recall:
       X * Y = X.L*Y.L + B*X.H*Y.L + B*X.L*Y.H + B*B*X.H*Y.H