home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / basic / compiler / ubasic / ubmpqs32 / mpqshd3.asm < prev    next >
Assembly Source File  |  1990-08-05  |  8KB  |  548 lines

  1. ;MPQSHD3.ASM 3.2
  2. ;    MACHINE LANGUAGE SUBROUTINE
  3. ;    FOR MULTIPLE POLYNOMIAL QUADRATIC SIEVE
  4. ;    Hard Disk verison
  5. ;    1989/90 by YUJI KIDA
  6. ;
  7. .386P
  8.  
  9. code    segment    use16
  10.     assume    cs:code,ds:code
  11.  
  12.     INCLUDE    UBP.MAC
  13.     include    mpqshd.h
  14.  
  15. org    100h
  16.  
  17. start:
  18.     jmp    start0
  19.  
  20. org    104h
  21.     mov    eax,cr0        ;reset PE bit
  22.     and    eax,07ffffffeh
  23.     mov    cr0,eax
  24.  
  25.     db    0eah        ;jmp far ptr flushrl
  26.     dw    offset flushrl
  27. csegmem    dw    ?
  28.  
  29.     ; return to REAL mode
  30. flushrl:
  31.     sti
  32.     mov    ax,ss
  33.     mov    ds,ax
  34.     mov    es,ax
  35.     retf
  36.  
  37.  
  38. ;üÖüÖ COMMAND é╠ÉUéΦò¬é»
  39.  
  40. start0:
  41.     MOV_AX    AR0        ;ARRAY[0] é┼âRâ}âôâhôné╡
  42.     MOV    BX,OFFSET CMD_TBL
  43.     SHL    AX,1
  44.     ADD    BX,AX
  45.     JMP    CS:[BX]
  46.  
  47. CMD_TBL:
  48.     dw    INITIALIZE,GETPRIME,STORE_DATA,CLOSEFILE
  49.  
  50.     even
  51. pmodecsseg    dw    ?
  52.  
  53. P_OFF    DW    ?
  54. P_SEG    dw    ?
  55. primeadrnow    dd    ?
  56.  
  57. W_OFF    DW    ?
  58. W_SEG    DW    ?
  59.  
  60. primes        dw    ?
  61.  
  62. handle1        dw    ?
  63. size1        dw    ?
  64. handle2        dw    ?
  65. size2        dw    ?
  66. handle3        dw    ?
  67. size3        dw    ?
  68. handle4        dw    ?
  69.  
  70. rowpointer    dw    ?
  71.  
  72. rownow        dw    ?
  73.  
  74. offnow        dw    ?
  75. ansnow        dw    ?
  76. masknow        dw    ?
  77.  
  78. allbytes    dw    ?
  79. allwords    dw    ?
  80. halfbytes    dw    ?
  81. halfwords    dw    ?
  82.  
  83.  
  84. ;
  85. ; get and set primes
  86. ;
  87. ;command#=1
  88.  
  89. getprime:
  90.     lds    si,dword ptr cs:[P_OFF]
  91.     mov    edi,dword ptr cs:[primeadrnow]
  92.     lodsw
  93.     cmp    ax,2
  94.     je    getprime4B
  95.     xor    eax,eax
  96.     mov    ax,[si]
  97. getprime10:
  98.     mov    fs:[edi],eax
  99.     add    cs:[primeadrnow],primeunitbytes
  100.     mov    ax,ss
  101.     mov    ds,ax
  102.     retf
  103. getprime4B:
  104.     mov    eax,[si]
  105.     jmp    getprime10
  106.  
  107.  
  108. ;
  109. ; üÖ êΩìsé╠É▌ÆΦ
  110. ;      W=X^2 é╚éΘ W é≡ô╛é─êΩìsé╠âfü[â^é≡É▌ÆΦé╖éΘ
  111.  
  112. ;COMMAND#=2
  113.  
  114. STORE_DATA:
  115.     PUSH    BP
  116.  
  117.     LDS    SI,DWORD PTR CS:[W_OFF]
  118.     MOV    BX,SI        ;MEMO èäéτéΩéΘÉö
  119.  
  120.     MOV    AX,[SI]
  121.     AND    AX,8000H        ;get sign
  122.     ROL    AX,1            ;sign in 0-th bit
  123.     AND    WORD PTR [SI],1FFFH    ;cut sign
  124.     MOV    cs:[ANSNOW],AX        ;16 BIT é╠îïë╩
  125.     MOV    cs:[MASKNOW],2
  126.     MOV    cs:[OFFNOW],offset BUFFER
  127.  
  128.     mov    edi,primeadr+primeunitbytes    ;offset of 2
  129.  
  130.     STD
  131.  
  132.     MOV    AX,[bx]        ;transfer to 32bit format
  133.     test    al,1
  134.     jz    div3200        ;if even words
  135.     MOV    SI,BX
  136.     mov    dx,ax
  137.     SHL    dX,1
  138.     ADD    SI,dX        ;highest adr
  139.     mov    word ptr [si+2],0
  140.     inc    ax
  141. div3200:
  142.     shr    ax,1
  143.     mov    [bx],ax
  144.  
  145.  
  146.     MOV    CX,CS:[primes]
  147. div32LP:
  148.     PUSH    CX
  149.  
  150.     MOV    eBP,fs:[eDI]    ;ebp=èäéΘÉö
  151. div3210:
  152.     MOV    SI,BX
  153.     MOV    AX,[SI]
  154.     MOV    CX,AX        ;dword length
  155.     SHL    AX,2
  156.     sub    ax,2
  157.     ADD    SI,AX        ;dword highest adr
  158.     XOR    eDX,eDX    
  159. div3220:
  160.     LODSd
  161.     DIV    eBP
  162.     LOOP    div3220
  163.  
  164.     OR    eDX,eDX
  165.     JZ    div32100    ;divide exactly if it can
  166.  
  167.     ROL    cs:[MASKNOW],1
  168.     JNC    DIV3230
  169.  
  170.     PUSH    BX        ;set results for each 16 primes
  171.     MOV    BX,cs:[OFFNOW]
  172.     MOV    AX,cs:[ANSNOW]
  173.     MOV    cs:[BX],AX
  174.     MOV    cs:[ANSNOW],0
  175.     ADD    BX,2
  176.     MOV    cs:[OFFNOW],BX
  177.     POP    BX
  178. DIV3230:    
  179.     add    edi,primeunitbytes
  180.     POP    CX
  181.     LOOP    div32LP
  182.  
  183.     mov    si,bx        ;transfer to 16bit format
  184.     mov    ax,[si]
  185.     shl    ax,1
  186.     mov    cx,ax        ;word length?
  187.     shl    ax,1
  188.     add    si,ax
  189.     cmp    word ptr [si],0    ;check highest word
  190.     jne    div3255
  191.     dec    cx
  192. div3255:
  193.     mov    [bx],cx        
  194.  
  195.     jmp    div200
  196.  
  197.  
  198. div32100:
  199.     MOV    AX,cs:[MASKNOW]
  200.     XOR    cs:[ANSNOW],AX
  201.  
  202.     MOV    SI,BX
  203.     MOV    AX,[SI]
  204.     MOV    CX,AX        ;dword length
  205.     SHL    AX,2
  206.     sub    ax,2
  207.     ADD    SI,AX        ;ì┼Åπê╩
  208.     XOR    eDX,eDX    
  209.  
  210.     LODSd
  211.     DIV    eBP
  212.     PUSH    eAX        ;push highst dword
  213.     JMPS    div32120
  214. div32110:
  215.     LODSd
  216.     DIV    eBP
  217. div32120:
  218.     MOV    [SI+4],eAX
  219.     LOOP    div32110
  220.  
  221.     POP    eAX        ;check highest dword
  222.     OR    eAX,eAX
  223.     JNZ    div32130
  224.     DEC    WORD PTR [bx]    ;ì┼Åπê╩鬠0 é╚éτé╬ LEN é≡ DEC
  225. div32130:
  226.     JMP    div3210
  227.  
  228.  
  229.     ;set 2nd matrix part
  230. div200:
  231.     cld
  232.     mov    ax,cs
  233.     mov    ds,ax
  234.     mov    es,ax
  235.  
  236.     mov    di,offset BUFFER
  237.     add    di,[halfbytes]
  238.     mov    cx,[halfwords]
  239.     xor    ax,ax
  240.     rep    stosw
  241.  
  242.     mov    ax,[rownow]
  243.     inc    ax
  244.     mov    [rownow],ax
  245.     dec    ax
  246.     mov    cx,ax        ;memo
  247.     shr    ax,3
  248.     add    ax,offset BUFFER
  249.     add    ax,[halfbytes]
  250.     mov    bx,ax
  251.     and    cl,7
  252.     mov    al,1
  253.     shl    al,cl
  254.     mov    [bx],al
  255.  
  256.     ;write result
  257.  
  258.     mov    ax,[rowpointer]
  259.     inc    ax
  260.     mov    [rowpointer],ax
  261.  
  262.     mov    bx,[handle1]
  263.     cmp    ax,[size1]
  264.     jbe    div220
  265.     mov    bx,[handle2]
  266.     cmp    ax,[size2]
  267.     jbe    div220
  268.     mov    bx,[handle3]
  269.     cmp    ax,[size3]
  270.     jbe    div220
  271.     mov    bx,[handle4]
  272. div220:
  273.     mov    ah,40h        ;write handle
  274.     mov    cx,[allbytes]
  275.     mov    dx,offset BUFFER
  276.     int    21h
  277.     jc    writeerror
  278.     cmp    ax,cx
  279.     jne    writeerror
  280.     
  281.     POP    BP
  282.     MOV    AX,SS
  283.     MOV    DS,AX
  284.     MOV    ES,AX
  285.     RETF
  286.  
  287. writeerror:
  288.     POP    BP
  289.     mov    bx,AR0
  290.     mov    word ptr cs:[bx],0    ;error mark
  291.  
  292.     mov    ax,ss
  293.     mov    ds,ax
  294.     mov    es,ax
  295.     jmp    closefile
  296.  
  297.  
  298. ;
  299. ;üÖ Initialize
  300. ;COMMAND#=0
  301.  
  302. ;    V1=P
  303. ;    V2=W#
  304. ;    AR[1]    TOTAL SIZE \ 2
  305. ;    AR[2]    DRIVE NUMBER 1
  306. ;    AR[3]    SIZE1 \ 2
  307. ;    AR[4]    DRIVE NUMBER 2
  308. ;    AR[5]    SIZE2 \ 2
  309. ;    AR[6]    DRIVE NUMBER 3
  310. ;    AR[7]    SIZE3 \ 2
  311. ;    AR[8]    DRIVE NUMBER 4
  312. ;    AR[9]    SIZE4 \ 2
  313.  
  314. INITIALIZE:
  315.     mov    ax,cs
  316.     mov    ds,ax
  317.     mov    es,ax
  318.  
  319.     MOV    BX,V1        ;P é╠ ADDRESS é╠É▌ÆΦ
  320.     MOV    AX,[BX]
  321.     MOV    [P_OFF],AX
  322.     MOV    AX,[BX+2]
  323.     MOV    [P_SEG],AX
  324.  
  325.     MOV    BX,V2        ;W# é╠ ADDRESS é╠É▌ÆΦ
  326.     MOV    AX,[BX]
  327.     MOV    [W_OFF],AX
  328.     MOV    AX,[BX+2]
  329.     MOV    [W_SEG],AX
  330.  
  331.     MOV    BX,V3        ;set PMODE segment
  332.     MOV    AX,[BX]
  333.     shr    ax,4
  334.     add    ax,[BX+2]
  335.     sub    ax,ARRAYHEADSEG
  336.     mov    [pmodecsseg],ax
  337.  
  338.     MOV_AX    AR1
  339.     shl    ax,1
  340.     dec    ax
  341.     mov    [primes],ax
  342.     inc    ax
  343.     shl    ax,1        ;*2
  344.     shr    ax,3        ;bits -> bytes
  345.     mov    [allbytes],ax
  346.     shr    ax,1
  347.     mov    [halfbytes],ax
  348.     mov    [allwords],ax    
  349.     shr    ax,1
  350.     mov    [halfwords],ax
  351.  
  352.     mov    [rownow],0
  353.     mov    [primeadrnow],primeadr
  354.  
  355.     ; open file
  356.  
  357.     mov    bx,offset MATRIXNAME1
  358.     MOV_AX    AR2
  359.     or    ax,ax
  360.     jz    INIT10
  361.     sub    bx,2
  362.     add    al,'@'
  363.     mov    [bx],al
  364. INIT10:
  365.     mov    dx,bx
  366.     mov    ah,3ch        ;create handle
  367.     mov    cx,0
  368.     int    21h
  369.     jc    diskerror
  370.     mov    [handle1],ax
  371.     MOV_AX    AR3
  372.     shl    ax,1
  373.     MOV    [size1],ax
  374.  
  375.     mov    bx,offset MATRIXNAME2
  376.     MOV_AX    AR4
  377.     or    ax,ax
  378.     jz    INIT20
  379.     sub    bx,2
  380.     add    al,'@'
  381.     mov    [bx],al
  382. INIT20:
  383.     mov    dx,bx
  384.     mov    ah,3ch        ;create handle
  385.     mov    cx,0
  386.     int    21h
  387.     jc    diskerror
  388.     mov    [handle2],ax
  389.     MOV_AX    AR5
  390.     shl    ax,1
  391.     add    ax,[size1]
  392.     MOV    [size2],ax
  393.  
  394.     mov    bx,offset MATRIXNAME3
  395.     MOV_AX    AR6
  396.     or    ax,ax
  397.     jz    INIT30
  398.     sub    bx,2
  399.     add    al,'@'
  400.     mov    [bx],al
  401. INIT30:
  402.     mov    dx,bx
  403.     mov    ah,3ch        ;create handle
  404.     mov    cx,0
  405.     int    21h
  406.     jc    diskerror
  407.     mov    [handle3],ax
  408.     MOV_AX    AR7
  409.     shl    ax,1
  410.     add    ax,[size2]
  411.     MOV    [size3],ax
  412.  
  413.     mov    bx,offset MATRIXNAME4
  414.     MOV_AX    AR8
  415.     or    ax,ax
  416.     jz    INIT40
  417.     sub    bx,2
  418.     add    al,'@'
  419.     mov    [bx],al
  420. INIT40:
  421.     mov    dx,bx
  422.     mov    ah,3ch        ;create handle
  423.     mov    cx,0
  424.     int    21h
  425.     jc    diskerror
  426.     mov    [handle4],ax
  427.  
  428.     mov    [rowpointer],0
  429.  
  430.     ; make GDT
  431.  
  432.     mov    ax,cs
  433.     mov    [csegmem],ax
  434.  
  435.     mov    ax,[pmodecsseg]
  436.     and    eax,0ffffh
  437.     shl    eax,4
  438.     mov    ebx,eax
  439.     shr    eax,16
  440.     mov    gdt_code32.base_0_15,bx
  441.     mov    gdt_code32.base_16_23,al
  442.  
  443.     mov    ax,cs
  444.     and    eax,0ffffh
  445.     shl    eax,4
  446.     mov    ebx,eax
  447.     shr    eax,16
  448.     mov    gdt_code16.base_0_15,bx
  449.     mov    gdt_code16.base_16_23,al
  450.  
  451.     mov    ax,cs
  452.     mov    ds,ax
  453.     mov    si,offset gdttbl
  454.     mov    ax,_gdttblseg
  455.     mov    es,ax
  456.     mov    di,0
  457.     mov    cx,10h
  458.     rep    movsw
  459.     
  460.     mov    eax,_gdttbladr
  461.     mov    dword ptr gdtaddr+2,eax
  462.     lgdt    gdtaddr
  463.  
  464.     cli
  465.  
  466.     mov    eax,cr0        ;set PE bit
  467.     or    eax,1
  468.     mov    cr0,eax
  469.  
  470.     db    0eah        ;goto protect mode
  471.     dw    100h
  472.     dw    offset gdt_code32-gdttbl    ;selector
  473.  
  474.  
  475. diskerror:
  476.     mov    bx,AR0
  477.     mov    word ptr [bx],8001h
  478.     MOV    AX,SS
  479.     MOV    DS,AX
  480.     MOV    ES,AX
  481.     RETF
  482.  
  483.  
  484. descriptor    struc
  485.     limit_0_15    dw    0
  486.     base_0_15    dw    0
  487.     base_16_23    db    0
  488.     access        db    0
  489.     gran        db    0
  490.     base_24_31    db    0
  491. descriptor    ends
  492.  
  493. code_seg_access    equ    09ah
  494. data_seg_access    equ    092h
  495.  
  496.     ; GDT table
  497.  
  498.     align    4
  499. gdtaddr    label    qword
  500.     dw    4*8        ;size of GDT table
  501.     dd    ?
  502.     dw    0
  503.  
  504. gdttbl        label    dword
  505. gdt_null    descriptor <,,,,,>
  506. gdt_code32    descriptor <0ffffh,,,code_seg_access,0cfh,0>
  507. gdt_data32    descriptor <0ffffh,0,0,data_seg_access,0cfh,0>
  508. gdt_code16    descriptor <0ffffh,,,code_seg_access,80h,0>
  509.  
  510.  
  511. ;
  512. ;üÖ close file
  513. ;COMMAND#=2
  514.  
  515. CLOSEFILE:
  516.     mov    ah,3eh
  517.     mov    bx,cs:[handle1]
  518.     int    21h
  519.  
  520.     mov    ah,3eh
  521.     mov    bx,cs:[handle2]
  522.     int    21h
  523.  
  524.     mov    ah,3eh
  525.     mov    bx,cs:[handle3]
  526.     int    21h
  527.  
  528.     mov    ah,3eh
  529.     mov    bx,cs:[handle4]
  530.     int    21h
  531.     retf
  532.  
  533.  
  534.         db    ' :'
  535. MATRIXNAME1    db    'MPQSMAT1.UBD',0
  536.         db    ' :'
  537. MATRIXNAME2    db    'MPQSMAT2.UBD',0
  538.         db    ' :'
  539. MATRIXNAME3    db    'MPQSMAT3.UBD',0
  540.         db    ' :'
  541. MATRIXNAME4    db    'MPQSMAT4.UBD',0
  542.  
  543. ALIGN 4
  544. BUFFER        db    maxbuffersize dup(0)
  545.  
  546. CODE    ENDS
  547. END    START
  548.