TITLE "count.a, count and output numbers, optimized version" USE "hawk.h" USE "stdio.h" USE "stdlib.h" SUBTITLE "MAIN -- test MYDEC print routine with powers of two" S MAIN INT MAIN ; stack usage ; 1 word, the return address MAIN: STORES R1,R2 ; save return address on the stack LIS R8,1 ; int i = 1; /* some powers of two */ WHILE: CMPI R8,10000 BGE WHILEX ; while (i < 10000) { LIL R1,MYDEC MOVE R3,R8 ; // parameter i ADDSI R2,4 JSRS R1,R1 ; mydec( i ); ADDSI R2,-4 LIL R1,PUTCHAR LIS R3,',' ; // parameter ADDSI R2,4 JSRS R1,R1 ; putchar( ',' ); ADDSI R2,-4 ADD R8,R8,R8; i = i * 2; BR WHILE WHILEX: ; } LOADS PC,R2 ; return to saved return address SUBTITLE "MYDEC -- output decimal integer as text" ; expects R3 = i, the nteger to print ; R2 stack pointer -- start of available memory ; R1 return address ; uses R3 to R7 ; preserves R9 and up ; stack usage -- structure of the optimized activation record RETAD = 0 ; return address REM = 4 ; the remainder i%10 ARSIZE = 8 ; -- small enough to use ADDSI instead of ADDI MYDEC: STORES R1,R2 ; // parameter (R3 = i = dividend) LIS R4,10 ; // parameter (R4 = divisor) ADDSI R2,ARSIZE LIL R1,DIVIDEU JSRS R1,R1 ; // do actual division ADDSI R2,-ARSIZE STORE R4,R2,REM ; int rem = i%10; // least sig digit ; int quo = i%10; // most sig digits TESTR R3 BLE MYDEIF ; if (quo > 0) { ; // parameter (R3 = quo) ADDSI R2,ARSIZE JSR R1,MYDEC ; mydec( quo ); // output leading digits ADDSI R2,-ARSIZE MYDEIF: ; } LOAD R3,R2,REM ADDI R3,R3,'0' ; // parameter ADDSI R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ; putchar( rem + '0' ); // convert lsd to text ADDSI R2,-ARSIZE LOADS PC,R2 ; return to saved return address END