CS 273 HW 4 Solutions

I tried a little experiment with these solutinos, embedding Scalable Vector Graphics figures. It didn't work well, since it was necessary to download plugins to see the figuers and it still wasn't reliable. So, I've gone back to having GIFs in the answers. If you'd like to see the SVG version, look here. The figures in this version are all clickable; if you click on one, you'll get a PDF version.

From the Book

4-9
Ouch. This doesn't turn out to have been a very good first structured programming question, since the given code could quite easily have been generated by a structured high level langauge program; also, since the two exit tests occur without any intervening code you have to be pretty pedantic to insist that they're really two exit locations. It's also really unfortunate to come up with a problem in which improving coding style makes the program more complicated! All the same, here's a way to replace the given code with a truly single exit location.

  1. figure for 4-9(a)
  2. It sees how many elements are in a table named TABLE before the first 0-element. The length of the table is LENGTH; if there are no 0-elements it reports the total number of elements in the table. Except for the limit on the number of elements, and the terminology, this is the standard C library strlen() function.

  3. figure for 4-9(c)
  4. In addition to the requested changes, I also modified it so it would run in our environment, and cleaned up the table definition slightly.
    RAM     equ   $0000
    EEPROM  equ   $f800
    
            org   RAM
    loopcnt rmb   1
    nzcnt   rmb   1
    
            org   EEPROM
    table   fcb   $0c, $a3, $00, $f0
    tabend
    length  fcb   tabend-table
    
    start   clr   nzcnt
            clrb
            ldx   #table
            ldaa  length
            staa  loopcnt
            
    again   tstb
            bne   last
    
            tst   loopcnt
            bne   notend
            incb
            bra   again
    
    notend  tst   0,x
            bne   notzer
            incb
            bra   again
    
    notzer  inc   nzcnt
            inx
            dec   loopcnt
            bra   again
    
    last    bra   last
            
            end   start
    			
    			

4-11

figure for 4-11

Not From the Book


figure for \

RAM     equ   $0000
EEPROM  equ   $f800
N       equ   10
        
        org   RAM
i       rmb   1
j       rmb   1
tmp     rmb   1
awry    rmb   N


        org   EEPROM
inarr   fcb   9, 12, $ff, 13, 0, -37, %01010101, N, 12, tmp
        
main    
* First, copy input to RAM
        ldx   #inarr      * x = &inarr
        ldy   #0          * y = &awry
        ldaa  #N          * a = N
*                         * do {
cloop   ldab  0,x         *     b = *x
        stab  awry,y      *     awry[y] = b
        inx               *     x++
        iny               *     y++
        deca              *     a--
        bne   cloop       * while (a != 0)

* Now we start the sorting.  This is the part that comes from the
* assignment
        clr   i           * i = 0
oloop   ldaa  i           * while...
        cmpa  #N          *      ...(i < N)...
        bge   odone       *                ...{

        clr   j           *     j = 0
        ldx   #awry       *     (x = &awry)
iloop   ldaa  j           *     while...
        cmpa  #N-1        *          ...(j < N-1)...
        bge   idone       *                      ...{

        ldaa  0,x         *             if (awry[j] >...
        cmpa  1,x         *                          ...awry[j+1])...
        ble   noswap      *                                       ...{
        staa  tmp         *                 tmp = awry[j]
        ldaa  1,x         *                 awry[j] =...
        staa  0,x         *                          ...awry[j+1]
        ldaa  tmp         *                 awry[j+1] =...
        staa  1,x         *                            ...tmp
*                         *             }

noswap  inc   j           *             j = j + 1
        inx               *             (ix = ix+1)
        bra   iloop       *     }

idone   inc   i           *     i = i + 1
        bra   oloop       * }

odone   bra   odone

        end   main
        


Last modified: Fri Oct 28 14:50:05 MDT 2005