home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / caway349.zip / SOURCE / ALL / CW / CWMAKE.ASM < prev    next >
Assembly Source File  |  1996-01-27  |  7KB  |  333 lines

  1.     .model small
  2.     .stack 400h
  3.     .386
  4.     option oldstructs
  5.  
  6.     include general.inc
  7.     include ..\strucs.inc
  8.  
  9.     .code
  10.  
  11. ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  12. Start    proc    near
  13.     mov    ax,DGROUP
  14.     mov    ds,ax
  15.     mov    PSPSegment,es
  16.     ;
  17.     mov    cx,sp
  18.     shr    cx,4
  19.     inc    cx
  20.     mov    ax,es
  21.     mov    bx,ss        ;Get program end segment.
  22.     add    bx,cx
  23.     sub    bx,ax        ;Size program.
  24.     mov    ah,4ah
  25.     int    21h        ;Re-size memory block.
  26.     ;
  27.     call    ProcessFile
  28.     ;
  29.     mov    ax,4c00h
  30.     int    21h
  31. Start    endp
  32.  
  33.  
  34. ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  35. ProcessFile    proc    near
  36.     mov    dx,offset InFileName
  37.     mov    ax,3d00h
  38.     int    21h
  39.     jc    @@9
  40.     mov    InFileHandle,ax
  41.     ;
  42.     mov    dx,offset OutFileName
  43.     mov    ax,3c00h
  44.     mov    cx,0
  45.     int    21h
  46.     jc    @@9
  47.     mov    OutFileHandle,ax
  48.     ;
  49.     ;Process .EXE relocation table and produce new format header.
  50.     ;
  51.     mov    dx,offset ExeSignature
  52.     mov    cx,1bh
  53.     mov    bx,InFileHandle
  54.     mov    ah,3fh
  55.     int    21h        ;read the .EXE header.
  56.     jc    @@9
  57.     cmp    ax,1bh
  58.     jnz    @@9
  59.     cmp    w[ExeSignature],'ZM'    ;Correct ID?
  60.     jnz    @@9
  61.     ;
  62.     ;Get header size in bytes.
  63.     ;
  64.     mov    bx,w[ExeHeaderSize]    ;Work out header size.
  65.     xor    cx,cx        ;/
  66.     add    bx,bx        ;/
  67.     adc    cx,0        ;/
  68.     add    bx,bx        ;/
  69.     adc    cx,0        ;/
  70.     add    bx,bx        ;/
  71.     adc    cx,0        ;/
  72.     add    bx,bx        ;/
  73.     adc    cx,0        ;/
  74.     mov    w[ExeSize1],bx    ;/
  75.     mov    w[ExeSize1+2],cx    ;/
  76.     ;
  77.     ;Get exe image size in bytes.
  78.     ;
  79.     mov    ax,w[ExeLength+2]    ;get length in 512 byte blocks
  80.     dec    ax        ;lose 1 cos its rounded up
  81.     add    ax,ax        ;mult by 2
  82.     mov    dh,0
  83.     mov    dl,ah
  84.     mov    ah,al
  85.     mov    al,dh        ;mult by 256=*512
  86.     add    ax,w[ExeLength]    ;add length mod 512
  87.     adc    dx,0        ;add any carry to dx
  88.     sub    ax,bx        ;remove header total size.
  89.     sbb    dx,cx        ;/
  90.     mov    w[ExeSize2],ax    ;/
  91.     mov    w[ExeSize2+2],dx    ;/
  92.     ;
  93.     shl    edx,16
  94.     mov    dx,ax
  95.     mov    si,offset NewHeader
  96.     mov    NewLength[si],edx    ;store EXE image length in the header.
  97.     ;
  98.     push    edx
  99.     cmp    edx,100000h
  100.     jc    @@0
  101.     add    edx,4095
  102.     shr    edx,12        ;lose bottom bits.
  103.     or    edx,1 shl 20        ;Set our version of the G bit.
  104. @@0:    mov    CodeSegLimit,edx
  105.     pop    edx
  106.     ;
  107.     add    edx,1024
  108.     mov    NewAlloc[si],edx
  109.     and    edx,0ffffffffh-3
  110.     mov    NewEntryESP[si],edx
  111.     mov    NewSegments[si],2
  112.     movzx    eax,ExeRelocNum
  113.     mov    NewRelocs[si],eax
  114.     movzx    eax,ExeEntryIP
  115.     mov    NewEntryEIP[si],eax    ;setup entry offset.
  116.     mov    NewEntryCS[si],1
  117.     mov    NewEntrySS[si],0
  118.     ;
  119.     ;Read relocation table.
  120.     ;
  121.     mov    bx,ExeRelocNum    ;get number of relocation items.
  122.     or    bx,bx
  123.     jz    @@NoRelocMem
  124.     shl    bx,2
  125.     shr    bx,4
  126.     inc    bx
  127.     mov    ah,48h
  128.     int    21h
  129.     jc    @@9
  130.     mov    RelocSegment,ax    ;stow the memory address.
  131.     ;
  132.     mov    dx,ExeRelocFirst
  133.     mov    cx,0
  134.     mov    bx,InFileHandle
  135.     mov    ax,4200h
  136.     int    21h        ;move to relocation table.
  137.     ;
  138.     mov    cx,ExeRelocNum
  139.     shl    cx,2
  140.     mov    bx,InFileHandle
  141.     push    ds
  142.     mov    ds,RelocSegment
  143.     mov    dx,0
  144.     mov    ax,3f00h
  145.     int    21h
  146.     pop    ds
  147.     jc    @@9
  148.     ;
  149. @@NoRelocMem:    ;Get some memory for the exe image.
  150.     ;
  151.     mov    ebx,d[ExeSize2]    ;get exe image size.
  152.     shr    ebx,4
  153.     inc    bx
  154.     mov    ah,48h
  155.     int    21h
  156.     jc    @@9
  157.     mov    EXESegment,ax
  158.     ;
  159.     mov    dx,w[ExeSize1]    ;get image file offset.
  160.     mov    cx,w[ExeSize1+2]
  161.     mov    bx,InFileHandle
  162.     mov    ax,4200h
  163.     int    21h        ;move to start of exe image.
  164.     jc    @@9
  165.     mov    bx,InFileHandle
  166.     mov    cx,w[ExeSize2]
  167.     mov    dx,w[ExeSize2+2]
  168.     push    ds
  169.     mov    ds,EXESegment
  170.     mov    dx,0
  171.     mov    ah,3fh
  172.     int    21h
  173.     pop    ds
  174.     jc    @@9
  175.     ;
  176.     mov    bx,OutFileHandle
  177.     mov    cx,0
  178.     mov    dx,0
  179.     mov    ax,4201h
  180.     int    21h
  181.     shl    edx,16
  182.     mov    dx,ax
  183.     mov    Real3POffset,edx
  184.     ;
  185.     ;Update exe image with real segment numbers.
  186.     ;
  187.     mov    cx,ExeRelocNum    ;number of entries.
  188.     jcxz    @@NoReloc
  189.     mov    es,RelocSegment    ;the relocations.
  190.     mov    si,0
  191. @@3:    mov    di,es:[si]
  192.     mov    ax,es:[si+2]
  193.     add    ax,EXESegment
  194.     mov    fs,ax
  195.     mov    fs:w[di],0
  196.     add    si,4        ;next relocation entry.
  197.     loop    @@3
  198.     ;
  199.     ;Convert relocation table to linear offsets.
  200.     ;
  201.     mov    cx,ExeRelocNum    ;number of entries.
  202.     mov    es,RelocSegment    ;list of relocations.
  203.     mov    si,0
  204. @@1:    movzx    eax,es:w[si+2]    ;get segment offset.
  205.     shl    eax,4        ;make it linear.
  206.     movzx    ebx,es:w[si+0]    ;get offset.
  207.     add    eax,ebx        ;add in offset.
  208.     mov    es:d[si],eax        ;store linear offset.
  209.     add    si,4
  210.     loop    @@1
  211. @@NoReloc:    ;
  212.     ;Write main header.
  213.     ;
  214.     mov    dx,offset NewHeader    ;write the header to make space.
  215.     mov    bx,OutFileHandle
  216.     mov    cx,size NewHeaderStruc
  217.     mov    ah,40h
  218.     int    21h
  219.     ;
  220.     ;Write segment definitions.
  221.     ;
  222.     mov    dx,offset DataSegBase
  223.     mov    cx,8+8
  224.     mov    bx,OutFileHandle
  225.     mov    ah,40h
  226.     int    21h
  227.     ;
  228.     ;Write relocation table.
  229.     ;
  230.     mov    cx,ExeRelocNum    ;number of entries.
  231.     shl    cx,2
  232.     mov    bx,OutFileHandle
  233.     push    ds
  234.     mov    ds,RelocSegment    ;where they are.
  235.     mov    dx,0
  236.     mov    ah,40h
  237.     int    21h
  238.     pop    ds
  239.     jc    @@9
  240.     ;
  241.     ;Write exe image.
  242.     ;
  243.     mov    cx,w[ExeSize2]
  244.     mov    dx,w[ExeSize2+2]
  245.     mov    bx,OutFileHandle
  246.     push    ds
  247.     mov    ds,ExeSegment
  248.     mov    dx,0
  249.     mov    ah,40h
  250.     int    21h
  251.     pop    ds
  252.     jc    @@9
  253.     ;
  254.     ;Calculate file size.
  255.     ;
  256.     mov    bx,OutFileHandle
  257.     mov    cx,0
  258.     mov    dx,0
  259.     mov    ax,4201h
  260.     int    21h
  261.     shl    edx,16
  262.     mov    dx,ax
  263.     sub    edx,Real3POffset
  264.     mov    si,offset NewHeader
  265.     mov    NewSize[si],edx
  266.     ;
  267.     ;Now go back and write the real header.
  268.     ;
  269.     mov    edx,Real3POffset
  270.     mov    cx,dx
  271.     shr    edx,16
  272.     xchg    cx,dx
  273.     mov    bx,OutFileHandle
  274.     mov    ax,4200h
  275.     int    21h
  276.     mov    dx,offset NewHeader    ;write the header to make space.
  277.     mov    bx,OutFileHandle
  278.     mov    cx,size NewHeaderStruc
  279.     mov    ah,40h
  280.     int    21h
  281.     ;
  282.     mov    bx,InFileHandle
  283.     mov    ah,3eh
  284.     int    21h
  285.     mov    bx,OutFileHandle
  286.     mov    ah,3eh
  287.     int    21h
  288.     ;
  289. @@9:    ret
  290. ProcessFile    endp
  291.  
  292.  
  293.     .data
  294. ;
  295. PSPSegment    dw ?
  296. ;
  297. InFileName    db 'cw.exe',0
  298. InFileHandle    dw ?
  299. OutFileName    db 'cw.cw',0
  300. OutFileHandle    dw ?
  301. ;
  302. ExeSignature    db ?    ;00 Identifier text.
  303.     db ?    ;01 /
  304. ExeLength    dw ?    ;02 Length of file MOD 512
  305.     dw ?    ;04 Length of file in 512 byte blocks.
  306. ExeRelocNum    dw ?    ;06 Number of relocation items.
  307. ExeHeaderSize    dw ?    ;08 Length of header in 16 byte paragraphs.
  308. ExeMinAlloc    dw ?    ;0A Minimum number of para's needed above program.
  309. ExeMaxAlloc    dw ?    ;0C Maximum number of para's needed above program.
  310. ExeStackSeg    dw ?    ;0E Segment displacement of stack module.
  311. ExeEntrySP    dw ?    ;10 value for SP at entry.
  312. ExeCheckSum    dw ?    ;12 Check sum...
  313. ExeEntryIP    dw ?    ;14 Contents of IP at entry.
  314. ExeEntryCS    dw ?    ;16 Segment displacement of CS at entry.
  315. ExeRelocFirst    dw ?    ;18 First relocation item offset.
  316. ExeOverlayNum    db ?    ;1A Overlay number.
  317. ;
  318. ExeSize1    dw ?,?    ;Header size
  319. ExeSize2    dw ?,?    ;Real file size.
  320. ;
  321. NewHeader    NewHeaderStruc <>    ;make space for a header.
  322. ;
  323. DataSegBase    dd 0
  324. DataSegLimit    dd 0fffffh+(1 shl 20)+(1 shl 21)
  325. CodeSegBase    dd 0
  326. CodeSegLimit    dd ?
  327. ;
  328. RelocSegment    dw 0
  329. EXESegment    dw 0
  330. Real3POffset    dd 0
  331. ;
  332.     end    Start
  333.