% Author unknown % Section 2.2.4, Program A, and Exercises 11, 13, 14, and 15 % Alpha-test version % % Each term has the three-octabyte form % % +---+---+---+---+---+---+---+---+ % | Coefficient (signed) | % +---+---+---+---+---+---+---+---+ % | A | B | C | D | Exponents (unsigned) % +---+---+---+---+---+---+---+---+ % | Link | % +---+---+---+---+---+---+---+---+ % % The terms have the form x^A y^B z^C w^D. Since we assume there is no % overflow of exponents, we can also think of the exponent as one % 64-bit field, two 32-bit fields, three 21-bit fields, eight 8-bit % fields, etc. Because the exponent octabyte does not have a sign bit % separate from the fields, the exponent zero marks the special node % at the end of every polynomial. This zero exponent corresponds to the % constant term, which each polynomial has (even though the coefficient % of this term may be zero). % A negative value is used for the null link. avail GREG 0 Next available memory node LOC #100 % ---------------------------- Program A ------------------------------ % Call with PUSHJ $R,Add, with polynomials in $(R+1) and $(R+2) % The polynomial pointed to by $(R+1) is added to $(R+2) Coef IS 0 Definition of coefficient field ABCD IS 8 Definition of ABCD exponent field Link IS 16 Definition of link field p IS $0 q IS $1 q1 IS $2 Add LDOU p,p,Link P <- Link(P) 1H SET q1,q Q1 <- Q LDOU q,q,Link Q <- Link(Q) 2H LDOU $3,p,ABCD ABCD(P) LDOU $4,q,ABCD ABCD(Q) CMPU $6,$3,$4 Compare ABCD(P) and ABCD(Q) BN $6,1B ABCD(P) < ABCD(Q) LDO $5,p,Coef Coef(P) BP $6,5F ABCD(P) > ABCD(Q) 3H LDO $6,q,Coef Coef(Q) ADD $6,$6,$5 Add coefficients STO $6,q,Coef Store new coefficient BZ $3,6F Is this the last term? PBNZ $6,Add If Coef(Q) = 0, go to A4 4H SET $4,q Q2 <- Q LDOU q,q,Link Q <- Link(Q) STOU q,q1,Link Link(Q1) <- Q STOU avail,$4,Link Avail <= Q2 SET avail,$4 LDOU p,p,Link P <- Link(P) JMP 2B Return to step A2 5H SET $6,avail Q2 <= Avail LDOU avail,avail,Link STO $5,$6,Coef Coef(Q2) <- Coef(P) STOU $3,$6,ABCD ABCD(Q2) <- ABCD(P) STOU q,$6,Link Link(Q2) <- Q STOU $6,q1,Link Link(Q1) <- Q2 SET q1,$6 Q1 <- Q2 LDOU p,p,Link P <- Link(P) JMP 2B Return to step A2 6H POP 0,0 Return from subroutine % ---------------------------- Program A ------------------------------ % --------------------------- Exercise 11 ----------------------------- % Call with PUSHJ $R,Copy, with the polynomial to copy in $(R+1) % The returned copy is pointed to by $R Copy GETA $1,9F LDOU $4,$0,ABCD 1H SET $2,avail LDOU avail,avail,Link STOU $2,$1,Link LDO $3,$0,Coef STO $3,$2,Coef STOU $4,$2,ABCD LDOU $0,$0,Link LDOU $4,$0,ABCD SET $1,$2 PBNZ $4,1B GETA $3,9F LDOU $4,$3,Link STOU $4,$1,Link SET $0,$4 POP 1,0 9H OCTA 0,0,0 % A slightly different Copy Copy2 SET $1,0 Set previous link to null 1H SET $3,avail $3 <= Avail LDOU avail,avail,Link BZ $1,3F If the first node, don"t set the STOU $3,$1,Link previous link JMP 4F 3H SET $2,$3 If the first node, save it for last 4H LDO $4,$0,Coef Copy the coefficient LDOU $5,$0,ABCD Copy the exponents STO $4,$3,Coef STOU $5,$3,ABCD LDOU $0,$0,Link Next term in P SET $1,$3 Remember previous link LDOU $5,$0,ABCD PBNZ $5,1B Loop 2H STOU $2,$3,Link Pointer to first node SET $0,$2 Return value POP 1,0 Return from subroutine % --------------------------- Exercise 11 ----------------------------- % --------------------------- Exercise 13 ----------------------------- % Call with PUSHJ $R,Erase, with the polynomial to erase in $(R+1) Erase LDOU $1,$0,Link Pnext <- Link(P) STOU avail,$0,Link Link(P) <- avail SET avail,$1 avail <- Pnext POP 0,0 Return % --------------------------- Exercise 13 ----------------------------- % --------------------------- Exercise 14 ----------------------------- % Call with PUSHJ $R,Zero % The returned zero polynomial is pointed to by $R Zero SET $0,avail $0 <= Avail LDOU avail,avail,Link STCO 0,$0,Coef Store zero as the coefficient STCO 0,$0,ABCD Store zeros as the exponents STOU $0,$0,Link Create circular link POP 1,0 Return % --------------------------- Exercise 14 ----------------------------- % --------------------------- Exercise 15 ----------------------------- % Call with PUSHJ $R,Mult, with polynomials $(R+1), $(R+2), and $(R+3) % The polynomial $(R+1) * $(R+3) is added to $(R+2) p IS $0 q IS $1 q1 IS $2 m IS $3 Mult SET m,$2 Get M 8H LDOU m,m,Link M <- Link(M) 1H LDOU p,p,Link P <- Link(P) 7H SET q1,q Q1 <- Q LDOU q,q,Link Q <- Link(Q) 2H LDOU $4,p,ABCD ABCD(P) LDOU $5,m,ABCD ABCD(M) ADDU $5,$4,$5 ABCD(P) + ABCD(M) LDOU $6,q,ABCD ABCD(Q) CMPU $7,$5,$6 Compare ABCD(P) + ABCD(M) and ABCD(Q) BN $7,7B ABCD(P) + ABCD(M) < ABCD(Q) LDO $8,p,Coef Coef(P) LDO $6,m,Coef Coef(M) MUL $6,$6,$8 Coef(P) * Coef(M) BP $7,5F ABCD(P) + ABCD(M) > ABCD(Q) 3H LDO $7,q,Coef Coef(Q) ADD $7,$7,$6 Add coefficients STO $7,q,Coef Store new coefficient BZ $4,6F Is this the last term in P? PBNZ $7,1B If Coef(Q) = 0, go to A4 4H SET $6,q Q2 <- Q LDOU q,q,Link Q <- Link(Q) STOU q,q1,Link Link(Q1) <- Q STOU avail,$6,Link Avail <= Q2 SET avail,$6 BZ $4,6F Is this the last term in P? LDOU p,p,Link P <- Link(P) JMP 2B Return to step A2 5H BZ $6,9F If zero, skip adding this term SET $7,avail Q2 <= Avail LDOU avail,avail,Link STO $6,$7,Coef Coef(Q2) <- Coef(P) * Coef(M) STOU $5,$7,ABCD ABCD(Q2) <- ABCD(P) + ABCD(M) STOU q,$7,Link Link(Q2) <- Q STOU $7,q1,Link Link(Q1) <- Q2 SET q1,$7 Q1 <- Q2 9H BZ $4,6F Is this the last term in P? LDOU p,p,Link P <- Link(P) JMP 2B Return to step A2 6H LDOU q,q,Link Go to the next term in Q LDOU $4,q,ABCD Have we reached the end? PBNZ $4,6B If not, continue LDOU $4,m,ABCD Is this the last term in M? PBNZ $4,8B If not, loop POP 0,0 Return from subroutine % --------------------------- Exercise 15 ----------------------------- % Testing and memory routines memsize GREG 300 Nodes of memory to allocate Nodsize IS 3 Node size in octabytes Main PUSHJ $2,InitMem Initialize memory GETA $3,PolyA Print polynomial A PUSHJ $2,Print GETA $3,PolyB Print polynomial B PUSHJ $2,Print GETA $3,PolyB Add polynomial B to polynomial A GETA $4,PolyA PUSHJ $2,Add GETA $3,PolyA Print polynomial A PUSHJ $2,Print GETA $4,PolyB Erase polynomial B PUSHJ $3,Erase PUSHJ $2,Zero Get new zero polynomial SET $4,$2 Print zero polynomial PUSHJ $3,Print SET $4,$2 Add zero polynomial to polynomial A GETA $5,PolyA PUSHJ $3,Add GETA $4,PolyA Print polynomial A PUSHJ $3,Print GETA $4,PolyC Print polynomial C PUSHJ $3,Print GETA $4,PolyA SET $5,$2 GETA $6,PolyC PUSHJ $3,Mult SET $4,$2 PUSHJ $3,Print SET $3,$2 Erase zero polynomial PUSHJ $2,Erase GETA $4,PolyA Make a copy of polynomial A PUSHJ $3,Copy SET $5,$3 Print it PUSHJ $4,Print GETA $4,PolyA And add it to polynomial A PUSHJ $2,Add GETA $3,PolyA Print polynomial A PUSHJ $2,Print PUSHJ $2,Zero Multiply 0 * A PUSHJ $4,Zero SET $5,$2 GETA $6,PolyA PUSHJ $3,Mult SET $4,$2 PUSHJ $3,Print PUSHJ $2,Zero Multiply A * 0 GETA $4,PolyA SET $5,$2 PUSHJ $6,Zero PUSHJ $3,Mult SET $4,$2 PUSHJ $3,Print GETA $3,PolyA Print polynomial A PUSHJ $2,Print GETA $3,PolyA Erase polynomial A PUSHJ $2,Erase PUSHJ $4,Zero Example multiplication from text GETA $3,Poly2 GETA $5,Poly1 PUSHJ $2,Mult PUSHJ $3,Print % Compute (x+1)^n for 1 <= n <= 64 GETA $2,PolyXP1 Get x + 1 and a copy of it in $2 and $3 SET $4,$2 PUSHJ $3,Copy SET $4,64 1H PUSHJ $5,Zero Set q <- 0 + pm SET $7,$3 p SET $8,$5 q SET $9,$2 m PUSHJ $6,Mult SET $7,$3 Print p PUSHJ $6,Print SET $7,$3 Erase old p PUSHJ $6,Erase SET $3,$5 Set p <- q SUB $4,$4,1 Repeat PBP $4,1B % Compute (x+1)^n for 1 <= n <= 64, n power of two GETA $2,PolyXP1 Get x + 1 and a copy of it in $2 and $3 SET $4,$2 PUSHJ $3,Copy SET $4,7 1H PUSHJ $5,Zero Set q <- 0 + pm SET $7,$3 p SET $8,$5 q SET $9,$2 m PUSHJ $6,Mult SET $7,$3 Print p PUSHJ $6,Print SET $7,$3 Erase old p PUSHJ $6,Erase SET $3,$5 Set p <- q SET $2,$5 Set m <- q SUB $4,$4,1 Repeat PBP $4,1B PUSHJ $2,MemTest TRAP 0,Halt,0 Stop % Call with PUSHJ $R,Print, with polynomial to print in $(R+1) Print GET $1,rJ Store return location LDOU $0,$0,Link Move to first term 4H LDO $3,$0,Coef Load coefficient PUSHJ $2,PriNum And print it 5H LDWU $3,$0,ABCD SET $4,"x" PUSHJ $2,2F LDWU $3,$0,ABCD+2 SET $4,"y" PUSHJ $2,2F LDWU $3,$0,ABCD+4 SET $4,"z" PUSHJ $2,2F LDWU $3,$0,ABCD+6 SET $4,"w" PUSHJ $2,2F GETA $255,3F Print newline SET $2,#a STBU $2,$255 TRAP 0,Fputs,StdOut LDOU $2,$0,ABCD Load exponents LDOU $0,$0,Link Move to next term PBNZ $2,4B Continue if exponents are not 0 GETA $255,3F Print newline SET $2,#a STBU $2,$255 TRAP 0,Fputs,StdOut PUT rJ,$1 Return POP 0,0 2H PBZ $0,4F GETA $255,3F STBU $1,$255 TRAP 0,Fputs,StdOut CMPU $2,$0,1 PBZ $2,4F GET $2,rJ SET $4,$0 PUSHJ $3,PriNum PUT rJ,$2 4H POP 0,0 3H BYTE 0,0,0,0 % Call with PUSHJ $R,PriNum, with signed octabyte to print in $(R+1) PriNum GETA $255,2F PBNN $0,1F NEG $0,$0 TRAP 0,Fputs,StdOut 1H LDA $255,$255,23 1H DIVU $0,$0,10 GET $2,rR ADDU $2,$2,"0" SUBU $255,$255,1 STBU $2,$255 PBNZ $0,1B TRAP 0,Fputs,StdOut POP 0,0 2H BYTE "-",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 PolyA OCTA 0 % x + y + z WYDE 0,0,0,0 OCTA @+8 OCTA 1 WYDE 1,0,0,0 OCTA @+8 OCTA 1 WYDE 0,