📄 3plib.asm
字号:
.386
.model small
.stack 1024
option oldstructs
include cw.inc
b equ byte ptr
w equ word ptr
d equ dword ptr
NewHeaderStruc struc
NewID db '3P' ;identifier.
NewSize dd 0 ;byte size of 3P section of file.
NewLength dd 0 ;byte size of exe image data.
NewAlloc dd 0 ;byte size of program.
NewSegments dw 0 ;number of segment definitions.
NewRelocs dd 0 ;number of relocation table entries.
NewEntryEIP dd 0 ;entry offset.
NewEntryCS dw 0 ;segment list entry number for entry CS.
NewEntryESP dd 0 ;ESP offset.
NewEntrySS dw 0 ;segment list entry number for SS.
NewFlags dd 0 ;Control flags.
NewAutoStack dd 0 ;Auto stack size.
NewAutoDS dw 0 ;Auto DS segment number +1
NewExports dd 0 ;Length of EXPORT section.
NewImports dd 0 ;Length of IMPORT section.
NewImportModCnt dd 0 ;Number of IMPORT modules.
NewReserved db 64-NewReserved dup (?)
NewHeaderStruc ends
.code
;*******************************************************************************
;
;Main entry point..
;
;*******************************************************************************
Start proc near
mov PSPSegment,es
push ds
pop es
mov ax,0ff00h
mov dx,0078h
int 21h
;
;Say hello.
;
mov edx,offset Copyright
mov ah,9
int 21h
;
;Parse command line.
;
call ReadCommand
mov ErrorNumber,1
cmp eax,1
jc @@exit
cmp OptionTable+"L",0
jnz @@8
cmp eax,2
jc @@exit
;
;Do whatever it is we're being asked to do.
;
@@8: cmp OptionTable+"A",0
jz @@0
call AddModule
jmp @@exit
@@0: cmp OptionTable+"D",0
jz @@1
call DeleteModule
jmp @@exit
@@1: cmp OptionTable+"L",0
jz @@2
call ListModules
jmp @@exit
@@2: cmp OptionTable+"E",0
jz @@3
call ExtractModule
jmp @@exit
@@3: cmp OptionTable+"I",0
jz @@4
call ImportObjModule
jmp @@exit
@@4: mov ErrorNumber,2
;
;Make sure temp file is deleted.
;
@@exit: mov edx,offset TMPName
mov ah,41h
int 21h
;
;Display error message and exit.
;
xor edx,edx
mov dl,ErrorNumber
mov edx,[Errorlist+edx*4]
mov ah,9
int 21h
mov al,ErrorNumber
mov ah,4ch
int 21h
Start endp
;*******************************************************************************
;
;Add or update a module.
;
;*******************************************************************************
AddModule proc near
;
;Get new modules name.
;
;
;Open the file.
;
mov edx,OptionPointers+4
mov ax,3d02h
int 21h
mov ErrorNumber,3
jc @@9
mov ModHandle,eax
mov ebx,eax
;
@@0: ;Read header so we can figure out the format.
;
mov edx,offset NewHeader
mov ecx,size NewHeader
mov ah,3fh
int 21h
mov ErrorNumber,4
jc @@9
cmp eax,ecx
jnz @@9
;
;Check the format.
;
cmp w[NewID+edx],"P3"
jz @@2
cmp w[NewID+edx],"ZM"
mov ErrorNumber,5
jnz @@9
;
;Have an MZ stub so find out how long it is and skip it.
;
xor eax,eax
mov ax,[edx+2+2]
dec eax
shl eax,8
xor ecx,ecx
mov cx,[edx+2]
or ecx,ecx
jnz @@1
add eax,512
@@1: add ecx,eax
sub ecx,size NewHeaderStruc
mov dx,cx
shr ecx,16
mov ax,4201h
int 21h
jmp @@0
@@2: mov edi,edx
;
;Check this module has exports.
;
cmp NewExports[edi],0
mov ErrorNumber,6
jz @@9
;
;Preserve offset of this modules real start.
;
xor cx,cx
xor dx,dx
mov ax,4201h
int 21h
shl edx,16
mov dx,ax
sub edx,size NewHeaderStruc
mov ModOffset,edx
;
;Skip segment definitions.
;
movzx edx,NewSegments[edi]
shl edx,3
sys cwcInfo
jc @@3p0
mov edx,eax
@@3p0: mov cx,dx
shr edx,16
xchg cx,dx
mov ax,4201h
int 21h
;
;Skip relocations.
;
mov edx,NewRelocs[edi]
shl edx,2
or edx,edx
jz @@3p1
sys cwcInfo
jc @@3p1
mov edx,eax
@@3p1: mov cx,dx
shr edx,16
xchg cx,dx
mov ax,4201h
int 21h
;
;Load export details.
;
mov ecx,NewExports[edi]
sys GetMemLinear32
mov ErrorNumber,7
jc @@9
mov edx,ecx
sys cwcInfo
jc @@3p2
push edi
mov edi,esi
sys cwcLoad
pop edi
mov ErrorNumber,4
jc @@9
jmp @@3p4
@@3p2: mov ecx,edx
mov edx,esi
mov ah,3fh
int 21h
mov ErrorNumber,4
jc @@9
cmp eax,ecx
jnz @@9
;
@@3p4: ;Copy the module name.
;
push esi
add esi,[esi+4] ;get offset of module name.
movzx ecx,b[esi] ;get name length.
inc ecx
mov edi,offset ModName
rep movsb
pop esi
;
;Clean this bit up.
;
sys RelMemLinear32
mov ecx,ModOffset
mov dx,cx
shr ecx,16
mov ax,4200h
int 21h
mov eax,d[NewHeader+NewSize]
mov ModLength,eax
;
;Open/Create the library.
;
mov edx,OptionPointers
mov ax,3d02h
int 21h
jnc @@3
mov ax,3c00h
xor cx,cx
int 21h
mov ErrorNumber,8
jc @@9
mov LibHandle,eax
jmp @@11
@@3: mov LibHandle,eax
mov ebx,eax
;
;Scan modules for name that matches the one we're adding/replaceing.
;
@@4: mov edx,offset NewHeader
mov ecx,size NewHeaderStruc
mov ah,3fh
int 21h
mov ErrorNumber,4
jc @@9
or eax,eax ;EOF?
jz @@10
cmp eax,ecx
jnz @@9
cmp w[NewID+edx],"P3"
jz @@6
cmp w[NewID+edx],"ZM"
jnz @@10 ;assume 3P EOF
;
;Have an MZ stub so find out how long it is and skip it.
;
xor eax,eax
mov ax,[edx+2+2]
dec eax
shl eax,8
xor ecx,ecx
mov cx,[edx+2]
or ecx,ecx
jnz @@5
add eax,512
@@5: add ecx,eax
sub ecx,size NewHeaderStruc
mov dx,cx
shr ecx,16
mov ax,4201h
int 21h
shl edx,16
mov dx,ax
mov LibOffset,edx
jmp @@4
@@6: mov edi,edx
;
;Store this modules file offset in case it has the right name.
;
xor cx,cx
xor dx,dx
mov ax,4201h
int 21h
shl edx,16
mov dx,ax
sub edx,size NewHeaderStruc
mov LibOffset,edx
;
;Skip segment definitions.
;
movzx edx,NewSegments[edi]
shl edx,3
sys cwcInfo
jc @@03p0
mov edx,eax
@@03p0: mov cx,dx
shr edx,16
xchg cx,dx
mov ax,4201h
int 21h
;
;Skip relocations.
;
mov edx,NewRelocs[edi]
shl edx,2
or edx,edx
jz @@03p1
sys cwcInfo
jc @@03p1
mov edx,eax
@@03p1: mov cx,dx
shr edx,16
xchg cx,dx
mov ax,4201h
int 21h
;
;Load export details.
;
mov ecx,NewExports[edi]
sys GetMemLinear32
mov ErrorNumber,7
jc @@9
mov edx,ecx
sys cwcInfo
jc @@03p2
push edi
mov edi,esi
sys cwcLoad
pop edi
mov ErrorNumber,4
jc @@9
jmp @@03p4
@@03p2: mov ecx,edx
mov edx,esi
mov ah,3fh
int 21h
mov ErrorNumber,4
jc @@9
cmp eax,ecx
jnz @@9
;
@@03p4: ;Copy the module name.
;
push esi
add esi,[esi+4] ;get offset of module name.
movzx ecx,b[esi] ;get name length.
inc ecx
mov edi,offset LibName
rep movsb
pop esi
;
;Lose export memory.
;
sys RelMemLinear32
;
;Compare the module names.
;
mov esi,offset ModName
mov edi,offset LibName
xor ecx,ecx
mov cl,[esi]
cmp cl,[edi]
jnz @@7
inc esi
inc edi
repe cmpsb
jz @@8
;
@@7: ;Move past this module.
;
mov edx,d[NewHeader+NewSize]
add edx,LibOffset
mov LibOffset,edx
mov cx,dx
shr edx,16
xchg cx,dx
mov ax,4200h
int 21h
jmp @@4
;
@@8: ;Set things up ready for this module to be replaced.
;
mov eax,d[NewHeader+NewSize]
mov LibIgnore,eax
;
;Find out how much trailing data there is.
;
xor cx,cx
xor dx,dx
mov ax,4202h
int 21h
shl edx,16
mov dx,ax
sub edx,LibOffset
sub edx,d[NewHeader+NewSize]
mov LibTrail,edx
jmp @@11
;
;Either EOF'd before we found a matching name or we reached the end of the 3P
;chain.
;
;
@@10: ;Set current file offset as copy to point.
;
xor cx,cx
xor dx,dx
mov ax,4201h
int 21h
shl edx,16
mov dx,ax
mov LibOffset,edx
;
;Anything else should be copied strieght through.
;
xor cx,cx
xor dx,dx
mov ax,4202h
int 21h
shl edx,16
mov dx,ax
sub edx,LibOffset
mov LibTrail,edx
;
;Time to generate the updated library.
;
;
@@11: ;Create a dummy output file.
;
mov edx,offset TMPName
xor cx,cx
mov ax,3c00h
int 21h
mov ErrorNumber,9
jc @@9
mov TMPHandle,eax
mov ebx,eax
;
;Copy leading data.
;
xor dx,dx
xor cx,cx
mov ebx,LibHandle
mov ax,4200h
int 21h
mov ebp,LibOffset
@@12: or ebp,ebp
jz @@14
mov ecx,4096
cmp ecx,ebp
jc @@13
mov ecx,ebp
@@13: mov edx,offset IOBuffer
mov ebx,LibHandle
mov ah,3fh
int 21h
mov ErrorNumber,4
jc @@9
cmp eax,ecx
jnz @@9
mov ebx,TMPHandle
mov ah,40h
int 21h
jc @@9
mov ErrorNumber,10
cmp eax,ecx
jnz @@9
sub ebp,eax
jmp @@12
;
@@14: ;Copy new module.
;
mov edx,ModOffset
mov cx,dx
shr edx,16
xchg cx,dx
mov ebx,ModHandle
mov ax,4200h
int 21h
mov ebp,ModLength
@@15: or ebp,ebp
jz @@17
mov ecx,4096
cmp ecx,ebp
jc @@16
mov ecx,ebp
@@16: mov edx,offset IOBuffer
mov ebx,ModHandle
mov ah,3fh
int 21h
mov ErrorNumber,4
jc @@9
cmp eax,ecx
jnz @@9
mov ebx,TMPHandle
mov ah,40h
int 21h
jc @@9
mov ErrorNumber,10
cmp eax,ecx
jnz @@9
sub ebp,eax
jmp @@15
;
@@17: ;Copy any trailing library data.
;
mov ebx,LibHandle
mov edx,LibIgnore
mov cx,dx
shr edx,16
xchg cx,dx
mov ax,4201h
int 21h
mov ebp,LibTrail
@@18: or ebp,ebp
jz @@20
mov ecx,4096
cmp ecx,ebp
jc @@19
mov ecx,ebp
@@19: mov edx,offset IOBuffer
mov ebx,LibHandle
mov ah,3fh
int 21h
mov ErrorNumber,4
jc @@9
cmp eax,ecx
jnz @@9
mov ebx,TMPHandle
mov ah,40h
int 21h
jc @@9
mov ErrorNumber,10
cmp eax,ecx
jnz @@9
sub ebp,eax
jmp @@18
;
;Close all the files.
;
@@20: mov ebx,ModHandle
mov ah,3eh
int 21h
mov ebx,LibHandle
mov ah,3eh
int 21h
mov ebx,TMPHandle
mov ah,3eh
int 21h
;
;Delete the origional library file.
;
mov edx,OptionPointers
mov ah,41h
int 21h
;
;Rename temp file as library file.
;
mov edx,offset TMPName
mov edi,OptionPointers
mov ah,56h
int 21h
;
;Return success to caller.
;
mov ErrorNumber,0
@@9: ret
AddModule endp
;*******************************************************************************
;
;Delete a module.
;
;*******************************************************************************
DeleteModule proc near
;
;Get module name to delete in right format.
;
mov esi,OptionPointers+4
xor ecx,ecx
@@0: mov al,[esi]
or al,al
jz @@1
inc esi
inc ecx
jmp @@0
@@1: mov esi,OptionPointers+4
mov edi,offset ModName
mov b[edi],cl
inc edi
@@2: mov al,[esi]
cmp al,61h ; 'a'
jb @@3
cmp al,7Ah ; 'z'
ja @@3
and al,5Fh ;convert to upper case.
@@3: mov [edi],al
inc esi
inc edi
dec ecx
jnz @@2
;
;Open the library.
;
mov edx,OptionPointers
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -