TITLE "MP4 by Douglas Jones, inorder version" ; plot / then left subtree, then \ then right subtree ; to plot a tree, pass the coordinates of the root USE "hawk.h" USE "stdio.h" INT MAIN S MAIN ; activation record for PLOTTREE ;RETAD = 0 SAVER8 = 4 SAVER9 = 8 SAVER10 = 12 SAVER11 = 16 ARSIZE = 20 PLOTTREE: ; expects R3 = size -- vertical size of this tree node ; R4 = x -- x coordinate of root ; R5 = y -- y coordinate of root STORES R1,R2 STORE R8,R2,SAVER8 STORE R9,R2,SAVER9 STORE R10,R2,SAVER10 STORE R11,R2,SAVER11 ADDI R2,R2,ARSIZE MOVE R8,R3 ; -- move size into place MOVE R9,R4 ; -- move x into place MOVE R10,R5 ; -- move y into place TESTR R8 BZS PLOTQT ; if (size >= 1) { MOVE R11,R8 ; i = size -- output size / marks LEFTLP: ; do { MOVE R3,R9 ; -- parameter x MOVE R4,R10 ; -- parameter y LIL R1,PUTAT JSRS R1,R1 ; putat( x, y ) LIS R3,'/' ; -- parameter LIL R1,PUTCHAR JSRS R1,R1 ; putchar( '/' ) ADDSI R9,-1 ; x = x - 1 ADDSI R10,1 ; y = y + 1 -- move down diagonal ADDSI R11,-1 ; i = i - 1 BGT LEFTLP ; } while (i > 0) MOVE R3,R8 SR R3,1 ; -- parameter size/2 MOVE R4,R9 ; -- parameter x MOVE R5,R10 ; -- parameter y JSR R1,PLOTTREE ; plottree( size/2, x, y ) ADD R9,R9,R8 ; x = x + size SUB R10,R10,R8 ; y = y + size -- undo left changes ADDSI R9,1 ; x = x + 1 -- move right for \ MOVE R11,R8 ; i = size -- output size \ marks RIGHTLP: ; do { MOVE R3,R9 ; -- parameter x MOVE R4,R10 ; -- parameter y LIL R1,PUTAT JSRS R1,R1 ; putat( x, y ) LIS R3,'\' ; -- parameter LIL R1,PUTCHAR JSRS R1,R1 ; putchar( '\' ) ADDSI R9,1 ; x = x + 1 ADDSI R10,1 ; y = y + 1 -- move down diagonal ADDSI R11,-1 ; i = i - 1 BGT RIGHTLP ; } while (i > 0) MOVE R3,R8 SR R3,1 ; -- parameter size/2 ADDI R4,R9,-1 ; -- parameter x-1 MOVE R5,R10 ; -- parameter y JSR R1,PLOTTREE ; plottree( size/2, x-1, y ) PLOTQT: ; } ADDI R2,R2,-ARSIZE LOAD R8,R2,SAVER8 LOAD R9,R2,SAVER9 LOAD R10,R2,SAVER10 LOAD R11,R2,SAVER11 LOADS PC,R2 ; activation record for MAIN ;RETAD = 0 ARSIZE = 4 MAIN: STORES R1,R2 ADDSI R2,ARSIZE LIL R3,TERMINFO LOAD R4,R3,TERMCOLS ; -- get termcols LOAD R5,R3,TERMROWS ; -- get termrows ; -- problem 1, what size fits? LIS R3,1 ; size = 1 ; here R3 = size ; -- size is always a power of 2 ; R4 = termcols ; R5 = termrows ; R6 = height then width SIZELP: ; for (;;) { MOVESL R6,R3,1 ADDSI R6,-1 ; height = 2*size - 1 CMP R5,R6 BLT SIZEQT ; if (termrows < height) break SL R6,1 ; width = 2*height CMP R4,R6 BLT SIZEQT ; if (termcols < width) break SL R3,1 ; size = size * 2 BR SIZELP SIZEQT: ; } ; -- we computed one size too large SRU R3,1 ; -- parameter size = size/2 SR R4,1 ADDSI R4,-1 ; -- parameter x = termrows/2 - 1 SR R5,1 SUB R5,R5,R3 ADDSI R5,1 ; -- parameter y = termcols/2 - size + 1 JSR R1,PLOTTREE ; plottree( size, x, y ) ADDSI R2,-ARSIZE LOADS PC,R2 ; return END