org $3000 START * Test main; it's NOT part of the solution: * 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 a long from the user move.l d1,-(sp) jsr _count_ones adda.w #4,sp ; restore stack andi.l #$FFFF,d0 ; clear upper word move.l d0,d1 ; and print count print bra _main ******************************************************************************* * Begin SOLUTION to midterm practice #2 ******************************************************************************* _count_ones: link a6,#0 movem.l d1-d2,-(sp) move.l 8(a6),d2 ; v clr.w d0 ; count = 0 move.w #31,d1 ; initialize loop counter loop btst #0,d2 beq skip addi.w #1,d0 ; the Count counts 1 (mwa ha ha) skip lsr.l #1,d2 dbf d1,loop movem.l (sp)+,d1-d2 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 * o 1/4 credit for an essentially correct function entry and exit (with or * without link/unlk, as long as it's correct) * o 1/4 credit for an essentially correct loading of the argument and using d0 * for the return value * Many other correct implementations are of course possible. end START