TITLE "decimal print routine" ; developed during lecture, Feb 24, 2014 ; this version of the code is nearly optimal USE "hawk.h" USE "monitor.h" EXT UNUSED SUBTITLE "subroutine to print a decimal number" ; activation record for PUTINT RETAD = 0 ; the return address ;;;;;N = 4 ; the parameter ;;;;;QUO= 8 ; the quotient REM = 4 ;12 ; the remainder ARSIZE = 8 ;16 PUTINT: ; expects R3 = n -- the number to print ; R1 = return address STORE R1,R2,RETAD ;;;;; STORE R3,R2,N ; -- save parameter n ;;;;; LOAD R3,R2,N ; -- parameter n LIS R5,10 ; -- parameter 10 ADDI R2,R2,ARSIZE LIL R1,DIVIDEU JSRS R1,R1 ; R3,R4 = divideu(n,10) ;;xxx ADDI R2,R2,-ARSIZE ;;;;; STORE R3,R2,QUO ; quo = R3 = n % 10 STORE R4,R2,REM-ARSIZE ; rem = R4 = n % 10 ;tricky!!! ;;;;; LOAD R3,R2,QUO ; -- recover quo ;;xxx ADDI R2,R2,ARSIZE ;moved UU CMPI R3,0 BEQ PUTDONE ; if (quo != 0) { ;;;;; LOAD R3,R2,QUO ; -- parameter quo ;;UU; ADDI R2,R2,ARSIZE JSR R1,PUTINT ; putint( quo ) ;;DD; ADDI R2,R2,-ARSIZE PUTDONE: ; } ADDI R2,R2,-ARSIZE ;moved DD LOAD R4,R2,REM ADDI R3,R4,"0" ; -- parameter rem+"0" ADDI R2,R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ; putchar( rem + "0" ) ADDI R2,R2,-ARSIZE LOAD R1,R2,RETAD JUMPS R1 ; return SUBTITLE "main program" ARSIZE = 4 INT MAIN S MAIN MAIN: ; entry point STORES R1,R2 ADDI R2,R2,ARSIZE ; --- begin aplication code --- LIS R8,0 ; i = 0 LIS R9,1 ; j = 1 LOOP: CMPI R8,10 BGT DONE ; while (i <= 10) { LIS R3," " ; -- parameter LIL R1,PUTCHAR JSRS R1,R1 ; putchar( " " ) MOVE R3,R9 ; -- parameter JSR R1,PUTINT ; putint( j ) ADDSI R8,1 ; i = i + 1 ADD R9,R9,R9 ; j = j*2 BR LOOP DONE: ; } ; --- end aplication code --- ADDI R2,R2,-ARSIZE LOADS R1,R2 JUMPS R1 ; return END