atof.s 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. .data
  2. ! Set of variables
  3. big: .byte 0
  4. .byte 0
  5. .byte 0x40
  6. .byte 24 ! 2^23
  7. negfrac:.space 1
  8. negexp: .space 1
  9. begzero:
  10. nd: .space 2
  11. fl: .space 6
  12. exp=fl+4
  13. eexp: .space 2
  14. flexp: .space 4
  15. exp5: .space 4
  16. endzero:
  17. ten: .byte 0
  18. .byte 0
  19. .byte 0x50
  20. .byte 4 ! 10
  21. dig: .byte 0
  22. .byte 0
  23. fildig: .byte 0 ! here a number from 0 to 31 will be converted flt.
  24. .byte 7
  25. bexp: .space 2
  26. .text
  27. atof: ! entry with stringpointer in hl
  28. ! exit with pointer to float in hl
  29. push ix
  30. push iy
  31. push bc
  32. push de
  33. push af
  34. ld b,1
  35. 1:
  36. ld a,(hl)
  37. inc hl
  38. cp ' '
  39. jr z,1b
  40. cp '-'
  41. jr nz,1f
  42. ld b,-1
  43. jr 2f
  44. 1: cp '+'
  45. jr z,2f
  46. dec hl
  47. 2: ld a,b
  48. ld (negfrac),a
  49. xor a
  50. ld de,begzero
  51. ld b,endzero-begzero
  52. 1: ld (de),a
  53. inc de
  54. djnz 1b
  55. 1: ld a,(hl)
  56. inc hl
  57. sub '0'
  58. jr c,1f
  59. cp 10
  60. jr nc,1f
  61. ld (fildig),a
  62. call cmpbigfl
  63. jr z,2f
  64. call mulandadd
  65. jr 3f
  66. 2: ld de,(exp)
  67. inc de
  68. ld (exp),de
  69. 3: ld de,(nd)
  70. inc de
  71. ld (nd),de
  72. jr 1b
  73. 1: cp '.'-'0'
  74. jr nz,4f
  75. 1: ld a,(hl)
  76. inc hl
  77. sub '0'
  78. jr c,4f
  79. cp 10
  80. jr nc,4f
  81. ld (fildig),a
  82. call cmpbigfl
  83. jr z,2f
  84. call mulandadd
  85. ld de,(exp)
  86. dec de
  87. ld (exp),de
  88. 2: ld de,(nd)
  89. inc de
  90. ld (nd),de
  91. jr 1b
  92. 4:
  93. ld b,1
  94. cp 'E'-'0'
  95. jr z,1f
  96. cp 'e'-'0'
  97. jr nz,5f
  98. 1: ld a,(hl)
  99. inc hl
  100. cp '+'
  101. jr z,1f
  102. cp '-'
  103. jr nz,2f
  104. ld b,-1
  105. jr 1f
  106. 2: dec hl
  107. 1: ld a,b
  108. ld (negexp),a
  109. exx
  110. xor a
  111. ld h,a
  112. ld l,a
  113. ld b,a
  114. ld d,a
  115. ld e,a
  116. exx
  117. 1: ld a,(hl)
  118. inc hl
  119. sub '0'
  120. jr c,1f
  121. cp 10
  122. jr nc,1f
  123. exx
  124. ld c,a
  125. add hl,hl
  126. add hl,hl
  127. add hl,de
  128. add hl,hl
  129. add hl,bc
  130. ld d,h
  131. ld e,l
  132. exx
  133. jr 1b
  134. 1: exx
  135. ld hl,negexp
  136. or a
  137. bit 7,(hl)
  138. ld hl,(exp)
  139. jr z,1f
  140. sbc hl,de
  141. jr 2f
  142. 1: add hl,de
  143. 2: ld (exp),hl
  144. exx
  145. 5: ld a,1
  146. ld de,(exp)
  147. push de
  148. bit 7,d
  149. jr z,1f
  150. neg
  151. ld hl,0
  152. or a
  153. sbc hl,de
  154. ex de,hl
  155. 1: ld (negexp),a
  156. ld (exp),de
  157. pop de
  158. ld hl,(nd)
  159. add hl,de
  160. ld de,-33 ! -LOGHUGE ?
  161. xor a
  162. sbc hl,de
  163. jp p,1f
  164. ld hl,fl
  165. ld b,6
  166. 2: ld (hl),a
  167. inc hl
  168. djnz 2b
  169. 1: ld hl,0x0140 ! 1.0
  170. ld (flexp+2),hl
  171. ld hl,0x0350 ! 5.0
  172. ld (exp5+2),hl
  173. ld hl,(exp)
  174. ld (bexp),hl
  175. 1: bit 0,l
  176. jr z,2f
  177. call xflt
  178. .word flexp,exp5,fpmult,4,flexp
  179. 2: sra h
  180. rr l
  181. ld a,h
  182. or l
  183. jr z,3f
  184. call xflt
  185. .word exp5,exp5,fpmult,4,exp5
  186. jr 1b
  187. 3: ld hl,negexp
  188. ld a,(bexp)
  189. bit 7,(hl)
  190. jr z,1f
  191. call xflt
  192. .word flexp,fl,fpdiv,4,fl
  193. neg
  194. jr 2f
  195. 1: call xflt
  196. .word flexp,fl,fpmult,4,fl
  197. 2: ld b,a
  198. ld a,(fl+3)
  199. add a,b
  200. ld (fl+3),a
  201. ld a,(negfrac)
  202. bit 7,a
  203. jr z,1f
  204. call xflt
  205. .word fl,fl,fpcomp,4,fl
  206. 1: call xflt
  207. .word fl,fl,fpnorm,4,fl
  208. ld hl,fl
  209. pop af
  210. pop de
  211. pop bc
  212. pop iy
  213. pop ix
  214. ret
  215. cmpbigfl:
  216. call xflt
  217. .word big,fl,fpcmf,0
  218. ld a,(fpac+1)
  219. bit 7,a
  220. ret
  221. mulandadd:
  222. call xflt
  223. .word fl,ten,fpmult,4,fl
  224. ld a,7
  225. ld (fildig+1),a
  226. call xflt
  227. .word dig,dig,fpnorm,4,dig
  228. call xflt
  229. .word fl,dig,fpadd,4,fl
  230. ret
  231. xflt:
  232. ex (sp),iy
  233. push af
  234. push bc
  235. push de
  236. push hl
  237. ld h,(iy+1)
  238. ld l,(iy+0)
  239. ld de,fpac
  240. ld bc,4
  241. ldir
  242. ld h,(iy+3)
  243. ld l,(iy+2)
  244. ld de,fpop
  245. ld bc,4
  246. ldir
  247. push iy
  248. ld hl,1f
  249. push hl
  250. ld h,(iy+5)
  251. ld l,(iy+4)
  252. jp (hl)
  253. 1: pop iy
  254. ld b,(iy+7)
  255. ld c,(iy+6)
  256. ld a,b
  257. or c
  258. jr z,1f
  259. inc iy
  260. inc iy
  261. ld hl,fpac
  262. ld d,(iy+7)
  263. ld e,(iy+6)
  264. ldir
  265. 1: push iy
  266. pop hl
  267. ld de,8
  268. add hl,de
  269. push hl
  270. pop iy
  271. pop hl
  272. pop de
  273. pop bc
  274. pop af
  275. ex (sp),iy
  276. ret