fp8087.s 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. .data4 -2147483648
  15. .sect .text
  16. .adf4:
  17. mov bx,sp
  18. flds 4(bx)
  19. fadds 8(bx)
  20. fstps 8(bx)
  21. wait
  22. ret
  23. .adf8:
  24. mov bx,sp
  25. fldd 4(bx)
  26. faddd 12(bx)
  27. fstpd 12(bx)
  28. wait
  29. ret
  30. .sbf4:
  31. mov bx,sp
  32. flds 8(bx)
  33. fsubs 4(bx)
  34. fstps 8(bx)
  35. wait
  36. ret
  37. .sbf8:
  38. mov bx,sp
  39. fldd 12(bx)
  40. fsubd 4(bx)
  41. fstpd 12(bx)
  42. wait
  43. ret
  44. .mlf4:
  45. mov bx,sp
  46. flds 4(bx)
  47. fmuls 8(bx)
  48. fstps 8(bx)
  49. wait
  50. ret
  51. .mlf8:
  52. mov bx,sp
  53. fldd 4(bx)
  54. fmuld 12(bx)
  55. fstpd 12(bx)
  56. wait
  57. ret
  58. .dvf4:
  59. mov bx,sp
  60. flds 8(bx)
  61. fdivs 4(bx)
  62. fstps 8(bx)
  63. wait
  64. ret
  65. .dvf8:
  66. mov bx,sp
  67. fldd 12(bx)
  68. fdivd 4(bx)
  69. fstpd 12(bx)
  70. wait
  71. ret
  72. .ngf4:
  73. mov bx,sp
  74. flds 4(bx)
  75. fchs
  76. fstps 4(bx)
  77. wait
  78. ret
  79. .ngf8:
  80. mov bx,sp
  81. fldd 4(bx)
  82. fchs
  83. fstpd 4(bx)
  84. wait
  85. ret
  86. .fif4:
  87. mov bx,sp
  88. flds 8(bx)
  89. fmuls 12(bx) ! multiply
  90. fld st ! copy result
  91. ftst ! test sign; handle negative separately
  92. fstsw ax
  93. wait
  94. sahf ! result of test in condition codes
  95. jb 1f
  96. frndint ! this one rounds (?)
  97. fcom st(1) ! compare with original; if <=, then OK
  98. fstsw ax
  99. wait
  100. sahf
  101. jbe 2f
  102. fisubs (one) ! else subtract 1
  103. jmp 2f
  104. 1: ! here, negative case
  105. frndint ! this one rounds (?)
  106. fcom st(1) ! compare with original; if >=, then OK
  107. fstsw ax
  108. wait
  109. sahf
  110. jae 2f
  111. fiadds (one) ! else add 1
  112. 2:
  113. fsub st(1),st ! subtract integer part
  114. mov bx,4(bx)
  115. fstps (bx)
  116. fstps 4(bx)
  117. wait
  118. ret
  119. .fif8:
  120. mov bx,sp
  121. fldd 8(bx)
  122. fmuld 16(bx) ! multiply
  123. fld st ! and copy result
  124. ftst ! test sign; handle negative separately
  125. fstsw ax
  126. wait
  127. sahf ! result of test in condition codes
  128. jb 1f
  129. frndint ! this one rounds (?)
  130. fcom st(1) ! compare with original; if <=, then OK
  131. fstsw ax
  132. wait
  133. sahf
  134. jbe 2f
  135. fisubs (one) ! else subtract 1
  136. jmp 2f
  137. 1: ! here, negative case
  138. frndint ! this one rounds (?)
  139. fcom st(1) ! compare with original; if >=, then OK
  140. fstsw ax
  141. wait
  142. sahf
  143. jae 2f
  144. fiadds (one) ! else add 1
  145. 2:
  146. fsub st(1),st ! subtract integer part
  147. mov bx,4(bx)
  148. fstpd (bx)
  149. fstpd 8(bx)
  150. wait
  151. ret
  152. .fef4:
  153. ! this could be simpler, if only the
  154. ! fxtract instruction was emulated properly
  155. mov bx,sp
  156. mov ax,8(bx)
  157. and ax,0x7f800000
  158. je 1f ! zero exponent
  159. shr ax,23
  160. sub ax,126
  161. mov cx,ax ! exponent in cx
  162. mov ax,8(bx)
  163. and ax,0x807fffff
  164. or ax,0x3f000000 ! load -1 exponent
  165. mov bx,4(bx)
  166. mov 4(bx),ax
  167. mov (bx),cx
  168. ret
  169. 1: ! we get here on zero exp
  170. mov ax,8(bx)
  171. and ax,0x007fffff
  172. jne 1f ! zero result
  173. mov bx,4(bx)
  174. mov (bx),ax
  175. mov 4(bx),ax
  176. ret
  177. 1: ! otherwise unnormalized number
  178. mov cx,8(bx)
  179. and cx,0x807fffff
  180. mov dx,cx
  181. and cx,0x80000000
  182. mov ax,-125
  183. 2:
  184. test dx,0x800000
  185. jne 1f
  186. dec ax
  187. shl dx,1
  188. or dx,cx
  189. jmp 2b
  190. 1:
  191. mov bx,4(bx)
  192. mov (bx),ax
  193. and dx,0x807fffff
  194. or dx,0x3f000000 ! load -1 exponent
  195. mov 4(bx),dx
  196. ret
  197. .fef8:
  198. ! this could be simpler, if only the
  199. ! fxtract instruction was emulated properly
  200. mov bx,sp
  201. mov ax,12(bx)
  202. and ax,0x7ff00000
  203. je 1f ! zero exponent
  204. shr ax,20
  205. sub ax,1022
  206. mov cx,ax ! exponent in cx
  207. mov ax,12(bx)
  208. and ax,0x800fffff
  209. or ax,0x3fe00000 ! load -1 exponent
  210. mov dx,8(bx)
  211. mov bx,4(bx)
  212. mov 4(bx),dx
  213. mov 8(bx),ax
  214. mov (bx),cx
  215. ret
  216. 1: ! we get here on zero exp
  217. mov ax,12(bx)
  218. and ax,0xfffff
  219. or ax,8(bx)
  220. jne 1f ! zero result
  221. mov bx,4(bx)
  222. mov (bx),ax
  223. mov 4(bx),ax
  224. mov 8(bx),ax
  225. ret
  226. 1: ! otherwise unnormalized number
  227. mov cx,12(bx)
  228. and cx,0x800fffff
  229. mov dx,cx
  230. and cx,0x80000000
  231. mov ax,-1021
  232. 2:
  233. test dx,0x100000
  234. jne 1f
  235. dec ax
  236. shl 8(bx),1
  237. rcl dx,1
  238. or dx,cx
  239. jmp 2b
  240. 1:
  241. and dx,0x800fffff
  242. or dx,0x3fe00000 ! load -1 exponent
  243. mov cx,8(bx)
  244. mov bx,4(bx)
  245. mov (bx),ax
  246. mov 8(bx),dx
  247. mov 4(bx),cx
  248. ret
  249. .cif4:
  250. mov bx,sp
  251. fildl 8(bx)
  252. fstps 8(bx)
  253. wait
  254. ret
  255. .cif8:
  256. mov bx,sp
  257. fildl 8(bx)
  258. fstpd 4(bx)
  259. wait
  260. ret
  261. .cuf4:
  262. mov bx,sp
  263. fildl 8(bx)
  264. cmp 8(bx),0
  265. jge 1f
  266. fisubl (bigmin)
  267. fisubl (bigmin)
  268. 1:
  269. fstps 8(bx)
  270. wait
  271. ret
  272. .cuf8:
  273. mov bx,sp
  274. fildl 8(bx)
  275. cmp 8(bx),0
  276. jge 1f
  277. fisubl (bigmin)
  278. fisubl (bigmin)
  279. 1:
  280. fstpd 4(bx)
  281. wait
  282. ret
  283. .cfi:
  284. mov bx,sp
  285. fstcw 4(bx)
  286. wait
  287. mov dx,4(bx)
  288. or 4(bx),0xc00 ! truncating mode
  289. wait
  290. fldcw 4(bx)
  291. cmp 8(bx),4
  292. jne 2f
  293. ! loc 4 loc ? cfi
  294. flds 12(bx)
  295. fistpl 12(bx)
  296. 1:
  297. mov 4(bx),dx
  298. wait
  299. fldcw 4(bx)
  300. ret
  301. 2:
  302. ! loc 8 loc ? cfi
  303. fldd 12(bx)
  304. fistpl 16(bx)
  305. jmp 1b
  306. .cfu:
  307. mov bx,sp
  308. fstcw 4(bx)
  309. wait
  310. mov dx,4(bx)
  311. and 4(bx),0xf3ff
  312. or 4(bx),0x400 ! to -infinity
  313. wait
  314. fldcw 4(bx)
  315. cmp 8(bx),4
  316. jne 2f
  317. ! loc 4 loc ? cfu
  318. flds 12(bx)
  319. fabs ! ???
  320. fiaddl (bigmin)
  321. fistpl 12(bx)
  322. wait
  323. mov ax,12(bx)
  324. sub ax,(bigmin)
  325. mov 12(bx),ax
  326. 1:
  327. mov 4(bx),dx
  328. wait
  329. fldcw 4(bx)
  330. ret
  331. 2:
  332. ! loc 8 loc ? cfu
  333. fldd 12(bx)
  334. fabs ! ???
  335. fiaddl (bigmin)
  336. fistpl 16(bx)
  337. wait
  338. mov ax,16(bx)
  339. sub ax,(bigmin)
  340. mov 16(bx),ax
  341. jmp 1b
  342. .cff4:
  343. mov bx,sp
  344. fldd 4(bx)
  345. fstcw 4(bx)
  346. wait
  347. mov dx,4(bx)
  348. and 4(bx),0xf3ff ! set to rounding mode
  349. wait
  350. fldcw 4(bx)
  351. fstps 8(bx)
  352. mov 4(bx),dx
  353. wait
  354. fldcw 4(bx)
  355. wait
  356. ret
  357. .cff8:
  358. mov bx,sp
  359. flds 4(bx)
  360. fstpd 4(bx)
  361. wait
  362. ret
  363. .cmf4:
  364. mov bx,sp
  365. xor cx,cx
  366. flds 8(bx)
  367. flds 4(bx)
  368. fcompp ! compare and pop operands
  369. fstsw ax
  370. wait
  371. sahf
  372. je 1f
  373. jb 2f
  374. dec cx
  375. jmp 1f
  376. 2:
  377. inc cx
  378. 1:
  379. mov ax,cx
  380. ret
  381. .cmf8:
  382. mov bx,sp
  383. xor cx,cx
  384. fldd 12(bx)
  385. fldd 4(bx)
  386. fcompp ! compare and pop operands
  387. fstsw ax
  388. wait
  389. sahf
  390. je 1f
  391. jb 2f
  392. dec cx
  393. jmp 1f
  394. 2:
  395. inc cx
  396. 1:
  397. mov ax,cx
  398. ret