TITLE "decimal.a decimal output demo" ; This file contains ; 1) DECPUT, a recursive subroutine to put numbers out in decimal ; 2) a main program to test the above ; Note that the code here is entirely un optimized. You can make it ; run much faster and shrink its size considerably. USE "hawk.macs" USE "monitor.h" S START ; -------------------------------- ; Recursive decimal output routine ; -------------------------------- ; activation record structure RETAD = 0 ; return address REM = 4 ; remainder after division QUOT = 8 ; quotient after division ARSIZE = 12 ; activation record size DECPUT: ; entry point ; on entry, R1 return address ; R2 points to the new AR ; R3 integer parameter STORE R1,R2,RETAD ; save return address in AR ; R3 = param to be divided LIS R4,10 ; R4 = param to divide by LIL R1,DIVU ADDI R2,R2,ARSIZE ; push activation record JSRS R1,R1 ; call divu() to do R3/10 ADDI R2,R2,-ARSIZE ; pop activation record STORE R3,R2,QUOT ; store quotient STORE R4,R2,REM ; store remainder LOAD R3,R2,QUOT ; get quotient LIS R4,0 CMP R3,R4 ; compare R3 with zero BLE ENDIF ; if (R3 > 0) { LOAD R3,R2,QUOT ; R3 = param, quotient ADDI R2,R2,ARSIZE ; push activation record JSR R1,DECPUT ; call decput(quotient) ADDI R2,R2,-ARSIZE ; pop activation record ENDIF: ; } LOAD R3,R2,REM ADDI R3,R3,'0' ; compute remainder + '0' LIL R1,DSPCH ADDI R2,R2,ARSIZE ; push activation record JSRS R1,R1 ; call dspch(remainder + '0') ADDI R2,R2,-ARSIZE ; pop activation record LOAD R1,R2,RETAD ; restore return address from AR JUMPS R1 ; actually return ; -------------------------------- ; Main program to test the above ; -------------------------------- EXT UNUSED ; boilerplate start of main program START: LIL R2,UNUSED LIL R1,DSPINI JSRS R1,R1 ; end of boilerplate LIS R8,0 ; initialize loop counter LOOP: ; loop LIS R3,' ' LIL R1,DSPCH JSRS R1,R1 ; call dspch(' ') MOVE R3,R8 JSR R1,DECPUT ; call decput(loop counter) ADD R8,R8,R8 ADDSI R8,1 ; double then increment loop counter CMPI R8,10000 BLT LOOP ; end loop when loop counter > 10,000 LIL R1,EXIT ; boilerplate end of main program JSRS R1,R1 END