TITLE "MP3 by Douglas Jones" ; a program to output Pascal's triangle ; uses a recursive computation of binom(n, k), not optimized much USE "hawk.h" USE "monitor.h" MAXROWS = 15 ; the maximum number of rows allowed in the triangle SUBTITLE "MAIN program prints the triangle" ; main program activation record ;RETAD = 0 ARSIZE = 4 ; main program code S MAIN INT MAIN MAIN: ; expects R3 = screen width ; R4 = screen height ; uses R8 = x coordinate of start of row of triangle ; R9 = y coordinate of start of row of triangle ; R10 = ymax maximum coordinate that will fit on screen ; R11 = item number in row STORES R1,R2 ; -- save RA ADDI R2,R2,ARSIZE ; -- push my AR MOVE R8,R3 SR R8,1 ADDSI R8,-3 ; x = width/2 - 3 -- so topmost 1 is centered LIS R9,0 ; y = 0 -- start at top row ADDI R10,R4,-1 ; maxy = height - 1 CMPI R10,MAXROWS BLTU MENDIF ; if (maxy >= maxrows) { LIS R10,MAXROWS - 1 ; maxy = MAXROWS - 1 MENDIF: ; } MLOOP1: ; do { -- for each row CMP R9,R10 BGTU MENDLP1 ; if (y > maxy) break; MOVE R3,R8 ; -- parameter x MOVE R4,R9 ; -- parameter y LIL R1,PUTAT JSRS R1,R1 ; putat(x, y) LIS R11,0 ; item = 0 -- start at left item MLOOP3: ; do { -- for each item in the row MOVE R3,R9 ; -- parameter n = y MOVE R4,R11 ; -- parameter k = item JSR R1,BINOM ; temp = binom( n, k ) ; -- parameter temp already in r3 LIS R4,4 ; -- parameter field width LIL R1,PUTDEC JSRS R1,R1 ; putdec(temp, 4) ADDSI R11,1 ; item = item + 1 CMP R11,R9 BLEU MLOOP3 ; } until (item > row) ADDSI R8,-2 ; x = x - 2 ADDSI R9,1 ; y = y + 1 BR MLOOP1 MENDLP1: ADDI R2,R2,-ARSIZE ; -- pop my AR LOADS R1,R2 ; -- recover RA JUMPS R1 SUBTITLE "BINOM computes binomial coefficients" ; binom activation record ;RETAD = 0 PARAMN = 4 PARAMK = 8 RESULT = 12 ARSIZE = 16 ; binom code BINOM: ; expects R3 = n ; R4 = k STORES R1,R2 ; -- save RA STORE R3,R2,PARAMN STORE R4,R2,PARAMK ; -- save params CMP R3,R4 BNE BELSE1 ; if (n == k) { -- basis case LIS R3,1 LOADS R1,R2 ; -- recover RA JUMPS R1 ; return 1 BELSE1: ; } TESTR R4 BZR BELSE2 ; if (k == 0) { -- basis case LIS R3,1 LOADS R1,R2 ; -- recover RA JUMPS R1 ; return 1 BELSE2: ; } ; -- the general case follows LOAD R3,R2,PARAMN ADDSI R3,-1 ; -- param n-1 LOAD R4,R2,PARAMK ; -- param k ADDI R2,R2,ARSIZE JSR R1,BINOM ; result = binom( n-1, k ) ADDI R2,R2,-ARSIZE STORE R3,R2,RESULT ; -- save result LOAD R3,R2,PARAMN ADDSI R3,-1 ; -- param n-1 LOAD R4,R2,PARAMK ADDSI R4,-1 ; -- param k-1 ADDI R2,R2,ARSIZE JSR R1,BINOM ; result1 = binom( n-1, k-1 ) ADDI R2,R2,-ARSIZE LOAD R4,R2,RESULT ; -- get first result ADD R3,R3,R4 ; result1 = result + result1 LOADS R1,R2 ; -- recover RA JUMPS R1 ; return result1 END