TITLE "MP3 by Douglas Jones" ; a program to output Pascal's triangle ; uses an array-based algorithm, some optimization USE "hawk.h" USE "monitor.h" MAXROWS = 15 ; the maximum number of rows allowed in the triangle S MAIN INT MAIN ; main program activation record ;RETAD = 0 ARRAY = 4 ARSIZE = 4 + (MAXROWS << 2) ; main program code 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) LEA R3,R2,ARRAY-ARSIZE MOVE R4,R9 ADDSL R4,R3,2 ; -- get address of array[y] LIS R5,1 STORES R5,R4 ; array[y] = 1 -- boundary condition MOVE R11,R9 ; item = y MLOOP2: ; do { -- compute items y-1 to 1 in row ADDSI R11,-1 ; item = item - 1 BLE MENDLP2 ; if (item <= 0) break LEA R3,R2,ARRAY-ARSIZE ADDI R4,R11,-1 ; -- get item - 1 ADDSL R4,R3,2 ; -- get address of array[item - 1] LOADS R5,R4 ; temp = array[item - 1] ; -- r4 holds address of array[item - 1] ADDSI R4,4 ; -- get address of array[item] LOADS R6,R4 ; temp1 = array[item] ; -- r4 still holds address of array[item] ADD R5,R5,R6 STORES R5,R4 ; array[item] = temp + temp1 BR MLOOP2 MENDLP2: ; } LIS R11,0 ; item = 0 MLOOP3: ; do { -- print items 0 to y in the row LEA R3,R2,ARRAY-ARSIZE MOVE R4,R11 ADDSL R4,R3,2 ; -- get address of array[item] LOADS R3,R4 ; -- parameter array[item] LIS R4,4 ; -- parameter field width LIL R1,PUTDEC JSRS R1,R1 ; putdec(array[item], 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 END