org $3000 START * Test main; it's NOT part of the solution (though it's worth understanding): * macro for reading number read MACRO move.l #4,d0 ; read long from keyboard trap #15 ENDM * macro for printing (d1.l) as long print MACRO move.l #3,d0 ; print long trap #15 move.l #0,d0 ; print newline move.w #0,d1 trap #15 ENDM _main: read ; get l move.l d1,d2 read ; get u move.l d1,d3 read ; get s move.l d1,d4 suba.w #4,sp ; make room for rv move.l sp,-(sp) ; pointer to return value move.l d4,-(sp) ; push s move.l d3,-(sp) ; push u move.l d2,-(sp) ; push l jsr _asum adda.w #16,sp ; restore stack move.l (sp)+,d1 ; retrieve rv tst.w d0 ; error? bne fin print ; no, so print result fin rts ******************************************************************************* * Begin SOLUTION to midterm practice #6 ******************************************************************************* _asum: link a6,#0 movem.l d1-d4/a0,-(sp) move.l 8(a6),d1 ; l move.l 12(a6),d2 ; u move.l 16(a6),d3 ; s move.l 20(a6),a0 ; rv clr.l d4 ; accumulator move.w #-1,d0 ; set for error (may get changed) cmp.l d1,d2 blt exit ; l > u (error) loop add.l d1,d4 ; add element to partial sum bvs exit ; overflow add.l d3,d1 ; compute next element in sequence bvs exit ; overflow cmp.l d2,d1 ; d1 > d2? ble loop move.l d4,(a0) ; write result clr.w d0 ; no error exit movem.l (sp)+,d1-d4/a0 unlk a6 rts ******************************************************************************* * End SOLUTION ******************************************************************************* * Grading: * o full credit for an essentially correct implementation with a few * syntax errors * o 1/2 credit for an essentially correct main loop * -1/8 if forgot checks for overflow * o 1/4 credit for an essentially correct function entry and exit * o 1/4 credit for an essentially correct loading of the arguments and return * values * Many other correct implementations are of course possible. end START