TITLE "mp3.a by Douglas Jones, Mar. 1 2015" ; traverse an n-ary tree and display the content ; this version is written without optimization ; this version does postorder 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 P = 4 ; pointer to a node I = 8 ; index into child array ARSIZE = 12 TRAVERS:; given: R3 = p -- pointer to a node STORES R1,R2 STORE R3,R2,P LIS R3,0 STORE R3,R2,I ; i = 0; TRAVLP: LOAD R3,R2,P LEA R3,R3,CHILDREN ; -- get address of p->children[0] LOAD R4,R2,I ADD R4,R4,R4 ADD R4,R4,R4 ADD R3,R3,R4 ; -- get address of p->children[i] LOADSCC R3,R3 ; -- get value of p->children[i] BZS TRAVQT ; while (p->children[i] != NULL) { ADDI R2,R2,ARSIZE ; -- note, parameter is already in place JSR R1,TRAVERS ; travers( p->children[i] ) ADDI R2,R2,-ARSIZE LOAD R3,R2,I ADDSI R3,1 ; i = i + 1 STORE R3,R2,I BR TRAVLP TRAVQT: ; } LOAD R5,R2,P LEA R5,R5,X ; -- compute address of p->x LOADS R4,R5 EXTH R3,R4,R5 ; -- parameter p->x LOAD R5,R2,P LEA R5,R5,Y LOADS R4,R5 EXTH R4,R4,R5 ; -- parameter p->y ADDI R2,R2,ARSIZE LIL R1,PUTAT JSRS R1,R1 ; putat( p->x, p->y ) ADDI R2,R2,-ARSIZE LOAD R3,R2,P LOAD R3,R3,TEXT ; -- parameter p->text ADDI R2,R2,ARSIZE LIL R1,PUTS JSRS R1,R1 ; puts( p->text ) ADDI R2,R2,-ARSIZE 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 LIL R3,ROOT JSR R1,TRAVERS ; travers( root ) ADDI R2,R2,-ARSIZE LOADS R1,R2 JUMPS R1 END