TITLE "MP3 by Douglas Jones" ; Plot the largest H-tree that will fit on the screen, ; Reference solution, Oct. 17, 2011 USE "hawk.h" USE "monitor.h" ;RETAD = 0 H = 4 W = 8 ARSIZE = 12 INT MAIN S MAIN MAIN: ; executable code begins here ; expects R3 = w, the screen width ; expects R4 = h, the screen height STORES R1,R2 STORE R3,R2,W STORE R4,R2,H ADDI R2,R2,ARSIZE JSR R1,FINDMAX ; order = findmax(w,h) ADDI R2,R2,-ARSIZE LOAD R4,R2,W SRU R4,1 LOAD R5,R2,H ADDSI R5,-1 SRU R5,1 ADDI R2,R2,ARSIZE JSR R1,PLOTH ; ploth(order,w/2,(h-1)/2) ADDI R2,R2,-ARSIZE LOADS R1,R2 JUMPS R1 ; return to the monitor to stop! SUBTITLE "findmax - find max h-tree that fits on screen" FINDMAX: ; expects R3 = w, the screen width ; expects R4 = h, the screen height ; returns R3 = order CMP R3,R4 BGEU FMXEI1 ; if (w < h) { -- find minimum dimension MOVE R4,R3 ; h = w FMXEI1: ; } -- h is now controlling dimension ; -- setup for loop LIS R3,0 ; order = 0 -- order of H-tree LIS R5,0 ; r = 0 -- right diagonal dimension LIS R6,0 ; l = 0 -- left diagonal dimension FMXLP: ; do { ADDSI R3,1 ; order = order + 1 BITTST R3,0 BBS FMXODD ; if (order is even) { ADD R5,R5,R5 ADDSI R5,2 ; l = 2l + 2 BR FMXEI2 FMXODD: ; } else { -- order is odd ADD R6,R6,R6 ADDSI R6,2 ; r = 2r + 2 FMXEI2: ; } ADD R7,R6,R5 CMP R7,R4 BLEU FMXLP ; } while ((r + l) < h) ADDSI R3,-1 ; order = order - 1 JUMPS R1 ; return order SUBTITLE "ploth - plot h-tree" ; activation record ;RETAD = 0 ARSIZE = 4 PLOTH: ; expects R3 = order of the tree to plot ; expects (R4,R5) = (x,y) coordinates of the center point STORES R1,R2 TESTR R1 BZS PLHEI1 ; if (order > 0) { ADDI R7,R3,-1 SRU R7,1 ; o = (order-1)/2; LIS R6,1 ; d = 1 TESTR R7 BZS PLHEI2 ; if (o != 0) { PLHLP: ; do { ADD R6,R6,R6 ; d = 2d ADDSI R7,-1 ; o = o - 1 BZR PLHLP ; } while (o != 0) PLHEI2: ; } ; -- now d = 2**((order-1)/2) BITTST R3,0 BBS PLHODD ; if (order is even) { MOVE R3,R6 ADDI R2,R2,ARSIZE JSR R1,PLOTHE ; plothe(d,x,y) ADDI R2,R2,-ARSIZE BR PLHEI3 PLHODD: ; } else { -- else order is odd MOVE R3,R6 ADDI R2,R2,ARSIZE JSR R1,PLOTHO ; plotho(d,x,y) ADDI R2,R2,-ARSIZE PLHEI3: ; } PLHEI1: ; } LOADS R1,R2 JUMPS R1 ; return SUBTITLE "plothe - plot even h-tree" ; activation record ;RETAD = 0 SAVER8 = 4 ; save locations for R8-R11 SAVER9 = 8 SAVER10 = 12 ARSIZE = 16 PLOTHE: ; expects R3 = d, halflength of diagonal of tree to plot ; expects (R4,R5) = (x,y) coordinates of the center point STORES R1,R2 TESTR R3 BZS PLEEI ; if (d != 0) { STORE R8,R2,SAVER8 STORE R9,R2,SAVER9 STORE R10,R2,SAVER10 ; -- save registers MOVE R8,R3 ; d = d MOVE R9,R4 ; x = x MOVE R10,R5 ; y = y -- moved to new locations ADD R4,R4,R3 ; -- plot my upper right child SUB R5,R5,R3 ADDI R2,R2,ARSIZE JSR R1,PLOTHO ; plotho(d,x+d,y-d) ADDI R2,R2,-ARSIZE MOVE R3,R8 MOVE R4,R9 MOVE R5,R10 ; -- recover d,x,y SUB R4,R4,R3 ; -- plot my lower left child ADD R5,R5,R3 ADDI R2,R2,ARSIZE JSR R1,PLOTHO ; plotho(d,x-d,y+d) ADDI R2,R2,-ARSIZE PLELP: ; do { -- plot diagonal twixt children ADD R3,R9,R8 ADDSI R3,-1 SUB R4,R10,R8 ADDSI R4,1 LIS R5,"/" ADDI R2,R2,ARSIZE JSR R1,ATPLOT ; atplot(x+d-1,y-d+1,'/') ADDI R2,R2,-ARSIZE SUB R3,R9,R8 ADD R4,R10,R8 LIS R5,"/" ADDI R2,R2,ARSIZE JSR R1,ATPLOT ; atplot(x-d,y+d,'/') ADDI R2,R2,-ARSIZE ADDSI R8,-1 ; d = d - 1; BZR PLELP ; } while (d != 0 LOAD R8,R2,SAVER8 LOAD R9,R2,SAVER9 LOAD R10,R2,SAVER10 ; -- restore registers PLEEI: ; } LOADS R1,R2 JUMPS R1 ; return SUBTITLE "plotho - plot odd h-tree" ; activation record ;RETAD = 0 SAVER8 = 4 ; save locations for R8-R10 SAVER9 = 8 SAVER10 = 12 ARSIZE = 16 PLOTHO: ; expects R3 = d, halflength of diagonal ; expects (R4,R5) = (x,y) coordinates of the center point STORES R1,R2 ; -- note order is odd, so d is nonzero STORE R8,R2,SAVER8 STORE R9,R2,SAVER9 STORE R10,R2,SAVER10 ; -- save registers MOVE R8,R3 ; d = d MOVE R9,R4 ; x = x MOVE R10,R5 ; y = y -- moved to new locations SUB R4,R4,R3 ; -- plot my upper left child SUB R5,R5,R3 SR R3,1 ADDI R2,R2,ARSIZE JSR R1,PLOTHE ; plothe(d/2,x-d,y-d) ADDI R2,R2,-ARSIZE MOVE R3,R8 MOVE R4,R9 MOVE R5,R10 ; -- recover order,x,y,d ADD R4,R4,R3 ; -- plot my lower left child ADD R5,R5,R3 SR R3,1 ADDI R2,R2,ARSIZE JSR R1,PLOTHE ; plothe(d/2,x+d,y+d) ADDI R2,R2,-ARSIZE PLOLP: ; do { -- plot diagonal twixt children SUB R3,R9,R8 SUB R4,R10,R8 ADDSI R4,1 LIS R5,"\" ADDI R2,R2,ARSIZE JSR R1,ATPLOT ; atplot(x-d,y-d+1,'\') ADDI R2,R2,-ARSIZE ADD R3,R9,R8 ADDSI R3,-1 ADD R4,R10,R8 LIS R5,"\" ADDI R2,R2,ARSIZE JSR R1,ATPLOT ; atplot(x+d-1,y+d,'\') ADDI R2,R2,-ARSIZE ADDSI R8,-1 ; d = d - 1; BZR PLOLP ; } while (d != 0 LOAD R8,R2,SAVER8 LOAD R9,R2,SAVER9 LOAD R10,R2,SAVER10 ; restore registers PLOEI: ; } LOADS R1,R2 JUMPS R1 ; return SUBTITLE "atplot - at a location plot a character" ; activation record ;RETAD = 0 CH = 4 ; character to plot ARSIZE = 8 ATPLOT: ; expects R3,R4 = x,y location ; expects R5 = ch, the character STORES R1,R2 STORE R5,R2,CH ADDI R2,R2,ARSIZE LIL R1,PUTAT JSRS R1,R1 ; putat(x,y) ADDI R2,R2,-ARSIZE LOAD R3,R2,CH ADDI R2,R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ; putchar(ch) ADDI R2,R2,-ARSIZE LOADS R1,R2 JUMPS R1 ; return END