📄 seedisk.asm
字号:
data segment
err db "CHS value not valid!$"
da db "Cover the first sector?(Y/N)$"
nam db " EXE$"
nm db "P EXE$"
mb db "PICK ASM$"
ni db "D EXE$"
data ends
paintsen macro a
pusha
mov dx,seg a
mov ds,dx
mov edi,offset a
mov dx,0b800h
mov es,dx
mov bh,0ah
mov esi,144h
KKKE:
mov al,[di]
inc di
cmp al,24h
jz KKKS
mov es:[si],al
inc si
mov es:[si],bh
inc si
jmp KKKE
KKKS:
popa
endm
fileinfo macro a ;get file size(ecx) and position(eax).
mov edi,offset a
call file
call cluster
call getfilesize
endm
code segment
assume cs:code
.386
start:
mov eax,1
mov esi,140h
AGG:
push esi
mov bl,40h
xor esi,esi
mov dx,4000h
call nowin
pop esi
SEE:
mov ecx,640h
mov dx,4000h
call seeram
push eax
push esi
mov bh,0ah
mov si,0a4h
call painteax
pop esi
mov eax,esi
mov si,0b6h
call painteax
mov esi,eax
pop eax
push eax
call control
pop ebx
cmp eax,ebx
jnz AGG
mov edx,esi
mov cl,10h
shr edx,cl
cmp dx,0
jz SEE
cmp dx,0ffffh
jnz NOTT
mov ecx,80h
call countdeax
and esi,0ffffh
jmp AGG
NOTT:
mov ecx,80h
call counteax
and esi,0ffffh
jmp AGG
dive: ; eax/ebx -> edx,eax-ebx*edx->eax.
xor edx,edx
SUU:
cmp eax,ebx
jb OBBE
sub eax,ebx
inc edx
jmp SUU
OBBE:
ret
fileoff: ;dx=segment,return value si="nam" offset.bh=0,found,else not found.
push eax ;this function can read root directory to ram dx:0000,and search
push edx ;"nam",if found,si is offset of "nam" in ram,else bh=1.
mov ebx,2
call cluster
mov bl,40h
xor esi,esi
call nowin
call searchroot
pop edx
pop eax
ret
getfilesize: ;this function can get file size to ecx(bytes),if file isn't
push edx ;found bh=1.
push ds
push esi
mov dx,4000h
call fileoff
cmp bh,0
jnz NNFF
add si,1ch
mov ds,dx
mov ecx,[si]
NNFF:
pop esi
pop ds
pop edx
ret
searchfat: ;this function can search FAT,ebx is cluster NO.,return value
push eax ;eax is FF FF FF 0F h,or the next cluster of file.
push esi
push edx
push ebx
mov bl,80h
mov dx,4000h
call fat
pop ebx
mov eax,4
mul ebx
mov esi,eax
mov eax,[esi]
pop edx
pop esi
pop eax
ret
fat: ;this function can read FAT to ram,dx is segment.
push eax ;length of FAT is defined by bl
push ebx
push edx
push esi
xor eax,eax
mov esi,0c001h
call lbb
xor esi,esi
call nowin
pop esi
pop edx
pop ebx
pop eax
ret
searchroot: ;this function can search file "nam" in root ,if found bh=0,
push eax ;the filename in ram is in offset si,seg dx,else bh=1
push edx
mov ebx,2
call cluster
xor esi,esi
mov bl,40h
call nowin
xor esi,esi
call search
pop edx
pop eax
ret
cfiles: ;this function can change the size of file " nam",size is in ecx
push eax
push ebx
push ecx
push edx
push esi
push ds
mov ebx,2
call cluster
mov dx,4000h
call searchroot
add si,1ch
mov ds,dx
mov [si],ecx
mov bl,40h
xor esi,esi
call nowout
pop ds
pop esi
pop edx
pop ecx
pop ebx
pop eax
ret
file: ;this function can search file "nam" in root,if found ,ah=0,ebx is
push ecx ;the 1st cluster of the file;else ah=1
push edx
push esi
push ds
push eax
mov ebx,2
call cluster
xor esi,esi
mov bl,40h
mov dx,6000h
call nowin
xor esi,esi
call search
cmp bh,0
jnz NFFD
mov ds,dx
add si,14h
mov bx,[si]
mov cl,10h
shl ebx,cl
add si,6
mov bx,[si]
jmp FFD
NFFD:
pop eax
pop ds
pop esi
pop edx
pop ecx
mov ah,1
xor ebx,ebx
ret
FFD:
pop eax
pop ds
pop esi
pop edx
pop ecx
mov ah,0
ret
search: ;this function can search nam from offset si,segment dx,if
push ax
push di
push si
AGGA: ;found ,bh=0,while si is the offset,else bh=1.
xor bh,bh
call match
cmp bh,0
jz OVVR
inc si
pop ax
push ax
cmp si,ax
jz NFD
jmp AGGA
NFD:
mov bh,1
pop si
pop di
pop ax
ret
OVVR:
mov bh,0
pop di
pop di
pop ax
ret
control:
push eax
AW:
mov ah,7
int 21h
cmp al,61h
jnz AB
add esi,500h
jmp OVE
AB:
cmp al,64h
jnz AC
sub esi,500h
jmp OVE
AC:
cmp al,77h
jnz AD
sub esi,0a0h
jmp OVE
AD:
cmp al,73h
jnz AE
add esi,0a0h
jmp OVE
AE:
cmp al,71h
jnz AS
jmp QU
AS:
cmp al,65h
jnz ATT
pop eax
call ea
xor esi,esi
push eax
jmp OVE
ATT:
cmp al,66h
jnz OVE
pop eax
push eax
call lba
OVE:
pop eax
ret
QU:
pop eax
jmp Q
match: ;this function can match 'nam' from offset si,segment dx,
push ds ;if matched ,bh=0,else, bh=1 ;di must be filled with offset.
push es
push eax
push ecx
push edx
push esi
push edi
push bx
mov ds,dx
mov dx,seg nam
mov es,dx
NEQ:
mov al,es:[di]
mov bl,[si]
cmp al,24h
jz ENDD
inc di
inc si
cmp al,bl
jz NEQ
mov bh,1
jmp NEQ
ENDD:
mov al,bh
pop bx
mov bh,al
pop edi
pop esi
pop edx
pop ecx
pop eax
pop es
pop ds
ret
cluster: ;cluster number is in ebx,return value is in eax,eax is LBA
push edx
push ebx
push ecx
push esi
mov esi,8001h
xor eax,eax
call lbb
push eax
mov eax,4e1h
add eax,ebx
mov ebx,10h
mul ebx
mov ecx,eax
pop eax
call counteax
pop esi
pop ecx
pop ebx
pop edx
ret
trans: ;souce data LBA is in eax,des data LBA is in ecx,the amount is in bl,
push edx ;it can copy sectors in LBA eax to sector in LBA ebx.
push esi
push ds
push eax
push ecx
push ebx
mov dx,4000h
xor esi,esi
call nowin
mov eax,ecx
call nowout
pop ebx
pop ecx
pop eax
pop ds
pop esi
pop edx
ret
nowout: ;as nowin,bl is the amount of sectors to write,eax is LBA,dx is
pusha ;segment,si is offset of the data to write
cmp eax,1h
jnz NNNN
paintsen da
IIOO:
mov ah,0
int 16h
cmp al,79h
jz NNNN
cmp al,6eh
jnz LLLL
jmp PPPP
LLLL:
jmp IIOO
NNNN:
popa
pusha
mov ds,dx
mov bh,30h
call fill
WWW:
call check
mov ax,[si]
add si,2
call writepio
mov dx,1437h
in al,dx
cmp al,50h
ja WWW
PPPP:
popa
ret
ea:
push ecx
push ds
push esi
push bx
push edx
mov dx,0b800h
mov ds,dx
xor edx,edx
mov si,0a4h
mov bh,4
mov cl,4
INN:
mov ah,7
int 21h
mov [si],al
inc si
mov [si],bh
inc si
call unascii
add dl,al
shl edx,cl
cmp si,0b2h
jnz INN
mov ah,7
int 21h
mov [si],al
inc si
mov [si],bh
call unascii
add dl,al
mov eax,edx
mov bh,04h
mov si,144h
call painteax
pop edx
pop bx
pop esi
pop ds
pop ecx
ret
lba:
push eax
call lbb
pop eax
ret
lbb:
push esi
push ebx
push ecx
push edx
cmp esi,0
jnz NOO
pop edx
jmp NOI
NOO:
xor edx,edx
JHDS:
mov cx,200h
JHS:
dec esi
cmp esi,0
jz OI
dec cx
cmp cx,0
ja JHS
inc edx
jmp JHDS
OI:
mov ecx,edx
pop edx
cmp ecx,0
jz NOI
call counteax
NOI:
mov bh,0ah
mov si,0c8h
call painteax
pop ecx
pop ebx
pop esi
ret
fill: ;bh ->port 1437h,bl->port 1432h,eax uses as LBA,this function
push edx ;can fill port 1432h~1437h for readpio or writepio
push ecx
push ds
push ebx
push eax
mov dx,1433h
out dx,al
mov cl,8
shr eax,cl
mov dx,1436h
out dx,al
mov dx,1434h
shr eax,cl
out dx,al
inc dx
shr eax,cl
out dx,al
mov dx,1432h
mov al,bl
out dx,al
mov dx,1437h
mov al,bh
out dx,al
pop eax
pop ebx
pop ds
pop ecx
pop edx
ret
check: ;check whether IDE device is OK
push eax
push edx
push eax
push ebx
mov dx,1437h
in al,dx
and al,21h
cmp al,0
jz EO
call ERRO
EO:
pop ebx
pop eax
pop edx
pop eax
ret
ERRO:
call ERROR
pop ebx
pop eax
pop edx
pop eax
jmp Q
ERROR:
push eax
push esi
push ds
push edi
push es
push ebx
mov bh,4
mov dx,0b800h
mov es,dx
mov dx,seg err
mov ds,dx
mov si,144h
mov di,offset err
VO:
mov al,[di]
inc di
cmp al,24h
jz OV
mov es:[si],al
inc si
mov es:[si],bh
inc si
jmp VO
OV:
pop ebx
pop es
pop edi
pop ds
pop esi
pop eax
ret
readpio: ;data in ax
push edx
W:
mov dx,0ebh
out dx,al
mov dx,1437h
in al,dx
and al,80h
ja W
mov dx,1430h
in ax,dx
pop edx
ret
writepio: ;data be in ax
push edx
push ax
WW:
mov dx,0ebh
out dx,al
mov dx,1437h
in al,dx
and al,80h
cmp al,0
ja WW
pop ax
mov dx,1430h
out dx,ax
pop edx
ret
ifover: ;al=1,busy;al=0,over
push edx
mov dx,1437h
in al,dx
cmp al,58h
jb N
mov al,1
jmp M
N:xor al,al
M:
pop edx
ret
nowin: ;read disk and put data to segment dx,offset si,the amount of sectors
push eax
push ds ;to read is in bl,eax used ax LBA
push ebx
push esi
mov bh,20h
mov ds,dx
call fill
AGA:
call check
call readpio
mov [si],ax
add si,2
call ifover
cmp al,1
jz AGA
pop esi
pop ebx
pop ds
pop eax
ret
counteax: ; amount of time is in ecx
push ecx
DE:
inc eax
cmp al,40h
jnz A
add eax,0c1h
A:
cmp ah,10h
jnz CCD
add eax,0f000h
CCD:
dec ecx
cmp ecx,0
ja DE
pop ecx
ret
countdeax: ;decrease eax,amount of time is in ecx
push ecx
BI:
dec eax
cmp al,0
jnz E
sub eax,1
E:
cmp al,0ffh
jnz F
sub al,0c0h
F:
cmp ah,0ffh
jnz G
sub ah,0f0h
G:
dec ecx
cmp ecx,0
ja BI
pop ecx
ret
ascii: ;change digits into ASCII data,bl is ahead of al
push ecx
push ebx
mov bl,al
and bl,0f0h
mov cl,4
shr bl,cl
and al,0fh
cmp al,9
ja H
add al,30h
jmp I
H:
add al,37h
I:
cmp bl,9
ja J
add bl,30h
jmp K
J:
add bl,37h
K:
pop ecx
mov cl,bl
mov bx,cx
pop ecx
ret
unascii: ;change ascii digits into data,regiser al is used
cmp al,30h
jb P
cmp al,39h
ja O
sub al,30h
jmp L
O:
cmp al,61h
jb P
cmp al,66h
ja P
sub al,57h
jmp L
P:
mov al,0ffh
L:
ret
paintal: ;si is offset,bh is color,si should be added 4 each time
push esi
push edx
push eax
push ebx
push ds
mov dx,0b800h
mov ds,dx
call ascii
mov [si],bl
inc si
mov [si],bh
inc si
mov [si],al
inc si
mov [si],bh
pop ds
pop ebx
pop eax
pop edx
pop esi
ret
paintax: ;si is offset,bh is color,si should be added 8 each time
push eax
push esi
xchg al,ah
call paintal
add si,4
mov al,ah
call paintal
pop esi
pop eax
ret
painteax: ;si is offset,bh is color,si should be added 10h each time
push eax
push ecx
push edx
push esi
mov edx,eax
mov cl,10h
shr eax,cl
call paintax
add si,8
mov ax,dx
call paintax
pop esi
pop edx
pop ecx
pop eax
ret
seeram: ;see ram in digits,si is offset,dx is segment,cx is the amount of
push es ;bytes .
push ds
push edx
push ecx
push ebx
push eax
push esi
push edi
mov es,dx
mov dx,0b800h
mov ds,dx
mov di,140h
mov bh,6
R:
mov al,es:[si]
inc si
call ascii
mov [di],bl
inc di
mov [di],bh
inc di
mov [di],al
inc di
mov [di],bh
inc di
dec cx
cmp cx,0
ja R
pop edi
pop esi
pop eax
pop ebx
pop ecx
pop edx
pop ds
pop es
ret
showram: ;see ram directly ,si is offset,dx is segment,cx is the amount of
push edx ;bytes
push ecx
push ebx
push eax
push esi
push edi
push es
push ds
mov es,dx
mov dx,0b800h
mov ds,dx
mov bh,9
mov di,140h
S:
mov al,es:[si]
mov [di],al
inc di
inc si
mov [di],bh
inc di
dec cx
cmp cx,0
ja S
pop ds
pop es
pop edi
pop esi
pop eax
pop ebx
pop ecx
pop edx
ret
Q:
mov ax,4c00h
int 21h
code ends
end start
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -