汇编开关atapi设备

光盘开关操作。

c 复制代码
;u have some tip to remember in ur heart:
;1 DMA flag=0
;2 Packet command must send 3 times
;3 Slave bit=0e0h,not 0a0h or 0
 
.386
Code segment para use16
assume cs:code

start:
Main Proc near
mov ax,Code
mov ds,ax
mov es,ax


mov dx,1f6h
mov byte ptr ds:[SlaveFlag],0f0h
call AtaPrimary
cmp word ptr ds:[AtapiPort],0
jnz MainProc
mov dx,1f6h
mov byte ptr ds:[SlaveFlag],0e0h
call AtaPrimary
cmp byte ptr ds:[AtapiPort],0
jnz MainProc
mov dx,176h
mov byte ptr ds:[SlaveFlag],0f0h
call AtaPrimary
cmp word ptr ds:[AtapiPort],0
jnz MainProc
mov dx,176h
mov byte ptr ds:[SlaveFlag],0e0h
call AtaPrimary
cmp word ptr ds:[AtapiPort],0
jnz MainProc

mov byte ptr ds:[SlaveFlag],0e0h
call GetSataPort
call CheckSataPort
cmp word ptr ds:[AtapiPort],0
jz NoCd

MainProc:
mov ah,9
mov dx,offset msgOpen
int 21h
mov ah,0
int 16h
mov si,offset PacketOpen
mov ds:[CommandEA],si
call SendCommand
mov ah,9
mov dx,offset msgClose
int 21h
mov ah,0
int 16h
mov si,offset PacketClose
mov ds:[CommandEA],si
call SendCommand
ToQuit:
call QUit
NOCd:
mov ah,9
mov dx,offset msgNoDevice
int 21h
jmp Quit
Main endp



GetSataPort proc near
pushad
cld
mov di,offset SataPort
mov eax,80000008h
NextFunc:
push eax
mov dx,0cf8h
out dx,eax
mov dx,0cfch
in  eax,dx
shr eax,16
cmp eax,0101h
jnz ToNextFunc

pop eax
push eax
add eax,8
mov cx,4
GetPort:
push eax
mov dx,0cf8h
out dx,eax
mov dx,0cfch
in  eax,dx
and al,0feh
stosw
inc word ptr ds:[SataPortNum]
pop eax
add eax,4
loop GetPort

ToNextFunc:
pop eax
add eax,100h
cmp eax,80010000h
jb NextFunc
popad
ret
GetSataPort endp



CheckSataPort proc near
pushad
mov si,offset SataPort
mov cx,ds:[SataPortNum]
cmp cx,0
jz  EndCheck
CheckNext:
lodsw
mov dx,ax
add dx,7
mov al,0a1h
out dx,al
call WaitTime
in al,dx
cmp al,58h
jnz Nextport
sub dx,7
mov ds:[AtapiPort],dx
call ReadBuf
jmp EndCheck
NextPort:
loop CheckNext
EndCheck:
popad
ret
CheckSataPort endp




SendCommand proc near
cli
pushad
mov dx,ds:[AtapiPort]
add dx,7
call WaitFree
call WaitRdy
mov al,0a0h
out dx,al
call WaitFree
call WaitRdy
call WaitRqt
mov dx,ds:[AtapiPort]
inc dx
mov al,ds:[DMAflag]
out dx,al
add dx,3
in al,dx
mov cl,al
inc dx
in al,dx
mov ch,al
inc dx
mov al,ds:[SlaveFlag]
out dx,al
shr cx,1
mov cx,6				;cx===6
mov dx,ds:[AtapiPort]
mov si,ds:[CommandEA]
rep outsw
add dx,7
call WaitFree
call WaitRdy
popad
sti
ret
SendCommand endp



ReadBuf Proc near
pushad
mov dx,ds:[AtapiPort]
add dx,4
in al,dx
mov cl,al
inc dx
in al,dx
mov ch,al

Resend:
push cx
shr cx,1
mov dx,ds:[AtapiPort]
mov di,offset DataBuf
rep insw
add dx,5
in al,dx
mov ah,al
dec dx
in al,dx
pop cx
cmp ax,cx
jnz Resend
mov dx,ds:[AtapiPort]
add dx,7
call WaitFree
call WaitRdy
popad
ret
ReadBuf endp



WaitFree proc near
in al,dx
test al,80h
jnz WaitFree
ret
WaitFree endp



WaitRdy proc near
in al,dx
test al,40h
jz WaitRdy
ret
WaitRdy endp

WaitRqt proc near
in al,dx
test al,8
jz WaitRqt
ret
WaitRqt endp



WaitTime proc near
pushad
sti
mov ax,40h
mov fs,ax
mov eax,dword ptr fs:[6ch]
add eax,1
WaitSec:
mov ebx,dword ptr fs:[6ch]
cmp eax,ebx
jae WaitSec
popad
ret
WaitTime endp


AtaPrimary proc near
pushad
mov al,ds:[SlaveFlag]
out dx,al
inc dx
mov al,0a1h
out dx,al
call WaitTime
in al,dx
cmp al,58h
jnz NotAtaPort
sub dx,7
mov ds:[AtapiPort],dx
call ReadBuf
NotAtaPort:
popad
ret
AtaPrimary endp


Quit proc near
mov ah,4ch
int 21h
Quit endp



CommandEA		dw 0
DmaFlag			db 1
SlaveFlag		db 0
AtapiPort 		dw 0
SataPort 		db 40h dup (0)
SataPortNum 		dw 0
DataBuf			dw 200h dup (0)
PacketOpen 		db 1bh,3 dup (0),2,7 dup (0)
PacketClose 		db 1bh,3 dup (0),3,7 dup (0)
msgOpen			db 'Press any key to Eject the CD_ROM:',24h
msgClose		db 0ah,0dh,'Press any Key to Close the Tray:',24h
msgNoDevice		db 'Not Found CD_ROM!',0ah,0dh,24h
Code ends
end Start
相关推荐
无小道9 小时前
c++-引用(包括完美转发,移动构造,万能引用)
c语言·开发语言·汇编·c++
liulilittle2 天前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
工业互联网专业2 天前
汇编与接口技术:8259中断实验
汇编·单片机·嵌入式硬件·8259中断实验
small_wh1te_coder2 天前
硬件嵌入式学习路线大总结(一):C语言与linux。内功心法——从入门到精通,彻底打通你的任督二脉!
linux·c语言·汇编·嵌入式硬件·算法·c
Ronin-Lotus15 天前
微处理器原理与应用篇---ARM常见汇编指令
汇编·arm开发·微处理原理与应用
永夜的黎明17 天前
【二进制安全作业】250616课上作业1-栈溢出漏洞利用
c语言·汇编·安全
Geometry Fu20 天前
物联网控制技术 知识点总结 第三章 汇编语言 第四章 C51语言
汇编·物联网·51单片机
半桔20 天前
【Linux手册】进程的状态:从创建到消亡的“生命百态”
linux·运维·服务器·汇编·深度学习·面试
一条叫做nemo的鱼23 天前
从汇编的角度揭开C++ this指针的神秘面纱(下)
java·汇编·c++·函数调用·参数传递
一条叫做nemo的鱼24 天前
从汇编的角度揭开C++ this指针的神秘面纱(上)
汇编·c++·算法·函数调用·this指针·参数传递