Advertisement
Runer112

ConvOP1

May 3rd, 2015
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Attempts to convert the TI float in OP1 into an unsigned integer in HL.
  2. ;
  3. ;Throws:
  4. ; - A data type error if OP1 doesn't hold a nonnegative real number.
  5. ; - A domain error if the value cannot be exactly converted to an unsigned
  6. ;   16-bit integer.
  7. ;
  8. ; I: (OP1)=float
  9. ; O: A=0, BC=((uint)(OP1))%10, DE=OP1+8, HL=(uint)(OP1), (OP1)=(float)0
  10. ;FO: S=0, Z=1, H=0, P/V=0, N=1, C=0
  11. ;CC: 355 + 210*d
  12. ;    d = (OP1)!=0 ? floor(log10((OP1))) + 1 : 1
  13. ConvOP1:
  14. ;Throws an error if OP1 doesn't hold a nonnegative real number.
  15.     ld  a,(OP1)
  16.     or  a
  17.     jr  nz,ErrDataType
  18. ;Initializes the 16-bit accumulator to 0.
  19.     ld  h,a
  20.     ld  l,a
  21. ConvOP1_Loop:
  22. ;Multiplies the 16-bit accumulator by 10, checking for overflow.
  23.     ld  b,h
  24.     ld  c,l
  25.     add hl,hl
  26.     adc a,a
  27.     add hl,hl
  28.     adc a,a
  29.     add hl,bc
  30.     adc a,a
  31.     add hl,hl
  32.     adc a,a
  33.     jr  nz,ErrDomain
  34. ;Rotates the first three mantissa bytes of OP1 left by a nibble, collecting the
  35. ;highest nibble/digit rotated out.
  36.     ex  de,hl
  37.     ld  hl,OP1+4
  38.     rld
  39.     dec l
  40.     rld
  41.     dec l
  42.     rld
  43.     dec l
  44. ;Adds the highest nibble/digit rotated out to the 16-bit accumulator, checking
  45. ;for overflow. Decrements the exponent and continues looping if it doesn't
  46. ;become $7F. Doesn't care about bad exponents, as the 16-bit accumulator would
  47. ;overflow eventually.
  48.     ld  c,a
  49.     xor a
  50.     ld  b,a
  51.     dec (hl)
  52.     ex  de,hl
  53.     add hl,bc
  54.     jr  c,ErrDomain
  55.     jp  po,ConvOP1_Loop
  56. ConvOP1_CheckIntLoop:
  57. ;Returns successfully if the last byte of the mantissa has been checked.
  58. #ifdef 83p
  59.     or  e
  60.     ret m
  61. #else
  62.     ld  a,(OP1+8)&$FF
  63.     sub e
  64.     ret z
  65. #endif
  66. ;Continues if the next byte of the mantissa is zero.
  67.     inc e
  68.     ld  a,(de)
  69.     or  a
  70.     jr  z,ConvOP1_CheckIntLoop
  71. ErrDomain:
  72. ;Throws a domain error.
  73.     B_CALL(_ErrDomain)
  74. ErrDataType:
  75. ;Throws a data type error.
  76.     B_CALL(_ErrDataType)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement