Self replicating program

A self replicating program is a kind of challenging concept in programming.  It has a long history.  This kind of program has been written for almost all programming languages.  It is given here as a form of brain-teaser for students to enjoy.

In an assembly language, self-replicating program is quite simple.  If the system is opened (no OS, no other form of prevention that a user can execute a program outside his/her user space), a self replicating program is just a program that copy machine code (its own program) into another memory location.

;; self
;;  movemem n words from &a to &b
;;  goto &b
;;  27 Aug 2003

.s
    ads_a 0
    ads_b 100
    n     11

.a 0
.c
;; let r1 = &a, r2 = &b, r3 = tmp, r4 = n

:mvmem
    ld r1 #ads_a
    ld r2 #ads_b
    ld r4 #n
:loop
    ld r3 @0 r1
    st @0 r2 r3
    add r1 r1 #1
    add r2 r2 #1
    sub r4 r4 #1
    jmp eq exit  ;; jump if zero
    jmp always loop
:exit
    trap stop r0
.e