org $3000 START * Test main; it's NOT part of the solution, though it's worth understanding: * 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: suba.l #4,sp ; space to put rv pea (sp) ; arg 3 move.w #N,-(sp) ; arg 2 pea S ; arg 1 jsr _sum adda.w #10,sp ; restore stack (from pushing args) move.l (sp),d1 ; retrieve sum from stack adda.w #4,sp ; restore stack (from space for sum) tst.w d0 bne skip ; exit if overflow occurred print ; else print result skip move.l #9,d0 ; exit trap #15 ******************************************************************************* * Begin SOLUTION to midterm practice #1 ******************************************************************************* _sum: link a6,#0 movem.l d1/a0,-(sp) ; not necessary by conventions, but ok move.l 8(a6),a0 ; summands clr.l d0 ; accumulate sum move.w 12(a6),d1 ; n beq exit ; return 0 if n == 0 subi.w #1,d1 ; prepare for dbf loop add.l (a0)+,d0 bvs error dbf d1,loop move.l 14(a6),a0 ; sump move.l d0,(a0) ; store sum clr.w d0 ; no error bra exit error move.w #-1,d0 ; error exit movem.l (sp)+,d1/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, including check for * overflow * o 1/6 credit for an essentially correct function entry and exit * o 1/6 credit for an essentially correct loading of arguments * o 1/6 credit for an essentially correct storing of the sum * * Many correct implementations are of couse possible, inluding an * optimized one that does not use link/unlk or does not preserve d1 * and a0 (recall that our compiler conventions do not require that d0, * d1, a0, and a1 be preserved by functions). * Data for test main, not part of solution: S dc.l 93,-15,21,-1,55 S_end N equ (S_end-S)/4 end START