                ORG     $0
                DC.L    $8000           Stack pointer value after a reset
                DC.L    START           Program counter value after a reset

START:          MOVE.L  #6,D0
                JSR     FAC_W           call FAC_W( 6)
                BREAK


********************************************************************************
*
* function:   D0 FAC_W( D0)
*
* description:
*   computes the factorial of the number given in D0 and returns
*   the result in D0 as well. Returns 0, iff an overflow (wrt. to word format)
*   is detected.
*
* registers affected: D0, D1
*

FAC_W:          MOVE.L  #1,D1           initialize the accumulator D1
                CMPI    #0,D0           if ( n eq 0)
                BEQ     DONE_W          return( D1)

LOOP_W:         MULU    D0,D1           multiply the accu with n
                BNE     OVERFLOW        result still in word format? (negativ)
                CMP.L   #$10000,D1
                BPL     OVERFLOW        result still in word format? (positiv)
                SUBI    #1,D0           decrement n
                BNE     LOOP_W          until ( n eq 0)
                
DONE_W:         MOVE.L  D1,D0           return the accu in D0
                RTS

OVERFLOW:       MOVE.L  #0,D0
                RTS
