4.2.1 SINGLE-PRECISION CALCULATIONS 205 13 FDIV STJ (Web hosting isp) EXITF
Wednesday, October 31st, 20074.2.1 SINGLE-PRECISION CALCULATIONS 205 13 FDIV STJ EXITF Floating point division subroutine: 14 JOV OFLO Ensure overflow is off. 15 STA TEMP TEMP + v. 16 STA FV (0 : 4) FV+-ItffffO. 17 LDI TEMP (EXP) 18 LD2 ACC(EXP) 19 DEC2 -Q,l r12 e e, -e, + q. Z?O ENTX 0 .21 LDA ACC 22 SLA 1 rA+ fu. 23 CMPA FV(I : 5) 24 JL *+3 Jump if lful < IfJ. .25 SRA I Otherwise, scale f,, right 26 INC2 1 and increase r12 by 1. .27 DIV FV Divide. .28 JNOV NORM Normalize, round, and exit. 29 DVZRO HLT 3 Unnormalized or zero divisor 1 The most noteworthy feature of this program is the provision for division in lines 23-26, which is made in order to ensure enough accuracy to round the answer. If Iful < Ifv I, straightforward application of Algorithm M would leave a result of the form & 0 f f f f in register A, and this would not allow a proper rounding without a careful analysis of the remainder (which appears in register X). So the program computes fw + fu/fv in this case, ensuring that fw is either zero or normalized in all cases; rounding can proceed with five significant bytes, possibly testing whether the remainder is zero. We occasionally need to convert values between fixed and floating point representations. A fix-to-float routine is easily obtained with the help of the normalization algorithm above; for example, in MIX, the following subroutine converts an integer to floating point form: 01 FLOT STJ FXITF Assume that rA = u, an integer. 02 JOV OFLO Ensure overflow is off. 03 ENT2 Q+5 Set raw exponent. (10) 04 ENTX 0 05 JMP NORM Normalize, round, and exit. 1 A float-to-fix subroutine is the subject of exercise 14. The debugging of floating point subroutines is usually a difficult job, since there are so many cases to consider. Here is a list of common pitfalls that often trap a programmer or machine designer who is preparing floating point routines: 1) Losing the sign. On many machines (not MIX), shift instructions between registers will affect the sign, and the shifting operations used in normalizing and scaling numbers must be carefully analyzed. The sign is also lost frequently when minus zero is present. (For example, Program A is careful to retain the sign of register A in lines 30-34. See also exercise 6.)