org $3000 START * Test main; it's NOT part of the solution: * macro for printing string pstr MACRO lea \1,a1 move.l #13,d0 trap #15 ENDM _main: move.w #N,-(sp) pea DST pea SRC jsr _copy_string adda.w #10,sp tst.w d0 ; error? bne fin pstr DST ; no, so safe to print fin move.l #9,d0 ; exit trap #15 ******************************************************************************* * Begin SOLUTION to midterm practice #3 ******************************************************************************* _copy_string: link a6,#0 movem.l a0-a1,-(sp) move.l 8(a6),a0 ; source move.l 12(a6),a1 ; dest move.w 16(a6),d0 ; nbytes subi.w #1,d0 ; set up for dbf loop move.b (a0)+,(a1)+ ; copy a character dbeq d0,loop ; loop if the last character copied was not ; a null terminator and space remains cmp.w #-1,d0 ; encountered null terminator? beq exit ; if no, return -1 clr.w d0 ; yes, so return 0 exit movem.l (sp)+,a0-a1 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 arguments and using d0 * for the return value * Many other correct implementations are of course possible. In particular, * there are less optimized ways of implementing the main loop that are * perfectly acceptable. I just wanted to demonstrate a nontrivial use of DBcc. * Data for test main; not part of solution. SRC dc.b 'Hello Universe! I am here.',0 N equ 32 DST ds.b N end START