DOS下读取PCI配置空间信息的汇编程序(通过IOCF8/IOCFC)

汇编程序编写的读取PCI配置空间信息的代码(通过IOCF8/IOCFC):

  1 ;------------------------------------------------
  2 ;功能: 读取PCI 配置信息,存入文件zpci_config.txt     
  3 ;环境: DOS + MASM5
  4 ;时间: 2015/08
  5 ;说明: 通过端口CF8h / CFCh 来读取
  6 ;
  7 ;---------------------自定义宏结构-------------------
  8 ;功能: 在文件中换行
  9 nextrow macro
 10     mov buffer  ,0dh
 11     mov buffer+1,0ah
 12     mov dx,offset buffer
 13     mov cx,2
 14     mov ah,40h
 15     int 21h
 16     endm
 17 ;功能:把ascii 表示的字符写入文件
 18 tofile macro ascii    
 19     mov buffer,ascii 
 20     mov dx,offset buffer
 21     mov cx,1      
 22     mov ah,40h  
 23     int 21h
 24     endm
 25 ;------------------------------------------------
 26     .386P
 27 ;-------------------------------------------------
 28 dseg segment use16
 29     busnum dw 0000h  ;总线号0 - 00FFh
 30     devnum dw 001fh  ;设备号0 - 001Fh
 31     funnum dw 0007h  ;功能号0 - 0007h
 32     regnum dw 00ffh  ;寄存器0 - 00FFh
 33     ;
 34     config_addr dd 00000000h      ;用来暂存eax中的地址
 35     buff_num db 'bus:device:function:'
 36     ;
 37     config_data dd 00000000h      ;用来存放eax中的pci数据
 38     fname db '\zpci_config.txt',0 ;文件名
 39     buffer db 2
 40 dseg ends
 41 ;----------------------------------------------------
 42 ;----------------------------------------------------
 43 cseg segment use16
 44 assume cs:cseg, ds:dseg
 45 start:
 46     mov ax,dseg
 47     mov ds,ax
 48     ;
 49     mov dx,offset fname  
 50     mov cx,0      ;common file
 51     mov ah,3ch    ;creat file
 52     int 21h
 53     ;
 54     mov bx,ax     ;save file handle
 55     ;
 56     mov busnum,0000h
 57     mov devnum,0000h
 58     mov funnum,0000h
 59     mov regnum,0000h
 60 ;-----------------------------------------
 61     call print_num    ;打印busnum:devnum:funnum = 00:00:00
 62     nextrow           ;换行
 63 nextreg:    
 64     call pci_read     ;读取pci 配置空间
 65     cmp regnum,00h
 66     jnz  continue     ;判断不是第一个寄存器
 67     cmp ax,0ffffh     ;判断设备是否存在
 68     jz nextfun        ;不存在,跳到下一个fun
 69 continue:
 70     call writefile
 71     add regnum,4      ;只能每次读4个寄存器
 72     cmp regnum,00ffh  ;判断
 73     ja nextfun        ;256B 已读完,跳到下一个function
 74     jmp nextreg       ;否则,读下一个reg
 75 nextfun:
 76     nextrow 
 77     ;
 78     mov regnum,0000h
 79     inc funnum
 80     cmp funnum,0007h
 81     ja nextdev        ;funnum 大于 7,跳到下一个dev    
 82     call print_num   
 83     nextrow     
 84     jmp nextreg    
 85 nextdev:
 86     mov regnum,0000h
 87     mov funnum,0000h
 88     inc devnum
 89     cmp devnum,001fh 
 90     ja nextbus      ;devnum 大于 1fh,跳到下一个bus
 91     call print_num    
 92     nextrow     
 93     jmp nextreg
 94 nextbus:
 95     mov regnum,0000h
 96     mov funnum,0000h
 97     mov devnum,0000h
 98     inc busnum
 99     cmp busnum,0005h
100     ja endd           ;busnum 大于5,跳到结束
101     call print_num 
102     nextrow
103     jmp nextreg    
104 ;-----------------------结束------------------------
105 endd:
106     mov ah,3eh   ;close file
107     int 21h
108     ;
109     mov ah,4ch   ;return DOS
110     int 21h
111 ;---------------------------------------------------
112 ;--------------------------------------------------
113 ;函数功能:打印busnum:devnum:funnum
114 print_num proc
115     mov config_addr,eax   ;保护eax中的地址
116     ;------------------------------------
117     mov dx,offset buff_num
118     mov cx,20
119     mov ah,40h    
120     int 21h
121     ;----------busnum------------
122     mov ax,busnum
123     push ax
124     shr al,4     
125     call toascii  
126     tofile al
127     pop ax
128     call toascii  
129     tofile al
130     tofile 2Dh
131     ;----------devnum----------
132     mov ax,devnum
133     push ax
134     shr al,4      
135     call toascii  
136     tofile al
137     pop ax
138     call toascii 
139     tofile al
140     tofile 2Dh
141     ;-----------funnum---------
142     mov ax,funnum
143     push ax
144     shr al,4      
145     call toascii  
146     tofile al
147     pop ax
148     call toascii 
149     tofile al
150     ;-----------
151     mov eax,config_addr    ;恢复eax
152     ret
153 print_num endp
154 ;------------------------------------------------------
155 ;---------------------- writefile ----------------------------
156 ;函数功能: 把eax 中的值写入文件
157 ;入口参数: eax
158 ;出口参数: 无
159 ;所用寄存器和存储单元:ebx,ecx,edx
160 writefile proc
161     mov config_data,eax   ;用config_data暂存eax中的pci数据
162     ;--------第一个字节-----
163     push eax
164     shr al,4     
165     call toascii 
166     tofile al
167     pop eax
168     call toascii  
169     tofile al
170     tofile 20h
171     ;--------第二个字节------
172     mov eax,config_data
173     shr eax,8
174     ;
175     push eax
176     shr al,4     
177     call toascii 
178     tofile al
179     pop eax
180     call toascii  
181     tofile al
182     tofile 20h
183     ;--------第三个字节-------
184     mov eax,config_data
185     shr eax,16
186     ;
187     push eax
188     shr al,4     
189     call toascii 
190     tofile al
191     pop eax
192     call toascii  
193     tofile al
194     tofile 20h
195     ;--------第四个字节---------
196     mov eax,config_data
197     shr eax,24
198     ;
199     push eax
200     shr al,4     
201     call toascii 
202     tofile al
203     pop eax
204     call toascii  
205     tofile al
206     tofile 20h
207     ret
208 writefile endp
209 ;---------------------------------------------------
210 ;-----------------------toascii---------------------------
211 ;子程序名: toascii
212 ;功能: 把al的低4位的值转成ascii码,存入al
213 ;入口参数: al
214 ;出口参数: al
215 toascii proc
216     and al,0fh 
217     add al,90h 
218     daa
219     adc al,40h
220     daa
221     ret
222 toascii endp
223 ;----------------------------------------------------
224 ;----------------------pci_read---------------------------
225 ;子程序名: pci_read
226 ;功能: 根据eax中的地址读取pci的配置空间,并存入eax
227 ;入口参数: busnum、devnum、funnum、regnum
228 ;出口参数: eax
229 ;
230 pci_read proc
231     ;protect register
232     push ebx     
233     push dx
234     ;clear
235     xor eax,eax
236     xor ebx,ebx
237     ;enable
238     add eax,1h
239     shl eax,31
240     ;bus number
241     mov ebx,ds:[00]
242     and ebx,0ffh
243     shl ebx,16
244     add eax,ebx
245     ;device number
246     xor ebx,ebx
247     mov ebx,ds:[02]
248     and ebx,0ffh
249     shl ebx,11
250     add eax,ebx
251     ;function number
252     xor ebx,ebx
253     mov ebx,ds:[04]
254     and ebx,0ffh
255     shl ebx,8
256     add eax,ebx
257     ;register
258     xor ebx,ebx
259     mov ebx,ds:[06]
260     and ebx,0ffh
261     add eax,ebx
262     ;read IO
263     mov dx,0cf8h
264     out dx,eax
265     mov dx,0cfch
266     in eax,dx
267     ;resume register
268     pop dx
269     pop ebx    
270     ret
271 pci_read endp
272 ;--------------------------------------------
273 ;----------------------------------------------
274 cseg ends
275     end start

 

转载于:https://www.cnblogs.com/nju347/p/7570737.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值