TITLE "mp3.a by Douglas Jones" ; plot a fractal on the screen USE "hawk.h" USE "monitor.h" INT MAIN S MAIN SUBTITLE "MAIN program" ; activation record ;RETAD = 0 ARSIZE = 4 MAIN: ; expects R3 = screen width ; expects R4 = screen height STORES R1,R2 ; -- find screen size MOVE R6,R3 SR R6,1 ; size = width/2 CMP R6,R4 BLE MAIWID ; if (size > height) { MOVE R6,R4 ; size = height MAIWID: ; } ; -- find order from screen size LIS R5,0 ; order = 0; LIS R7,1 ; 3**order = 1 MAILOOP: CMP R7,R4 BGT MAIENDL ; while (3**order <= size) { ADDSI R5,1 ; order = order + 1 ADDSL R7,R7,1 ; -- 3**order = 3**order * 3 BR MAILOOP MAIENDL: ; } ; -- order is smallest that's too big to plot SR R3,1 ; -- parameter width/2 SR R4,1 ; -- parameter height/2 ADDSI R5,-1 ; -- parameter order-1 ADDI R2,R2,ARSIZE JSR R1,FRACTAL ; fractal( width/2,height/2,order-1 ) ADDI R2,R2,-ARSIZE LOADS R1,R2 JUMPS R1 ; return SUBTITLE "FRACTAL plot subroutine" ; activation record for FRACTAL ; retad = 0 X = 4 Y = 8 ; (x,y) = coordinates of the center ORDER = 12 ; order (of the sub figures, eventually) DISP = 16 ; 3**(order), offset of sub-figures in figure ARSIZE = 20 ; note that this implementation has been partly optimized FRACTAL:; expects: R3 = x ; R4 = y ; R5 = order STORES R1,R2 TESTR R5 BZR FRCBIG ; if (order == 0) { ADDI R2,R2,ARSIZE ; -- partly optimized below here LIL R1,PUTAT JSRS R1,R1 ; putat( x,y ) LIS R3,'[' LIL R1,PUTCHAR JSRS R1,R1 ; putchar( '[' ) LIS R3,']' LIL R1,PUTCHAR JSRS R1,R1 ; putchar( ']' ) ADDI R2,R2,-ARSIZE ; -- partly optimized above here BZR FRCDONE FRCBIG: ; } else { STORE R3,R2,X STORE R4,R2,Y ; -- partly optimized below here ADDSI R5,-1 ; order = order - 1; STORE R5,R2,ORDER ; -- save parameters *** ; -- compute disp = 3**(order-1) LIS R6,1 ; disp = 1; *** ; o = order ; -- instrs marked *** don't change CCs FRCLP: BZS FRCQT ; while (o != 0) { ADDSL R6,R6,1 ; disp = 3*disp ADDSI R5,-1 ; o = o - 1 BR FRCLP ; -- partly optimized above here FRCQT: ; } STORE R6,R2,DISP ; -- save disp ; -- in the following, we recursively plot ; -- the figure based on this 9-cell grid: ; -- 1 [2][3] ; -- [4][5] 6 the cells in brackets ; -- 7 8 [9] are the ones that print ; -- cell 5 ; -- parameters x and y still in R3,R4 LOAD R5,R2,ORDER ; -- parameter order ADDI R2,R2,ARSIZE JSR R1,FRACTAL ; fractal( x, y, order ) ADDI R2,R2,-ARSIZE ; -- cell 4 LOAD R3,R2,X LOAD R4,R2,DISP SL R4,1 SUB R3,R3,R4 ; -- parameter x - 2*disp LOAD R4,R2,Y ; -- parameter y LOAD R5,R2,ORDER ; -- parameter order ADDI R2,R2,ARSIZE JSR R1,FRACTAL ; fractal( x - 2*disp, y, order ) ADDI R2,R2,-ARSIZE ; -- cell 2 LOAD R3,R2,X ; -- parameter x LOAD R4,R2,Y LOAD R5,R2,DISP SUB R4,R4,R5 ; -- parameter y - disp LOAD R5,R2,ORDER ; -- parameter order ADDI R2,R2,ARSIZE JSR R1,FRACTAL ; fractal( x, y - disp, order ) ADDI R2,R2,-ARSIZE ; -- cell 3 LOAD R3,R2,DISP LOAD R4,R2,X ADDSL R3,R4,1 ; -- parameter x + 2*disp LOAD R4,R2,Y LOAD R5,R2,DISP SUB R4,R4,R5 ; -- parameter y - disp LOAD R5,R2,ORDER ; -- parameter order ADDI R2,R2,ARSIZE ; -- partly optimized below here JSR R1,FRACTAL ; fractal( x + 2*disp, y - disp, order ) ; -- cell 9 LOAD R3,R2,DISP -ARSIZE LOAD R4,R2,X -ARSIZE ADDSL R3,R4,1 ; -- parameter x + 2*disp LOAD R4,R2,DISP -ARSIZE LOAD R5,R2,Y -ARSIZE ADD R4,R4,R5 ; -- parameter y + disp LOAD R5,R2,ORDER-ARSIZE; -- parameter order JSR R1,FRACTAL ; fractal( x + 2*disp, y + disp, order ) ADDI R2,R2,-ARSIZE ; -- partly optimized above here FRCDONE: ; } LOADS R1,R2 JUMPS R1 ; return END