TITLE "mp5.a by Douglas Jones" ; An implementation of PUTFLOAT ; This version is mostly un-optimized ; Except, it uses FPA0 to hold the number being converted ; Also note, this code makes a point of restoring COSTAT to ; its original value on return and not turning off any ; coprocessors that were already on; this goes beyond the ; requirements of the assinment but would be a good idea on ; any system where there were other coprocessors. ; The assignment did not specify whether the PUTFLOAT should ; expect to be called with the coprocessor on or off. ; therefore, it must be written to work either way. ; If the caller had the coprocessor on at the time of call, ; it is pretty obvious that PUTFLOAT should leave it on return. ; If the caller had the coprocessor off at the time of call, ; it makes sense that PUTFLOAT should turn it off on return. USE "hawk.h" USE "stdio.h" USE "float.h" INT PUTFLOAT ; putfloat activation record RETAD = 0 SVCOSTAT= 4 ; saved coprocessor status register INUM = 8 ; integer part of the number PLACES = 12 ; places of precision to show ARSIZE = 16 PUTFLOAT: ; expects R3 = num, a floating point number ; R4 = places, number of places to print after the point ; returns nothing ; does not turn off any coprocessors currently turned on ; uses R3-R7 ; FPA0 -- holds f ; FPA1 -- used as a temporary STORES R1,R2 STORE R4,R2,PLACES ; -- saved COGET R5,COSTAT STORE R5,R2,SVCOSTAT ; -- save old coprocessor status TRUNC R5,8 ; -- clear coop and cosel fields LIL R6,FPENAB + FPSEL + FPSHORT OR R5,R6 ; -- careful not to turn off other coprocessors COSET R5,COSTAT ; -- enable and select floating single TESTR R3 BNR PFELSE ; if (num < 0) { -- make positive, print sign CMP R3,R3 ; -- set carry bit by comparing equal values ADJUST R3,CMSB ; num = -num -- by adding 1 to sign bit COSET R3,FPA0 ; -- fpa0 now holds num LIS R3,'-' ADDI R2,R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ADDI R2,R2,-ARSIZE ; putchar( '-' ) BR PFENDIF PFELSE: ; } else { COSET R3,FPA0 ; -- fpa0 now holds num PFENDIF: ; } COGET R3,FPA0 ADDI R2,R2,ARSIZE LIL R1,FTOI JSRS R1,R1 ADDI R2,R2,-ARSIZE STORE R3,R2,INUM ; inum = (int)num LIS R4,1 ADDI R2,R2,ARSIZE LIL R1,PUTDECU JSRS R1,R1 ADDI R2,R2,-ARSIZE ; putdec( inum, 1 ) LIS R3,'.' ADDI R2,R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ADDI R2,R2,-ARSIZE ; putchar( '.' ) PFLOOP: ; loop { TEST R2,PLACES BLE PFQUIT ; if (places < 0) break; LOAD R3,R2,INUM COSET R3,FPINT+FPA1 COGET R3,FPA1 COSET R3,FPSUB+FPA0 ; num = num - (float)inum LIF R3,10 COSET R3,FPMUL+FPA0 ; num = num * 10.0 COGET R3,FPA0 ADDI R2,R2,ARSIZE LIL R1,FTOI JSRS R1,R1 ADDI R2,R2,-ARSIZE STORE R3,R2,INUM ; inum = (int)num ADDI R3,R3,'0' ADDI R2,R2,ARSIZE LIL R1,PUTCHAR JSRS R1,R1 ADDI R2,R2,-ARSIZE ; putchar( inum + '0' ) LOAD R3,R2,PLACES ADDSI R3,-1 STORE R3,R2,PLACES ; places = places - 1 BR PFLOOP PFQUIT: ; } LOAD R5,R2,SVCOSTAT COSET R5,COSTAT ; -- restore coprocessor status LOADS R1,R2 JUMPS R1 ; return END