TITLE "mp3.a by Douglas Jones, Oct. 5 2014" ; draws a the largest centered Sierpinski triangle ; that will fit on the screen USE "hawk.h" USE "monitor.h" SUBTITLE "Sierpinski triangle plotter" ; activation record format ;RETAD = 0 H = 4 ; height of the triangle; for order o, h = 2**(o - 1) X = 8 ; x coordinate of left edge Y = 12 ; y coordinate of top edge ARSIZE = 16 TRIANG: ; given: R3 = h -- note, h must be a power of two ; R4 = x ; R5 = y STORES R1,R2 STORE R3,R2,H STORE R4,R2,X STORE R5,R2,Y ADDI R2,R2,ARSIZE ; -- activation record optimized CMPI R3,1 BGT TRIELS ; if (h == 1) { -- simple case MOVE R3,R4 ; -- parameter 1: x -- values still in regs! MOVE R4,R5 ; -- parameter 2: y LIL R1,PUTAT JSRS R1,R1 ; setat( x, y ) LEA R3,TRIS1 ; -- parameter 1: s1 = "__" LIL R1,PUTS JSRS R1,R1 ; puts( "__" ) LOAD R3,R2,X-ARSIZE ; -- parameter 1: x LOAD R4,R2,Y-ARSIZE ADDSI R4,1 ; -- parameter 2: y + 1 LIL R1,PUTAT JSRS R1,R1 ; setat( x, y + 1 ) LEA R3,TRIS2 ; -- parameter 1: s1 = "\/" LIL R1,PUTS JSRS R1,R1 ; puts( "\/" ) BR TRIEND TRIELS: ; } else { -- recursive case ; -- bottom triangle LOAD R3,R2,H-ARSIZE SR R3,1 ; -- parameter 1: h/2 = 2**(o - 2) LOAD R4,R2,X-ARSIZE ADD R4,R4,R3 ; -- parameter 2: x + h/2 LOAD R5,R2,Y-ARSIZE ADD R5,R5,R3 ; -- parameter 3: y + h/2 JSR R1,TRIANG ; triang( h/2, x + h/2, y + h/2 ) ; -- left triangle LOAD R3,R2,H-ARSIZE SR R3,1 ; -- parameter 1: h/2 = 2**(o - 2) LOAD R4,R2,X-ARSIZE ; -- parameter 2: x LOAD R5,R2,Y-ARSIZE ; -- parameter 3: y JSR R1,TRIANG ; triang( h/2, x, y ) ; -- right triangle LOAD R3,R2,H-ARSIZE LOAD R4,R2,X-ARSIZE ADD R4,R4,R3 ; -- parameter 2: x + h = x + 2**(o - 1) SR R3,1 ; -- parameter 1: h/2 = 2**(o - 2) LOAD R5,R2,Y-ARSIZE ; -- parameter 3: y JSR R1,TRIANG ; -- triang( h/2, x + h, y ) TRIEND: ; } ADDI R2,R2,-ARSIZE ; -- activation record optimized LOADS R1,R2 JUMPS R1 TRIS1: ASCII "__",0 TRIS2: ASCII "\/",0 ALIGN 2 SUBTITLE "main program" INT MAIN S MAIN ;RETAD = 0 ARSIZE = 4 MAIN: ; given: R3 = w -- screen width ; R4 = h -- screen height STORES R1,R2 ADDI R2,R2,ARSIZE ; -- activation record optimized ; -- does w or h set the size maximum m? MOVE R5,R3 SR R5,1 ; m = w/2 ADDI R6,R4,-1 ; -- h-1 CMP R6,R5 BLE MAIEI ; if ((h-1) > w/2) { MOVE R6,R5 ; m = w/2 MAIEI: ; } ; -- what power of 2 th is <= m? LIS R7,1 ; th = 1 MAILP: ; do { SL R7,1 ; th = 2*h CMP R7,R6 BLE MAILP ; } while (th <= m) SR R7,1 ; th = th/2 SUB R5,R4,R7 SR R5,1 ; -- parameter 3: y = h/2 - th/2 = (h - th)/2 SR R3,1 SUB R4,R3,R7 ; -- parameter 2: x = w/2 - th MOVE R3,R7 ; -- parameter 1: th JSR R1,TRIANG ; triang( h, w/2 - th, h/2 - th/2 ) ADDI R2,R2,-ARSIZE ; -- activation record optimized LOADS R1,R2 JUMPS R1 END