fp8087.s 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. .define .adf4, .adf8, .sbf4, .sbf8, .mlf4, .mlf8, .dvf4, .dvf8
  2. .define .ngf4, .ngf8, .fif4, .fif8, .fef4, .fef8
  3. .define .cif4, .cif8, .cuf4, .cuf8, .cfi, .cfu, .cff4, .cff8
  4. .define .cmf4, .cmf8
  5. .sect .text; .sect .rom; .sect .data; .sect .bss
  6. ! $Id$
  7. ! Implement interface to floating point package for Intel 8087
  8. .sect .rom
  9. one:
  10. .data2 1
  11. two:
  12. .data2 2
  13. bigmin:
  14. .data2 0, -32768
  15. .sect .text
  16. .adf4:
  17. mov bx,sp
  18. wait
  19. flds 2(bx)
  20. wait
  21. fadds 6(bx)
  22. wait
  23. fstps 6(bx)
  24. wait
  25. ret
  26. .adf8:
  27. mov bx,sp
  28. wait
  29. fldd 2(bx)
  30. wait
  31. faddd 10(bx)
  32. wait
  33. fstpd 10(bx)
  34. wait
  35. ret
  36. .sbf4:
  37. mov bx,sp
  38. wait
  39. flds 6(bx)
  40. wait
  41. fsubs 2(bx)
  42. wait
  43. fstps 6(bx)
  44. wait
  45. ret
  46. .sbf8:
  47. mov bx,sp
  48. wait
  49. fldd 10(bx)
  50. wait
  51. fsubd 2(bx)
  52. wait
  53. fstpd 10(bx)
  54. wait
  55. ret
  56. .mlf4:
  57. mov bx,sp
  58. wait
  59. flds 2(bx)
  60. wait
  61. fmuls 6(bx)
  62. wait
  63. fstps 6(bx)
  64. wait
  65. ret
  66. .mlf8:
  67. mov bx,sp
  68. wait
  69. fldd 2(bx)
  70. wait
  71. fmuld 10(bx)
  72. wait
  73. fstpd 10(bx)
  74. wait
  75. ret
  76. .dvf4:
  77. mov bx,sp
  78. wait
  79. flds 6(bx)
  80. wait
  81. fdivs 2(bx)
  82. wait
  83. fstps 6(bx)
  84. wait
  85. ret
  86. .dvf8:
  87. mov bx,sp
  88. wait
  89. fldd 10(bx)
  90. wait
  91. fdivd 2(bx)
  92. wait
  93. fstpd 10(bx)
  94. wait
  95. ret
  96. .ngf4:
  97. mov bx,sp
  98. wait
  99. flds 2(bx)
  100. wait
  101. fchs
  102. wait
  103. fstps 2(bx)
  104. wait
  105. ret
  106. .ngf8:
  107. mov bx,sp
  108. wait
  109. fldd 2(bx)
  110. wait
  111. fchs
  112. wait
  113. fstpd 2(bx)
  114. wait
  115. ret
  116. .fif4:
  117. mov bx,sp
  118. push bx ! make room for FP status word
  119. wait
  120. flds 4(bx)
  121. wait
  122. fmuls 8(bx) ! multiply
  123. wait
  124. fld st ! copy result
  125. wait
  126. ftst ! test sign; handle negative separately
  127. wait
  128. fstsw -2(bx)
  129. wait
  130. mov ax,-2(bx)
  131. sahf ! result of test in condition codes
  132. jb 1f
  133. frndint ! this one rounds (?)
  134. wait
  135. fcom st(1) ! compare with original; if <=, then OK
  136. wait
  137. fstsw -2(bx)
  138. wait
  139. mov ax,-2(bx)
  140. sahf
  141. jbe 2f
  142. fisubs (one) ! else subtract 1
  143. wait
  144. jmp 2f
  145. 1: ! here, negative case
  146. frndint ! this one rounds (?)
  147. wait
  148. fcom st(1) ! compare with original; if >=, then OK
  149. wait
  150. fstsw -2(bx)
  151. wait
  152. mov ax,-2(bx)
  153. sahf
  154. jae 2f
  155. fiadds (one) ! else add 1
  156. wait
  157. 2:
  158. fsub st(1),st ! subtract integer part
  159. wait
  160. mov bx,2(bx)
  161. fstps (bx)
  162. wait
  163. fstps 4(bx)
  164. wait
  165. pop bx
  166. ret
  167. .fif8:
  168. mov bx,sp
  169. push bx ! make room for FP status word
  170. wait
  171. fldd 4(bx)
  172. wait
  173. fmuld 12(bx) ! multiply
  174. wait
  175. fld st ! and copy result
  176. wait
  177. ftst ! test sign; handle negative separately
  178. wait
  179. fstsw -2(bx)
  180. wait
  181. mov ax,-2(bx)
  182. sahf ! result of test in condition codes
  183. jb 1f
  184. frndint ! this one rounds (?)
  185. wait
  186. fcom st(1) ! compare with original; if <=, then OK
  187. wait
  188. fstsw -2(bx)
  189. wait
  190. mov ax,-2(bx)
  191. sahf
  192. jbe 2f
  193. fisubs (one) ! else subtract 1
  194. wait
  195. jmp 2f
  196. 1: ! here, negative case
  197. frndint ! this one rounds (?)
  198. wait
  199. fcom st(1) ! compare with original; if >=, then OK
  200. wait
  201. fstsw -2(bx)
  202. wait
  203. mov ax,-2(bx)
  204. sahf
  205. jae 2f
  206. fiadds (one) ! else add 1
  207. wait
  208. 2:
  209. fsub st(1),st ! subtract integer part
  210. mov bx,2(bx)
  211. fstpd (bx)
  212. wait
  213. fstpd 8(bx)
  214. wait
  215. pop bx
  216. ret
  217. .fef4:
  218. ! this could be simpler, if only the
  219. ! fxtract instruction was emulated properly
  220. mov bx,sp
  221. mov ax,6(bx)
  222. and ax,077600
  223. je 1f ! zero exponent
  224. mov cx,7
  225. shr ax,cl
  226. sub ax,126
  227. mov cx,ax ! exponent in cx
  228. mov ax,6(bx)
  229. and ax,0100177
  230. or ax,0037400 ! load -1 exponent
  231. mov dx,4(bx)
  232. mov bx,2(bx)
  233. mov 4(bx),ax
  234. mov 2(bx),dx
  235. mov (bx),cx
  236. ret
  237. 1: ! we get here on zero exp
  238. mov ax,6(bx)
  239. and ax,0177
  240. or ax,4(bx)
  241. jne 1f ! zero result
  242. xor ax,ax
  243. mov bx,2(bx)
  244. mov (bx),ax
  245. mov 2(bx),ax
  246. mov 4(bx),ax
  247. ret
  248. 1: ! otherwise unnormalized number
  249. mov cx,6(bx)
  250. and cx,0100177
  251. mov dx,cx
  252. and cx,0x8000
  253. mov ax,-125
  254. 2:
  255. test dx,0x80
  256. jne 1f
  257. dec ax
  258. shl 4(bx),1
  259. rcl dx,1
  260. or dx,cx
  261. jmp 2b
  262. 1:
  263. mov cx,4(bx)
  264. mov bx,2(bx)
  265. mov (bx),ax
  266. mov 2(bx),cx
  267. and dx,0100177
  268. or dx,0037400 ! load -1 exponent
  269. mov 4(bx),dx
  270. ret
  271. .fef8:
  272. ! this could be simpler, if only the
  273. ! fxtract instruction was emulated properly
  274. mov bx,sp
  275. mov ax,10(bx)
  276. and ax,077760
  277. je 1f ! zero exponent
  278. mov cx,4
  279. shr ax,cl
  280. sub ax,1022
  281. mov cx,ax ! exponent in cx
  282. mov ax,10(bx)
  283. and ax,0100017
  284. or ax,0037740 ! load -1 exponent
  285. push 8(bx)
  286. push 6(bx)
  287. push 4(bx)
  288. mov bx,2(bx)
  289. pop 2(bx)
  290. pop 4(bx)
  291. pop 6(bx)
  292. mov 8(bx),ax
  293. mov (bx),cx
  294. ret
  295. 1: ! we get here on zero exp
  296. mov ax,10(bx)
  297. and ax,017
  298. or ax,8(bx)
  299. or ax,6(bx)
  300. or ax,4(bx)
  301. jne 1f ! zero result
  302. xor ax,ax
  303. mov bx,2(bx)
  304. mov (bx),ax
  305. mov 2(bx),ax
  306. mov 4(bx),ax
  307. mov 6(bx),ax
  308. mov 8(bx),ax
  309. ret
  310. 1: ! otherwise unnormalized number
  311. mov cx,10(bx)
  312. and cx,0100017
  313. mov dx,cx
  314. and cx,0x8000
  315. mov ax,-1021
  316. 2:
  317. test dx,0x10
  318. jne 1f
  319. dec ax
  320. shl 4(bx),1
  321. rcl 6(bx),1
  322. rcl 8(bx),1
  323. rcl dx,1
  324. or dx,cx
  325. jmp 2b
  326. 1:
  327. and dx,0100017
  328. or dx,0037740 ! load -1 exponent
  329. mov cx,8(bx)
  330. push 6(bx)
  331. push 4(bx)
  332. mov bx,2(bx)
  333. mov (bx),ax
  334. mov 8(bx),dx
  335. mov 6(bx),cx
  336. pop 2(bx)
  337. pop 4(bx)
  338. ret
  339. .cif4:
  340. mov bx,sp
  341. cmp 2(bx),2
  342. jne 1f
  343. wait
  344. filds 4(bx)
  345. wait
  346. fstps 2(bx)
  347. wait
  348. ret
  349. 1:
  350. wait
  351. fildl 4(bx)
  352. wait
  353. fstps 4(bx)
  354. wait
  355. ret
  356. .cif8:
  357. mov bx,sp
  358. cmp 2(bx),2
  359. jne 1f
  360. wait
  361. filds 4(bx)
  362. wait
  363. fstpd 2(bx)
  364. wait
  365. ret
  366. 1:
  367. wait
  368. fildl 4(bx)
  369. wait
  370. fstpd 2(bx)
  371. wait
  372. ret
  373. .cuf4:
  374. mov bx,sp
  375. cmp 2(bx),2
  376. jne 1f
  377. mov ax,4(bx)
  378. mov 2(bx),ax
  379. mov 4(bx),0
  380. wait
  381. fildl 2(bx)
  382. wait
  383. fstps 2(bx)
  384. wait
  385. ret
  386. 1:
  387. wait
  388. fildl 4(bx)
  389. wait
  390. cmp 6(bx),0
  391. jge 1f
  392. 2:
  393. wait
  394. fisubl (bigmin)
  395. wait
  396. fisubl (bigmin)
  397. 1:
  398. wait
  399. fstps 4(bx)
  400. wait
  401. ret
  402. .cuf8:
  403. mov bx,sp
  404. cmp 2(bx),2
  405. jne 1f
  406. mov 6(bx),0
  407. 1:
  408. wait
  409. fildl 4(bx)
  410. wait
  411. cmp 6(bx),0
  412. jge 1f
  413. 2:
  414. wait
  415. fisubl (bigmin)
  416. wait
  417. fisubl (bigmin)
  418. 1:
  419. wait
  420. fstpd 2(bx)
  421. wait
  422. ret
  423. .cfi:
  424. mov bx,sp
  425. push bx
  426. wait
  427. fstcw -2(bx)
  428. wait
  429. mov dx,-2(bx)
  430. or -2(bx),0xc00 ! truncating mode
  431. wait
  432. fldcw -2(bx)
  433. pop ax
  434. cmp 4(bx),4
  435. jne 2f
  436. ! loc 4 loc ? cfi
  437. wait
  438. flds 6(bx)
  439. wait
  440. fistpl 6(bx)
  441. wait
  442. cmp 2(bx),2
  443. jne 1f
  444. mov ax,6(bx)
  445. 1:
  446. mov 4(bx),dx
  447. wait
  448. fldcw 4(bx)
  449. wait
  450. ret
  451. 2:
  452. ! loc 8 loc ? cfi
  453. wait
  454. fldd 6(bx)
  455. wait
  456. fistpl 10(bx)
  457. wait
  458. cmp 2(bx),2
  459. jne 1b
  460. mov ax,10(bx)
  461. jmp 1b
  462. .cfu:
  463. mov bx,sp
  464. push bx
  465. wait
  466. fstcw -2(bx)
  467. wait
  468. mov dx,-2(bx)
  469. and -2(bx),0xf3ff
  470. or -2(bx),0x400 ! to -infinity
  471. wait
  472. fldcw -2(bx)
  473. wait
  474. pop ax
  475. cmp 4(bx),4
  476. jne 2f
  477. ! loc 4 loc ? cfu
  478. flds 6(bx)
  479. wait
  480. fabs ! ???
  481. wait
  482. fiaddl (bigmin)
  483. fistpl 6(bx)
  484. wait
  485. mov ax,8(bx)
  486. sub ax,(bigmin+2)
  487. mov 8(bx),ax
  488. cmp 2(bx),2
  489. jne 1f
  490. mov ax,6(bx)
  491. 1:
  492. mov 4(bx),dx
  493. wait
  494. fldcw 4(bx)
  495. wait
  496. ret
  497. 2:
  498. wait
  499. ! loc 8 loc ? cfu
  500. fldd 6(bx)
  501. wait
  502. fabs ! ???
  503. wait
  504. fiaddl (bigmin)
  505. fistpl 10(bx)
  506. wait
  507. mov ax,12(bx)
  508. sub ax,(bigmin+2)
  509. mov 12(bx),ax
  510. cmp 2(bx),2
  511. jne 1b
  512. mov ax,10(bx)
  513. jmp 1b
  514. .cff4:
  515. mov bx,sp
  516. wait
  517. fldd 2(bx)
  518. wait
  519. fstcw 2(bx)
  520. wait
  521. mov dx,2(bx)
  522. and 2(bx),0xf3ff ! set to rounding mode
  523. wait
  524. fldcw 2(bx)
  525. wait
  526. fstps 6(bx)
  527. mov 2(bx),dx
  528. wait
  529. fldcw 2(bx)
  530. wait
  531. ret
  532. .cff8:
  533. mov bx,sp
  534. wait
  535. flds 2(bx)
  536. wait
  537. fstpd 2(bx)
  538. wait
  539. ret
  540. .cmf4:
  541. mov bx,sp
  542. push bx ! room for 8087 status word
  543. xor cx,cx
  544. wait
  545. flds 6(bx)
  546. wait
  547. flds 2(bx)
  548. wait
  549. fcompp ! compare and pop operands
  550. wait
  551. fstsw -2(bx)
  552. wait
  553. mov ax,-2(bx)
  554. sahf
  555. je 1f
  556. jb 2f
  557. dec cx
  558. jmp 1f
  559. 2:
  560. inc cx
  561. 1:
  562. mov ax,cx
  563. pop bx
  564. ret
  565. .cmf8:
  566. mov bx,sp
  567. push bx ! room for 8087 status word
  568. xor cx,cx
  569. wait
  570. fldd 10(bx)
  571. wait
  572. fldd 2(bx)
  573. wait
  574. fcompp ! compare and pop operands
  575. wait
  576. fstsw -2(bx)
  577. wait
  578. mov ax,-2(bx)
  579. sahf
  580. je 1f
  581. jb 2f
  582. dec cx
  583. jmp 1f
  584. 2:
  585. inc cx
  586. 1:
  587. mov ax,cx
  588. pop bx
  589. ret