TITLE "count.a, count and output numbers" 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 integer 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 activation record RETAD = 0 ; return address I = 4 ; the parameter i REM = 8 ; the remainder i%10 QUO = 12 ; the quotient i/10 ARSIZE = 16 MYDEC: STORES R1,R2 STORE R3,R2,I LOAD R3,R2,I ; // parameter (dividend) LIS R4,10 ; // parameter (divisor) ADDI R2,R2,ARSIZE LIL R1,DIVIDEU JSRS R1,R1 ; // do actual division ADDI R2,R2,-ARSIZE STORE R4,R2,REM ; int rem = i%10; // least sig digit STORE R3,R2,QUO ; int quo = i%10; // most sig digits LOAD R3,R2,QUO CMPI R3,0 BLE MYDEIF ; if (quo > 0) { LOAD R3,R2,QUO ; // parameter ADDI R2,R2,ARSIZE LIL R1,MYDEC JSRS R1,R1 ; mydec( quo ); // output leading digits ADDI R2,R2,-ARSIZE MYDEIF: ; } LOAD R3,R2,REM ADDI R3,R3,'0' ; // parameter ADDI R2,R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ; putchar( rem + '0' ); // convert lsd to text ADDI R2,R2,-ARSIZE LOADS PC,R2 ; return to saved return address END