TITLE "toy1.a -- a toy recursive program" USE "hawk.h" USE "stdio.h" USE "ascii.h" USE "stdlib.h" INT MAIN S MAIN ; activation record for myprint RETAD = 0 PARAMB = 4 DIGIT = 8 ARSIZE = 12 MYPRINT:; expects R3 = n, number to print ; R4 = b, number base STORES R1,R2 ; void myprint( n, b ) { STORE R4,R2,PARAMB ADDI R2,R2,ARSIZE ; -- by luck, params n and b in right places LIL R1,DIVIDEU JSRS R1,R1 ADDI R2,R2,-ARSIZE ; int rest = n/b -- in R3 STORE R4,R2,DIGIT ; int digit = n%b TESTR R3 BLE QUIT ; if (rest > 0) { ; -- parameter rest (already in R3) LOAD R4,R2,PARAMB ; -- parameter b ADDI R2,R2,ARSIZE JSR R1,MYPRINT ; myprint( rest, b ); ADDI R2,R2,-ARSIZE QUIT: ; } LOAD R3,R2,DIGIT ADDI R3,R3,'0' ; -- param digit + '0' ADDI R2,R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ; putchar( digit + '0' ); ADDI R2,R2,-ARSIZE LOADS R1,R2 JUMPS R1 ; } ; activation record for main RETAD = 0 ARSIZE = 4 MAIN: ; no parameters ; void main () { STORES R1,R2 ; /* test myprint with powers of 2 */ LIS R8,1 ; int i = 1 LOOP: CMPI R8,100 BGE DONE ; while (i < 100) { MOVE R3,R8 ; -- param i LIS R4,9 ; -- param 9 ADDI R2,R2,ARSIZE JSR R1,MYPRINT ; myprint( i, 9 ); ADDI R2,R2,-ARSIZE ADD R8,R8,R8 ; i = i * 2 LIS R3,LF ; -- param '\n' ADDI R2,R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ; putchar( '\n' ); ADDI R2,R2,-ARSIZE BR LOOP DONE: ; } LOADS R1,R2 JUMPS R1 ; }