bios磁盘磁盘低级操作

    木有操作系统的电脑要靠简单的bios中断程序操作计算机设备

    调用中断程序的一般方法为先查手册设置好相关寄存器的值,然后调用中断指令。

    13h中断为磁盘操作。

    ah = 3 写磁盘

    ah = 2 读磁盘#include <dos.h> //dos接口

    驱动器号送入dl

    磁头号送入dh

    磁道号送入ch

    扇区号送入cl

    扇区数送入al

    es内存段地址

    bx内存偏移地址

 

    比如我需要把一个512字节的启动二进制代码写入软盘的启动扇区:

union REGS inreg,outreg;
struct SREGS segreg;

int main(){
     char boot_buf[512];
     //读取文件保存到buf
    inreg.h.ah=0x03;                             /*调用bios13h的3号写盘功能*/
    inreg.h.al=0x1;                              /*要读的扇区数为1*/
    inreg.h.ch=0;                                /*磁道号为0*/
    inreg.h.cl=1;                                /*扇区号为1*/
    inreg.h.dh=0;                                /*磁头号为0*/
    inreg.h.dl=0;                                /*驱动器号为0,即a盘*/
    inreg.x.bx=FP_OFF(boot_buf);                 /*bx中写boot_buf的内存偏移地址*/
    segreg.es=FP_SEG(boot_buf);                  /*es中写它的内存段地址*/
    int86x(0x13,&inreg,&outreg,&segreg);         /*调中断写盘*/
    if(0 == _AH){
        printf("success");
    }else{
        printf("failed");
    }
    return 0;

}
 

猜你喜欢

转载自westice.iteye.com/blog/1063356