TITLE "mp3.a by Douglas Jones, Mar. 1 2014" ; traverse an n-ary tree and display the content ; this version is fairly well optimized ; this version does preorder traversal USE "hawk.h" USE "monitor.h" SUBTITLE "Recursive tree traverser" ; tree node format X = 0 ; halfword X coordinate Y = 2 ; halfword Y coordinate TEXT = 4 ; pointer to text CHILDREN= 8 ; first of many children ; activation record format ;RETAD = 0 SVR8 = 4 ; save area for R8 ARSIZE = 8 TRAVERS:; given: R3 = p -- pointer to a node STORES R1,R2 STORE R8,R2,SVR8 ADDI R2,R2,ARSIZE ; -- activation record optimized MOVE R8,R3 ; -- R8 holds p LOADS R4,R8 EXTH R3,R4,R8 ; -- parameter p->x SRU R4,16 ; -- parameter p->y LIL R1,PUTAT JSRS R1,R1 ; putat( p->x, p->y ) LOAD R3,R8,TEXT ; -- parameter p->text LIL R1,PUTS JSRS R1,R1 ; puts( p->text ) LEA R8,R8,CHILDREN ; p = *p->children ; -- p points to the next child pointer LOADSCC R3,R8 BZS TRAVQT ; if (*p != NULL) { TRAVLP: ; do { ; -- note, parameter is already in place JSR R1,TRAVERS ; travers( *p ) ADDSI R8,4 ; p++ LOADSCC R3,R8 BZR TRAVLP ; } while (*p != NULL) TRAVQT: ; } ADDI R2,R2,-ARSIZE ; -- activation record optimized LOAD R8,R2,SVR8 LOADS R1,R2 JUMPS R1 ; return SUBTITLE "main program" INT MAIN S MAIN EXT ROOT ;RETAD = 0 ARSIZE = 4 MAIN: STORES R1,R2 ADDI R2,R2,ARSIZE ; -- activation record optimized LIL R3,ROOT JSR R1,TRAVERS ; travers( root ) ADDI R2,R2,-ARSIZE ; -- activation record optimized LOADS R1,R2 JUMPS R1 END