Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Attempts to convert the TI float in OP1 into an unsigned integer in HL.
- ;
- ;Throws:
- ; - A data type error if OP1 doesn't hold a nonnegative real number.
- ; - A domain error if the value cannot be exactly converted to an unsigned
- ; 16-bit integer.
- ;
- ; I: (OP1)=float
- ; O: A=0, BC=((uint)(OP1))%10, DE=OP1+8, HL=(uint)(OP1), (OP1)=(float)0
- ;FO: S=0, Z=1, H=0, P/V=0, N=1, C=0
- ;CC: 355 + 210*d
- ; d = (OP1)!=0 ? floor(log10((OP1))) + 1 : 1
- ConvOP1:
- ;Throws an error if OP1 doesn't hold a nonnegative real number.
- ld a,(OP1)
- or a
- jr nz,ErrDataType
- ;Initializes the 16-bit accumulator to 0.
- ld h,a
- ld l,a
- ConvOP1_Loop:
- ;Multiplies the 16-bit accumulator by 10, checking for overflow.
- ld b,h
- ld c,l
- add hl,hl
- adc a,a
- add hl,hl
- adc a,a
- add hl,bc
- adc a,a
- add hl,hl
- adc a,a
- jr nz,ErrDomain
- ;Rotates the first three mantissa bytes of OP1 left by a nibble, collecting the
- ;highest nibble/digit rotated out.
- ex de,hl
- ld hl,OP1+4
- rld
- dec l
- rld
- dec l
- rld
- dec l
- ;Adds the highest nibble/digit rotated out to the 16-bit accumulator, checking
- ;for overflow. Decrements the exponent and continues looping if it doesn't
- ;become $7F. Doesn't care about bad exponents, as the 16-bit accumulator would
- ;overflow eventually.
- ld c,a
- xor a
- ld b,a
- dec (hl)
- ex de,hl
- add hl,bc
- jr c,ErrDomain
- jp po,ConvOP1_Loop
- ConvOP1_CheckIntLoop:
- ;Returns successfully if the last byte of the mantissa has been checked.
- #ifdef 83p
- or e
- ret m
- #else
- ld a,(OP1+8)&$FF
- sub e
- ret z
- #endif
- ;Continues if the next byte of the mantissa is zero.
- inc e
- ld a,(de)
- or a
- jr z,ConvOP1_CheckIntLoop
- ErrDomain:
- ;Throws a domain error.
- B_CALL(_ErrDomain)
- ErrDataType:
- ;Throws a data type error.
- B_CALL(_ErrDataType)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement