home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / ubppmpqs.zip / ppmp6.asm < prev    next >
Encoding:
Assembly Source File  |  1992-05-10  |  9.6 KB  |  719 lines

  1. ;PPMP6.ASM 2.0
  2. ;    MACHINE LANGUAGE SUBROUTINES
  3. ;    FOR PPMPQS
  4. ;    1991/92 by YUJI KIDA
  5. ;
  6. .386p
  7.  
  8. code    segment    use16
  9.     assume    cs:code,ds:code
  10.  
  11.  
  12.     org    100h
  13. start:
  14.  
  15.     INCLUDE    UBP.MAC
  16.     include    ppmp.h
  17.  
  18.  
  19.     JMP    start0
  20.  
  21.   align    4
  22. XMEMsize        dd    ?
  23. ROWSbase        dd    ?
  24. decompdatabase        dd    ?
  25. decomppointerbase    dd    ?
  26. historybase        dd    ?
  27. freebase        dd    ?
  28.  
  29. totaloperations        dd    ?
  30.  
  31. P_OFF        dw    ?
  32. P_SEG        dw    ?
  33.  
  34. E_OFF        dw    ?
  35. E_SEG        dw    ?
  36.  
  37. Y_OFF        dw    ?
  38. Y_SEG        dw    ?
  39.  
  40. DRV_OFF        dw    ?
  41. DRV_SEG        dw    ?
  42.  
  43. primes        dw    ?
  44. FBsize        dw    ?
  45. Rmatrixsize    dw    ?
  46.  
  47. primehandle    dw    ?
  48. ROWShandle    dw    ?
  49. decomphandle    dw    ?
  50. anshandle    dw    ?
  51. histhandle    dw    ?
  52.  
  53. databuffer    dw    ?
  54. ans_seg        dw    ?
  55.  
  56. Rbytes        dw    ?
  57. Rwords        dw    ?
  58.  
  59. Tbytes        dw    ?
  60. Twords        dw    ?
  61. Tdwords        dw    ?
  62.  
  63. spmem        dw    ?
  64.  
  65.  
  66. ;branch
  67.  
  68. START0:
  69.     MOV_AX    AR0        ;ARRAY[0] is the command number
  70.     MOV    BX,OFFSET CMD_TBL
  71.     SHL    AX,1
  72.     ADD    BX,AX
  73.     JMP    CS:[BX]
  74.  
  75. CMD_TBL:
  76.     DW    final_init,square_ans,putPandE,closefiles
  77.  
  78. ;
  79. ; close files
  80. ;
  81. ;command#=3
  82.  
  83. closefiles:
  84.     mov    ah,3eh
  85.     mov    bx,cs:[anshandle]
  86.     or    bx,bx
  87.     jz    short closefnext
  88.     int    21h
  89. closefnext:
  90. consefret:
  91.     retf
  92.  
  93. ;
  94. ; answer exponents of p
  95. ;
  96. ;command#=2
  97.  
  98. putPandE:
  99.     mov    ax,cs
  100.     mov    ds,ax
  101.     mov    es,ax
  102.  
  103.     mov    ebx,primeadr+12        ;exp part of 2
  104.     mov    cx,[primes]
  105. putPE10:
  106.     mov    eax,fs:[ebx]
  107.     or    eax,eax
  108.     jnz    short putPE100
  109.     add    ebx,8
  110.     loop    putPE10
  111.  
  112.     lds    si,dword ptr [P_OFF]
  113.     mov    word ptr [si],0        ;set 0 = no more data
  114.  
  115. putPEret:
  116.     mov    ax,ss
  117.     mov    ds,ax
  118.     mov    es,ax
  119.     retf
  120.  
  121. putPE100:
  122.     mov    dword ptr fs:[ebx],0    ;clear
  123.     lds    si,dword ptr [E_OFF]
  124.     call    seteax
  125.     mov    eax,fs:[ebx-4]
  126.     lds    si,dword ptr cs:[P_OFF]
  127.     call    seteax
  128.     jmp    putPEret
  129.  
  130. seteax:
  131.     mov    cx,2
  132.     cmp    eax,10000h
  133.     jae    short seteaxjp
  134.     dec    cx
  135.     or    ax,ax
  136.     jnz    short seteaxjp
  137.     dec    cx
  138. seteaxjp:
  139.     mov    [si],cx
  140.     mov    [si+2],eax
  141.     ret    
  142.     
  143.  
  144. ;
  145. ; answer square data
  146. ;
  147. ;command#=1
  148.  
  149. square_ans:
  150.     mov    ax,cs
  151.     mov    ds,ax
  152.     mov    es,ax
  153.     mov    [spmem],sp
  154.  
  155. ans_again:
  156.     mov    dx,offset BUFFER    ;read answer
  157.     mov    cx,[Rbytes]
  158.     mov    bx,[anshandle]
  159.     mov    ah,3fh
  160.     int    21h
  161.     or    ax,ax
  162.     jz    nomoreanswer
  163.  
  164.     add    ax,offset BUFFER
  165.     add    ax,3
  166.     and    ax,0fffch
  167.     mov    [databuffer],ax
  168.  
  169.     call    translate
  170.     call    backsubstitution
  171.  
  172.     mov    si,[databuffer]
  173.     mov    cx,[Tbytes]
  174.     mov    di,si
  175.     add    di,cx
  176.     sub    di,2        ;di = highest word adr
  177.     shr    cx,1
  178.  
  179.     xor    ax,ax
  180.     std
  181.     repe    scasw
  182.     cld
  183.  
  184.     je    ans_again    ;illegal answer
  185.  
  186.     inc    cx        ;word length
  187.  
  188.     push    es
  189.     mov    es,[ans_seg]
  190.     xor    di,di
  191.     mov    ax,cx
  192.     stosw            ;word length
  193.     rep    movsw
  194.     pop    es
  195.  
  196.     call    countexponents
  197.  
  198. squareret:
  199.     mov    ax,ss
  200.     mov    ds,ax
  201.     mov    es,ax
  202.     retf
  203.  
  204. nomoreanswer:
  205.     mov    bx,AR0
  206.     mov    word ptr [bx],0
  207.     jmp    squareret
  208.  
  209.  
  210. translate:
  211.     mov    di,[databuffer]
  212.     mov    cx,[Tdwords]
  213.     xor    eax,eax
  214.     rep    stosd
  215.  
  216.     mov    ebx,[ROWSbase]
  217.     mov    si,offset BUFFER
  218.     mov    cx,[Rwords]
  219. trans10:
  220.     push    cx
  221.     lodsw
  222.     mov    cx,16
  223. trans20:
  224.     shr    ax,1
  225.     jnc    short trans30
  226.  
  227.     push    cx
  228.     push    ax
  229.  
  230.     mov    di,fs:[ebx]
  231.     mov    cx,di
  232.     and    cl,7
  233.     shr    di,3
  234.     mov    al,1
  235.     shl    al,cl
  236.     add    di,[databuffer]
  237.     or    [di],al
  238.  
  239.     pop    ax
  240.     pop    cx
  241.  
  242. trans30:
  243.     add    ebx,2
  244.     loop    trans20
  245.  
  246.     pop    cx
  247.     loop    trans10
  248.     ret
  249.  
  250.  
  251. backsubstitution:
  252.     mov    bx,[databuffer]
  253.     mov    edx,[totaloperations]
  254.     mov    esi,edx
  255.     dec    esi
  256.     shl    esi,2
  257.     add    esi,[historybase]
  258.   align    4
  259. backsubst10:
  260.     mov    edi,fs:[esi]
  261.     mov    cx,di
  262.     and    cl,7
  263.     shr    di,3
  264.     mov    al,1
  265.     shl    al,cl
  266.     test    [di+bx],al
  267.     jnz    short backsubst30
  268. backsubst20:
  269.     sub    esi,4
  270.     dec    edx
  271.     jnz    backsubst10
  272.     ret
  273.  
  274.   align    4
  275. backsubst30:
  276.     shr    edi,16            ;source
  277.     mov    cx,di
  278.     and    cl,7
  279.     shr    di,3
  280.     mov    al,1
  281.     shl    al,cl
  282.     xor    [di+bx],al
  283.     jmp    backsubst20
  284.  
  285.  
  286. countexponents:
  287.     mov    edi,primeadr+4        ;adr of exponents
  288.     mov    cx,[FBsize]
  289.     xor    eax,eax
  290. ctexp10:
  291.     mov    fs:[edi],eax        ;clear exponents
  292.     add    edi,8
  293.     loop    ctexp10
  294.  
  295.     mov    ebx,[decomppointerbase]
  296.     mov    si,[databuffer]
  297.     mov    cx,[Twords]
  298. ctexp20:
  299.     push    cx
  300.     lodsw
  301.     mov    cx,16
  302. ctexp30:
  303.     shr    ax,1
  304.     jc    short ctexp100
  305. ctexp40:
  306.     add    ebx,4
  307.     loop    ctexp30
  308.  
  309.     pop    cx
  310.     loop    ctexp20
  311.     ret
  312.  
  313. ctexp100:
  314.     push    ax
  315.     push    cx
  316.  
  317.     mov    edi,fs:[ebx]
  318.     mov    cx,fs:[edi]
  319.   align    4
  320. ctexp110:
  321.     add    edi,2
  322.     movzx    eax,word ptr fs:[edi]
  323.     shl    eax,3            ;*8
  324.     add    eax,primeadr+4
  325.     inc    dword ptr fs:[eax]    ;inc exponents
  326.     loop    ctexp110
  327.  
  328.     pop    cx
  329.     pop    ax
  330.     jmp    ctexp40
  331.  
  332.     
  333. ;
  334. ;* Initialize
  335. ;
  336. ;COMMAND#=0
  337. ;
  338.  
  339. final_init:
  340.     ;get parameters
  341.  
  342.     mov    ax,cs
  343.     mov    ds,ax
  344.     mov    es,ax
  345.     mov    [spmem],sp
  346.  
  347.     MOV_AX    V1+2        ;memo the segment of ans%(0)
  348.     ADD    AX,ARRAYHEADSEG
  349.     MOV    [ANS_SEG],AX
  350.  
  351.     MOV    BX,V2        ;memo the address of P
  352.     MOV    AX,[BX]
  353.     MOV    [P_OFF],AX
  354.     MOV    AX,[BX+2]
  355.     MOV    [P_SEG],AX
  356.  
  357.     MOV    BX,V3        ;memo the address of E
  358.     MOV    AX,[BX]
  359.     MOV    [E_OFF],AX
  360.     MOV    AX,[BX+2]
  361.     MOV    [E_SEG],AX
  362.  
  363.     MOV    BX,V4        ;memo the address of Y
  364.     MOV    AX,[BX]
  365.     MOV    [Y_OFF],AX
  366.     MOV    AX,[BX+2]
  367.     MOV    [Y_SEG],AX
  368.  
  369.     MOV    BX,V5        ;memo the address of DataDrv$
  370.     MOV    AX,[BX]
  371.     MOV    [DRV_OFF],AX
  372.     MOV    AX,[BX+2]
  373.     MOV    [DRV_SEG],AX
  374.  
  375.     xor    eax,eax
  376.     mov_ax    AR1
  377.     shl    eax,10
  378.     mov    [XMEMsize],eax
  379.  
  380.     mov_ax    AR2
  381.     shl    ax,1
  382.     mov    [FBsize],ax
  383.     dec    ax
  384.     mov    [primes],ax
  385.     inc    ax
  386.     shr    ax,3            ;/ 8
  387.     mov    [Tbytes],ax
  388.     shr    ax,1
  389.     mov    [Twords],ax
  390.     shr    ax,1
  391.     mov    [Tdwords],ax
  392.  
  393.     call    setPROTECTfs
  394.  
  395.     call    openANSFILE
  396.     call    getPRIMES
  397.     call    getROWS
  398.     call    getDECOMPDATA
  399.     call    gethistDATA
  400.     mov    eax,[XMEMsize]
  401.     add    eax,pmodeworkadr
  402.     cmp    eax,[freebase]
  403.     jb    short lackofmemory
  404. initout:
  405.     mov    ax,ss
  406.     mov    ds,ax
  407.     mov    es,ax
  408.     retf
  409.  
  410. diskerror:
  411.     mov    ax,8001h
  412. initerror:
  413.     mov    ds:[AR0],ax
  414.     mov    sp,[spmem]
  415.     jmp    initout
  416.  
  417. lackofmemory:
  418.     mov    ax,8002h
  419.     jmp    initerror
  420.  
  421.  
  422. openANSFILE:
  423.     mov    dx,offset ansfilename    ;read matrix size
  424.     mov    ax,3d00h        ;from answer file
  425.     int    21h
  426.     jc    diskerror
  427.     mov    [anshandle],ax
  428.  
  429.     mov    dx,offset Rmatrixsize    ;get matrixsize
  430.     mov    cx,2
  431.     mov    bx,ax
  432.     mov    ah,3fh            ;read
  433.     int    21h
  434.     jc    diskerror
  435.  
  436.     mov    ax,[Rmatrixsize]
  437.     shr    ax,3
  438.     mov    [Rbytes],ax
  439.     shr    ax,1
  440.     mov    [Rwords],ax
  441.     ret
  442.  
  443.  
  444. gethistDATA:
  445.     mov    ax,3d00h        ;open for read
  446.     mov    dx,offset historyfilename
  447.     int    21h
  448.     jc    diskerror
  449.     mov    [histhandle],ax
  450.  
  451.     mov    edi,[historybase]
  452. gethist10:
  453.     mov    dx,offset BUFFER
  454.     mov    cx,400h
  455.     mov    bx,[histhandle]
  456.     mov    ah,3fh
  457.     int    21h
  458.     or    ax,ax
  459.     jz    short gethisteof
  460.  
  461.     mov    cx,ax
  462.     shr    cx,2
  463.     mov    si,offset BUFFER
  464. gethist20:
  465.     lodsd
  466.     mov    fs:[edi],eax
  467.     add    edi,4
  468.     loop    gethist20
  469.     jmp    gethist10
  470.  
  471. gethisteof:
  472.     mov    [freebase],edi
  473.     sub    edi,[historybase]
  474.     shr    edi,2            ;4bytes/unit
  475.     mov    [totaloperations],edi
  476.  
  477.     mov    ah,3eh            ;close file
  478.     mov    bx,[histhandle]
  479.     int    21h
  480.     ret
  481.  
  482.  
  483. getDECOMPDATA:
  484.     mov    si,offset decompfilename
  485.     call    setpathname
  486.     mov    dx,offset BUFFER
  487.     mov    ax,3d00h        ;open for read
  488.     int    21h
  489.     jc    diskerror
  490.     mov    [decomphandle],ax
  491.  
  492.     mov    edi,[decompdatabase]
  493. getdecomp10:
  494.     mov    dx,offset BUFFER
  495.     mov    cx,400h
  496.     mov    bx,[decomphandle]
  497.     mov    ah,3fh
  498.     int    21h
  499.     or    ax,ax
  500.     jz    short getdecompeof
  501.  
  502.     mov    cx,ax
  503.     shr    cx,1
  504.     mov    si,offset BUFFER
  505. getdecomp20:
  506.     lodsw
  507.     mov    fs:[edi],ax
  508.     add    edi,2
  509.     loop    getdecomp20
  510.     jmp    getdecomp10
  511.  
  512. getdecompeof:
  513.     add    edi,3
  514.     and    di,0fffch
  515.     mov    [decomppointerbase],edi
  516.  
  517.     mov    ah,3eh            ;close file
  518.     mov    bx,[decomphandle]
  519.     int    21h
  520.  
  521.     mov    ebx,[decomppointerbase]
  522.     mov    eax,[decompdatabase]
  523.     xor    edx,edx
  524. getdecomp100:
  525.     mov    fs:[ebx],eax
  526.     mov    dx,fs:[eax]
  527.     inc    dx
  528.     shl    dx,1
  529.     add    eax,edx
  530.     add    ebx,4
  531.     cmp    eax,[decomppointerbase]
  532.     jb    getdecomp100
  533.     mov    [historybase],ebx
  534.     ret
  535.  
  536.  
  537. getROWS:
  538.     mov    ax,3d00h        ;open for read
  539.     mov    dx,offset ROWSfilename
  540.     int    21h
  541.     jc    diskerror
  542.     mov    [ROWShandle],ax
  543.  
  544.     mov    edi,[ROWSbase]
  545. getROWS10:
  546.     mov    dx,offset BUFFER
  547.     mov    cx,400h            ;1024bytes unit
  548.     mov    bx,[ROWShandle]
  549.     mov    ah,3fh            ;read
  550.     int    21h
  551.     or    ax,ax
  552.     jz    short getROWSeof
  553.  
  554.     mov    cx,ax
  555.     shr    cx,1
  556.     mov    si,offset BUFFER
  557. getROWS20:
  558.     lodsw
  559.     mov    fs:[edi],ax
  560.     add    edi,2
  561.     loop    getROWS20
  562.     jmp    getROWS10
  563.  
  564. getROWSeof:
  565.     mov    eax,edi
  566.     add    eax,3
  567.     and    ax,0fffch
  568.  
  569.     mov    [decompdatabase],eax
  570. comment %
  571.     mov    eax,edi
  572.     sub    eax,[ROWSbase]
  573.     shr    eax,1
  574.     mov    [ROWSsize],ax
  575. %
  576.     mov    ah,3eh            ;close file
  577.     mov    bx,[ROWShandle]
  578.     int    21h
  579.     ret
  580.  
  581.  
  582. getPRIMES:
  583.     mov    si,offset primefilename
  584.     call    setpathname
  585.     mov    dx,offset BUFFER
  586.     mov    ax,3d00h        ;open for read
  587.     int    21h
  588.     jc    diskerror
  589.     mov    [primehandle],ax
  590.  
  591.     mov    edi,primeadr
  592. primelp:
  593.     mov    dx,offset BUFFER
  594.     mov    cx,400h
  595.     mov    bx,[primehandle]
  596.     mov    ah,3fh
  597.     int    21h
  598.     or    ax,ax
  599.     jz    short primeeof
  600.  
  601.     shr    ax,2
  602.     mov    cx,ax
  603.     mov    si,offset BUFFER
  604. prime50:
  605.     lodsd
  606.     mov    fs:[edi],eax
  607.     add    edi,8
  608.     loop    prime50
  609.     jmp    primelp
  610.  
  611. primeeof:
  612.     mov    [ROWSbase],edi
  613.  
  614.     mov    ah,3eh
  615.     mov    bx,[primehandle]
  616.     int    21h
  617.     ret
  618.  
  619.  
  620. setpathname:                ;input si
  621.     push    ax
  622.     push    cx
  623.     push    di
  624.  
  625.     push    ds            ;/*
  626.     push    si
  627.  
  628.     lds    si,dword ptr [Drv_off]
  629.     mov    di,offset BUFFER
  630.     lodsw
  631.     mov    cx,ax
  632.     shl    cx,1
  633.     and    cx,00ffh        ;cut 255 chars
  634.     shl    ax,1
  635.     jnc    short setpath10
  636.     dec    cx
  637. setpath10:
  638.     rep    movsb
  639.  
  640.     pop    si
  641.     pop    ds            ;*/
  642.  
  643. setpath20:
  644.     lodsb
  645.     stosb
  646.     or    al,al
  647.     jnz    setpath20
  648.  
  649.     pop    di
  650.     pop    cx
  651.     pop    ax
  652.     ret
  653.  
  654.  
  655. primefilename    db    'PPMPPRMS.PPM',0
  656. ansfilename    db    'PPMPANS.PPM',0
  657. historyfilename    db    'PPMPHIST.PPM',0
  658. ROWSFILENAME    db    'PPMPROWS.PPM',0
  659. DECOMPFILENAME    db    'PPMPDCMP.PPM',0
  660.  
  661.  
  662. setPROTECTfs:
  663.     pushf
  664.  
  665.     ; make GDT
  666.  
  667.     mov    ax,cs
  668.     and    eax,0ffffh
  669.     shl    eax,4
  670.     add    eax,offset gdttbl
  671.     mov    dword ptr cs:[gdtaddr+2],eax
  672.     lgdt    cs:[gdtaddr]
  673.  
  674.     cld
  675.     cli
  676.  
  677.     mov    eax,cr0        ;set PE bit
  678.     or    eax,1        ;
  679.     mov    cr0,eax        ;
  680.     jmp    $+2        ;flush buffer
  681.  
  682.     mov    ax,8        ;now in protect mode
  683.     mov    fs,ax
  684.  
  685.     mov    eax,cr0        ;reset PE bit
  686.     and    eax,07ffffffeh    ;
  687.     mov    cr0,eax        ;
  688.     jmp    $+2        ;flush buffer
  689.  
  690.     popf
  691.     ret
  692.  
  693. descriptor    struc
  694.     limit_0_15    dw    0
  695.     base_0_15    dw    0
  696.     base_16_23    db    0
  697.     access        db    0
  698.     gran        db    0
  699.     base_24_31    db    0
  700. descriptor    ends
  701.  
  702. data_seg_access    equ    092h
  703.  
  704. gdtaddr    label    qword
  705.     dw    2*8        ;size of GDT table
  706.     dd    ?
  707.     dw    0
  708. gdttbl    label    dword
  709.  
  710. gdt_null    descriptor <,,,,,>
  711. gdt_extra    descriptor <0ffffh,0,0,data_seg_access,0cfh,0>
  712.  
  713.  
  714.   align    4
  715. BUFFER        dw    maxbufferwords dup(0)
  716.  
  717. CODE    ENDS
  718. END    START
  719.