home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / float.c < prev    next >
C/C++ Source or Header  |  1993-01-19  |  7KB  |  431 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                     This file is not copyrighted!                      */
  3. /* ---------------------------------------------------------------------- */
  4. #include "defines.h"
  5. #include "nasm.h"
  6.  
  7. /* ---------------------------------------------------------- */
  8. /*  This is from the ATARI BASIC Source Book reverse          */
  9. /*  engineered. OK. Since only mathematicians would be        */
  10. /*  interested in optimizing this code, and knowing their     */
  11. /*  humorlessness let me explain: "This is a 9K joke!"        */
  12. /*  **-PORT #6-**                                             */
  13. /* ---------------------------------------------------------- */
  14.  
  15. #define _fprec     6
  16. #define _fmprec    _fprec - 1
  17.  
  18. static signed char   a, x, y;
  19. static signed char   c, t;
  20. signed char          page0[ 0x100];
  21. static signed char   page1[ 0x100];
  22. static unsigned char p1_sp = 0xFF;
  23. static signed char   *inbuff;
  24.  
  25. #define _fr0     0xD4U
  26. #define _fr0m    0xD5U
  27. #define _fre     0xDAU
  28. #define _frx     0xECU
  29. #define _eexp    0xEDU
  30. #define _nsign   0xEEU
  31. #define _esign   0xEFU
  32. #define _fchrflg 0xF0U
  33. #define _digrt   0xF1U
  34. #define _cix     0xF2U
  35.  
  36. #define fr0      page0[ _fr0]
  37. #define fr0m     page0[ _fr0m]
  38. #define fre      page0[ _fre]
  39. #define frx      page0[ _frx]
  40. #define eexp     page0[ _eexp]
  41. #define nsign    page0[ _nsign]
  42. #define esign    page0[ _esign]
  43. #define fchrflg  page0[ _fchrflg]
  44. #define digrt    page0[ _digrt]
  45. #define cix      page0[ _cix]
  46.  
  47.  
  48. #define pha()  page1[ --p1_sp ] = a
  49. #define pla()  a = page1[ p1_sp++]
  50. #define bcc(v) if( ! c) goto v
  51. #define bcs(v) if( c)   goto v
  52. #define clc()  c = 0
  53. #define sec()  c = 1
  54. #define rts()  return
  55. #define jmp(v) goto  v;
  56. #define jsr(v) v()
  57. #define lda(v) a = v
  58. #define ldx(v) x = v
  59. #define ldy(v) y = v
  60. #define sta(v) v = a
  61. #define stx(v) v = x
  62. #define sty(v) v = y
  63. #define iny()  ++y
  64. #define dey()  --y
  65. #define inx()  ++x
  66. #define dex()  --x
  67. #define ora(v) a |= v
  68. #define eor(v) a ^=  v
  69. #define and(v) a &= v
  70. #define inc(v) ++v
  71. #define dec(v) --v
  72. #define tax()  x = a
  73. #define tay()  y = a
  74. #define txa()  a = x
  75. #define tya()  y = a
  76. #define cmp(v) c = (( a - v) >= 0)
  77. #define asl(v) c = (v) & 0x80; v <<= 1
  78. #define lsr(v) c = (v) & 0x01; v >>= 1
  79. #define rol(v) t = c; c = ((v) & 0x80) != 0; (v) = ((v) << 1) | t
  80. #define ror(v) t = c; c = (v) & 0x01; (v) = ((v) >> 1) | (t ? 0x80 : 0)
  81. #define adc(v)                      \
  82.    if( ((int) a + v + c) >= 0x100)  \
  83.    {                                \
  84.       a += v + c;                   \
  85.       c = 1;                        \
  86.    }                                \
  87.    else                             \
  88.    {                                \
  89.       a += v + c;                   \
  90.       c = 0;                        \
  91.    }
  92. #define sbc(v)                      \
  93.    if( ((int) a - v - ! c ) < 0)    \
  94.    {                                \
  95.       a -= v - ! c;                 \
  96.       c = 0;                        \
  97.    }                                \
  98.    else                             \
  99.    {                                \
  100.       a -= v - ! c;                 \
  101.       c = 1;                        \
  102.    }
  103.  
  104. void  ab_tstnum()
  105. {
  106.    ldy( cix);
  107.    lda( inbuff[y]);
  108.    sec();
  109.    sbc( 0x30);
  110.    bcc( tsnfail1);
  111.    c = ((a - 0xA) >= 0);
  112.    rts();
  113.  
  114. tsnfail1:
  115.    sec();
  116.    rts();
  117. }
  118.  
  119. void ab_getchar()
  120. {
  121.    jsr( ab_tstnum);
  122.    ldy( cix);
  123.    bcc( _gchr1);
  124.    lda( inbuff[y]);
  125. _gchr1:
  126.    jsr( ab_gchr1);
  127. }
  128.  
  129. void ab_gchr1()
  130. {
  131.    iny();
  132.    sty( cix);
  133.    rts();
  134. }
  135.  
  136. void  ab_tstchar()
  137. {
  138.    lda( cix);
  139.    pha();
  140.    jsr( ab_getchar);
  141.    bcc( rtpass);
  142.    if( a == '.')
  143.       goto tstn;
  144.    if( a == '+')
  145.       goto tstn1;
  146.    if( a == '-')
  147.       goto tstn1;
  148.  
  149. rtfail:
  150.    pla();
  151.    sec();
  152.    rts();
  153.  
  154. tstn1:
  155.    jsr( ab_getchar);
  156.    bcc( rtpass);
  157.    if( a != '.')
  158.       goto rtfail;
  159.  
  160. tstn:
  161.    jsr( ab_getchar);
  162.    bcc( rtpass);
  163.    bcs( rtfail);
  164.  
  165. rtpass:
  166.    pla();
  167.    sta( cix);
  168.    clc();
  169.    rts();
  170. }
  171.  
  172. void ab_nibsh0()
  173. {
  174.    ldx( _fr0m);
  175.    ldy( 4);
  176. nibs:
  177.    clc();
  178.    rol( page0[ 4 + (byte) x]);
  179.    rol( page0[ 3 + (byte) x]);
  180.    rol( page0[ 2 + (byte) x]);
  181.    rol( page0[ 1 + (byte) x]);
  182.    rol( page0[ (byte) x]);
  183.    rol( frx);
  184.    if( dey())
  185.       goto nibs;
  186.    rts();
  187. }
  188.  
  189. void ab_norm()
  190. {
  191.    ldx(0);
  192.    stx( page0[ _fr0 + _fprec]);
  193.    ldx( _fmprec - 1);
  194.    if( ! (lda( fr0)))
  195.       goto ndone;
  196. norm:
  197.    if( lda( fr0m))
  198.       goto tstbig;
  199.  
  200.    ldy(0);
  201. nsh:
  202.    lda( page0[ _fr0m + 1 + (byte) y]);
  203.    sta( page0[ _fr0m + (byte) y]);
  204.    iny();
  205.    if( y < _fmprec)
  206.       goto nsh;
  207.  
  208.    dec( fr0);
  209.    if( dex())
  210.       goto norm;
  211.  
  212.    if( ! (lda( fr0m)))
  213.       goto tstbig;
  214.    sta( fr0);
  215.    clc();
  216.    rts();
  217.  
  218. tstbig:
  219.    lda( fr0);
  220.    and( 0x7F);
  221.    if( a < 49 + 64)
  222.       goto tstund;
  223.    rts();
  224.  
  225. tstund:
  226.    if( a >= -49 + 64)
  227.       goto ndone;
  228.    jsr( ab_zfr0);
  229.  
  230. ndone:
  231.    clc();
  232.    rts();
  233. }
  234.  
  235. void ab_zfr0()
  236. {
  237.    ldx( _fr0);
  238.    ldy( 6);
  239.    jsr( ab_zxly);
  240. }
  241.  
  242. void ab_zxly()
  243. {
  244.    lda( 0);
  245.  
  246. zf2:
  247.    sta( page0[ (byte) x]);
  248.    inx();
  249.    if( dey())
  250.       goto zf2;
  251.    rts();
  252. }
  253.  
  254. int  ab_ascin( s)
  255. signed char  *s;
  256. {
  257.    inbuff = s;
  258.    cix    = 0;
  259.  
  260.  
  261.    jsr( ab_tstchar);
  262.    bcs( nonum);
  263.  
  264.    ldx( _eexp);
  265.    ldy( 4);
  266.    jsr( ab_zxly);
  267.  
  268.    ldx( 0xFF);
  269.    stx( digrt);
  270.    jsr( ab_zfr0);
  271.    jmp( in2);
  272.  
  273. in1:
  274.    lda( 0xFF);
  275.    sta( fchrflg);
  276. in2:
  277.    jsr( ab_getchar);
  278.    bcs( non1);
  279.  
  280.    pha();                                    /* it's a number */
  281.    if( ldx( fr0m))                           /* get 1st byte  */
  282.       goto ince;                             /* incr exponent */
  283.  
  284.    jsr( ab_nibsh0);                          /* shift fr0 one nibble left */
  285.  
  286.    pla();
  287.    ora( page0[ _fr0m + _fmprec - 1]);
  288.    sta( page0[ _fr0m + _fmprec - 1]);
  289.  
  290.    if( (ldx( digrt)) < 0)
  291.       goto in1;
  292.    inx();
  293.    if( stx( digrt))
  294.       goto in1;
  295.  
  296. ince:
  297.    pla();
  298.    if( (ldx( digrt)) >= 0)
  299.       goto ince2;
  300.    inc( eexp);
  301.  
  302. ince2:
  303.    jmp( in1);
  304.  
  305. nonum:
  306.    return( (int) c);
  307.  
  308. non1:
  309.    if( a == '.')
  310.       goto dp;
  311.    if( a == 'E')
  312.       goto exp;
  313.  
  314.    if( ldx( fchrflg))
  315.       goto exit;
  316.    if( a == '+')
  317.       goto in1;
  318.    if( a == '-')
  319.       goto minus;    /* it's unbelievable */
  320.  
  321. minus:
  322.    if( ! (sta( nsign)))
  323.       goto in1;
  324.  
  325. dp:
  326.    if( (ldx( digrt)) >= 0)
  327.       goto exit;
  328.    inx();
  329.    if( ! (stx( digrt)))
  330.       goto in1;
  331.  
  332. exp:
  333.    lda( cix);
  334.    sta( frx);
  335.    jsr( ab_getchar);
  336.    bcs( non2);
  337.  
  338. exp2:
  339.    tax();
  340.    lda( eexp);
  341.    pha();
  342.    stx( eexp);
  343.    jsr( ab_getchar);
  344.  
  345.    bcs( exp3);
  346.    pha();
  347.  
  348.    lda( eexp);
  349.    asl( a);
  350.    sta( eexp);
  351.    asl( a);
  352.    asl( a);
  353.    adc( eexp);
  354.    sta( eexp);
  355.    pla();
  356.    clc();
  357.    adc( eexp);
  358.  
  359.    ldy( cix);
  360.    jsr( ab_gchr1);
  361.  
  362. exp3:
  363.    if( ! (lda( esign)))
  364.       goto exp1;
  365.    lda ( eexp);
  366.    eor( 0xFF);
  367.    clc();
  368.    adc( 1);
  369.    sta( eexp);
  370.  
  371. exp1:
  372.    pla();
  373.    clc();
  374.    adc( eexp);
  375.    if( sta( eexp))
  376.       goto exit;
  377.  
  378. non2:
  379.    if( a == '+')
  380.       goto eplus;
  381.    if( a != '-')
  382.       goto note;
  383.  
  384.    sta( esign);
  385. eplus:
  386.    jsr( ab_getchar);
  387.    bcc( exp2);
  388.  
  389. note:
  390.    lda( frx);
  391.    sta( cix);
  392.  
  393. exit:
  394.    dec( cix);
  395.  
  396.    lda( eexp);
  397.    if( (ldx( digrt)) <= 0)
  398.       goto exit1;
  399.    sec();
  400.    sbc( digrt);
  401.  
  402. exit1:
  403.    pha();
  404.    rol( a);
  405.    pla();
  406.    ror( a);
  407.    sta( eexp);
  408.    bcc( even);
  409.  
  410.    jsr( ab_nibsh0);
  411. even:
  412.    lda( eexp);
  413.    clc();
  414.    adc( 0x44);
  415.    sta( fr0);
  416.  
  417.    jsr( ab_norm);
  418.    bcs( ind2);
  419.  
  420.    if( ! (ldx( nsign)))
  421.       goto indon;
  422.    lda( fr0);
  423.    ora( 0x80);
  424.    sta( fr0);
  425. indon:
  426.    clc();
  427. ind2:
  428.    return( c);
  429. }
  430.  
  431.