bochs调试操作系统,以及可引导img的制作

首先写这篇文章的目的是自己在用bochs调试操作系统的时候有很多关键的资料网上写的很乱,所以我根据自己的看书以及实践成功了,在这里分享一下某一些关键的步骤。

这篇文章假设你已经了解了部分知识了

1,你知道利用bximage.exe制作一个img文件。

2,你知道如何写汇编程序。

3,你知道如何利用nasw程序编译你写的汇编程序(nasw xxx.asm -o xxx.bin)。

4,你知道如何写bochs的配置文件(可以将dlxlinux里面的配置文件拿过来用)。

5,就是将你的编译之后的xxx.bin写入img文件就可以了。


运行结果截图:

bochs调试操作系统,以及可引导img的制作_第1张图片

MBRNew.asm

    org 07c00h
    mov ax, cs   
    mov ds, ax   
    mov es, ax                       
    call DispStr
    mov ax,0
    mov es,ax
    mov bx,8c00h
    mov al,1
    mov ch,0
    mov cl,2
    mov dl,0
    mov dh,0
    mov ah,2
    int 13h
    jmp 08c00h
DispStr:   
    mov ax, BootMessage   
    mov bp, ax
    mov cx, 15
    mov ax, 01301h 
    mov bx, 000ch
    mov dl, 0   
    int 10h
    ret   
BootMessage:   
    db "Muhong new MBR!"   
    times 510-($-$$) db 0
    dw 0xaa55


MBRSys.asm

    org 08c00h
    mov ax, cs   
    mov ds, ax   
    mov es, ax                       
    call DispStr
    jmp $
DispStr:   
    mov ax, BootMessage   
    mov bp, ax
    mov cx, 18
    mov ax, 01301h 
    mov bx, 000ch
mov dh, 1
    mov dl, 0   
    int 10h
    ret   
BootMessage:   
    db "Muhong system MBR!"


MBRCopyToImg.cpp

// OS.cpp : 定义控制台应用程序的入口点。
//
/************************************************************************/
/* 
用于将nasw编译之后的新的MBR写入img第一个扇区。
以及将系统的MBR写入img的第二个扇区。
*/
/************************************************************************/
#include "stdafx.h"
using namespace std;


char OS_IMG[] = "tinyos.img";
char MBR_NEW[] = "MBRNew.bin";
char MBR_SYS[] = "MBRSys.bin";


int write_mbrNew()
{
FILE *OSfp, *Mfp;
char buf[512]={0};


if((OSfp=fopen(OS_IMG,"r+"))==NULL){
cout<<"can not open file"<<endl;
return -1;
}
if((Mfp=fopen(MBR_NEW,"r"))==NULL){
cout<<"can not open file"<<endl;
return -1;
}


fseek(OSfp,0,SEEK_SET);
fseek(Mfp,0,SEEK_SET);


fread(buf,512,1,Mfp);
fwrite(buf,512,1,OSfp);


fclose(Mfp);
fclose(OSfp);
return 0;
}


int write_MBRSys()
{
FILE *OSfp, *Mfp;
int fileLen; //
char buf[512]={0};


if((OSfp=fopen(OS_IMG,"r+"))==NULL){
cout<<"can not open file"<<endl;
return -1;
}
if((Mfp=fopen(MBR_SYS,"r"))==NULL){
cout<<"can not open file"<<endl;
return -1;
}
fseek(OSfp,512,SEEK_SET);
fread(buf,1,512,Mfp);
fseek(Mfp,0, SEEK_END);


fileLen = ftell(Mfp);
fseek(Mfp,0, SEEK_SET);


fwrite(buf, 1, fileLen, OSfp);
system("pause");
fclose(Mfp);
fclose(OSfp);
}




int _tmain(int argc, _TCHAR* argv[])
{
write_mbrNew();
write_MBRSys();
return 0;
}


你可能感兴趣的:(c,汇编,操作系统,OS,调试)