2 ;; BY RICHARD W.M. JONES
4 00001: ; Marks beginning of the cell.
6 JMPF ~00011 ; Jump to start of the cell code
9 00010: ; Marks start of local variables.
10 DW 4 ; Local variable storage.
12 ; This is the main cell program.
14 00011: ; Marks the start of the cell code.
16 ; When debugging, use this canard to detect stack underflows.
20 FINDB ~00010 ; Find beginning of local variables.
21 MOVE I,A ; Add length of label.
23 MOVE A,B ; Initialize base register.
24 FINDB ~00001 ; Find beginning address of cell.
26 STORE A,0 ; Save it for later.
27 FINDF ~01111 ; Find end address of cell.
29 ADD 5,A ; Add length of final label.
30 STORE A,1 ; Save it for later.
32 CALLF ~00100 ; Call subroutine to subtract
33 ; var 0 from var 1 and store in var 2.
35 LOAD 2,A ; Load the computed length.
36 MALLOC ; Allocate memory for the daughter.
38 JMPZB ~00011 ; If allocation failed, start again.
39 STORE A,3 ; Store the daughter's address in 3.
41 CALLF ~00110 ; Call subroutine to do the copy.
43 DIVIDE ; Break off the daughter cell.
45 JMPB ~00011 ; Repeat forever!
47 ; This subroutine subtracts var 0 from var 1 and stores the
48 ; resulting value in var 2.
51 LOAD 1,A ; Copy var 1 into var 2
53 LOAD 0,A ; A <- amount to subtract
54 00101: JMPZF ~01001 ; Loop until A == 0
56 LOAD 2,A ; Subtract 1 from var 2.
64 ; This subroutine copies var 2 bytes from [var 0] to [var 3] (ie.
65 ; from the mother to the daughter).
68 LOAD 2,A ; Number of bytes to copy.
69 00111: JMPZF ~01000 ; Leave loop when A == 0.
71 LOAD 0,A ; Get source address.
73 MOVE [I],A ; Get item to copy from source.
75 LOAD 3,A ; Get destination address.
78 MOVE A,[I] ; Store item at destination addr.
79 LOAD 0,A ; Increment source address.
82 LOAD 3,A ; Increment destination address.
90 01111: ; Marks end of the cell.