パソコン活用研究シリコンバレー(C、C++、の活用研究)


ディスクへのアクセス
―準備中―


ディスクへのアクセスの練習としてセクタNoを指定してダンプするプログラムを創ります。
上がTurboCのabsreadというディスクアクセスの関数を使ったもの
下はシステムコールINT 25hを使うもの。


#include <dos.h>
#include <stdio.h>
#include <ctype.h>

#define secsize 1024

main()
{
int i,j,drive,sectno;
unsigned char buf[secsize];
printf(" Drive  sect-No ? ");scanf("%d  %d", &drive, &sectno);

absread(drive,1,sectno,buf);
for (i=0; i<secsize/16;i++){
printf("%04x:  ",i*16);
for(j=0;j<16;j++) printf("%02x ",buf[i*16+j]);
for (j=0;j<16;j++){
if (isprint(buf[i*16+j])) putchar(buf[i*16+j]);
else
putchar('.');
}
putchar('\n');
}
}
Turbo C用
/* For PC-98? */
#include <dos.h>
#include <stdio.h>
#include <ctype.h>
#define secsize 1024
union REGS inregs,outregs;
main()
{
int i,j,drive,sectno;
unsigned char buf[secsize];
printf("Drive sect-No ? ");scanf("%d %d",&drive, &sectno);
inregs.h.al=drive;
inregs.x.bx=(int)buf;
inregs.x.cx=1;
inregs.x.dx=sectno;
int86(0x25,&inregs,&outregs);
for (i=0; i<secsize/16;i++){
printf("%04x: ",i*16);
for(j=0;j<16;j++) printf("%02x ",buf[i*16+j]);
for (j=0;j<16;j++){
if (isprint(buf[i*16+j])) putchar(buf[i*16+j]);
else
putchar('.');
}
putchar('\n');
}
}


テキストへの書き出し機能を追加したバージョン

int25v3.c

/* INT25 ver 0.0.3   INT25Hによるディスク直接リード
   Coded by Tsuyoshi kasai
   Compiled by Turbo C(PC/AT)                                */

#include <string.h>
#include <stdlib.h>
#include <dos.h>
#include <stdio.h>
#include <ctype.h>
#define secsize 1024

void text (int);
union REGS inregs,outregs;
unsigned char buf[secsize];

main()
{
int i,j,drive,sectno;
char c;

printf("***INT25 ver 0.0.3****\nINT25H Disk direct read   Coded by Tsuyoshi Kasai\n");
printf("Compiled by Turbo C(PC/AT)\n");
st:
printf("\nDrive  sect-No ? ");scanf("%d  %d",&drive, &sectno);
inregs.h.al=drive;
inregs.x.bx=(int)buf;
inregs.x.cx=1;
inregs.x.dx=sectno;
int86(0x25,&inregs,&outregs);
for (i=0; i<secsize/16;i++){
printf("%04x:  ",i*16);
for(j=0;j<16;j++) printf("%02x ",buf[i*16+j]);
for (j=0;j<16;j++){
if (isprint(buf[i*16+j])) putchar(buf[i*16+j]);
else
putchar('.');
}
putchar('\n');
}

putchar('\n');
printf("Complete\n");
printf("Output to Text File  Y/N ?");
scanf("%s",&c);
if (c=='Y' || c=='y')  text(sectno);
printf("Continue? Y/N ?");
scanf("%s",&c);
if (c=='Y' || c=='y') goto st;
}

void text(int sectno){
FILE *fp;
 static char fname[40];
int i,j;
printf("File Name ?");
scanf("%s",fname);
if (strlen(fname)<=0) exit(1);
fp=fopen(fname,"w");
if (fp == NULL){ printf("File cannot be opened");fclose(fp);exit(1);}
fprintf(fp, "Sec No. %04x \n", sectno);
for (i=0; i<secsize/16;i++){
fprintf(fp,"%04x:  ",i*16);
for(j=0;j<16;j++) fprintf(fp,"%02x ",buf[i*16+j]);
for (j=0;j<16;j++){
if (isprint(buf[i*16+j])) putc(buf[i*16+j],fp);
else
putc('.',fp);
}
putc('\n',fp);
}
fclose(fp);
}

TurboCでコンパイル
VirtualBox上に仮想的に構築したWindows95のDOSモードで実行

Windosw95起動ディスクのセクタ0を書き出したところ

0000:  eb 34 90 29 36 54 37 72 49 48 43 00 02 01 01 00 .4.)6T7rIHC.....
0010:  02 e0 00 40 0b f0 09 00 12 00 02 00 00 00 00 00 ...@............
0020:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 ................
0030:  00 00 00 00 01 00 fa 33 c0 8e d0 bc 00 7c 16 07 .......3.....|..
0040:  bb 78 00 36 c5 37 1e 56 16 53 bf 2b 7c b9 0b 00 .x.6.7.V.S.+|...
0050:  fc ac 26 80 3d 00 74 03 26 8a 05 aa 8a c4 e2 f1 ..&.=.t.&.......
0060:  06 1f 89 47 02 c7 07 2b 7c fb cd 13 72 67 a0 10 ...G...+|...rg..
0070:  7c 98 f7 26 16 7c 03 06 1c 7c 03 06 0e 7c a3 3f |..&.|...|...|.?
0080:  7c a3 37 7c b8 20 00 f7 26 11 7c 8b 1e 0b 7c 03 |.7|. ..&.|...|.
0090:  c3 48 f7 f3 01 06 37 7c bb 00 05 a1 3f 7c e8 9f .H....7|....?|..
00a0:  00 b8 01 02 e8 b3 00 72 19 8b fb b9 0b 00 be d6 .......r........
00b0:  7d f3 a6 75 0d 8d 7f 20 be e1 7d b9 0b 00 f3 a6 }..u... ..}.....
00c0:  74 18 be 77 7d e8 6a 00 32 e4 cd 16 5e 1f 8f 04 t..w}.j.2...^...
00d0:  8f 44 02 cd 19 be c0 7d eb eb a1 1c 05 33 d2 f7 .D.....}.....3..
00e0:  36 0b 7c fe c0 a2 3c 7c a1 37 7c a3 3d 7c bb 00 6.|...<|.7|.=|..
00f0:  07 a1 37 7c e8 49 00 a1 18 7c 2a 06 3b 7c 40 38 ..7|.I...|*.;|@8
0100:  06 3c 7c 73 03 a0 3c 7c 50 e8 4e 00 58 72 c6 28 .<|s..<|P.N.Xr.(
0110:  06 3c 7c 74 0c 01 06 37 7c f7 26 0b 7c 03 d8 eb .<|t...7|.&.|...
0120:  d0 8a 2e 15 7c 8a 16 fd 7d 8b 1e 3d 7c ea 00 00 ....|...}..=|...
0130:  70 00 ac 0a c0 74 22 b4 0e bb 07 00 cd 10 eb f2 p....t".........
0140:  33 d2 f7 36 18 7c fe c2 88 16 3b 7c 33 d2 f7 36 3..6.|....;|3..6
0150:  1a 7c 88 16 2a 7c a3 39 7c c3 b4 02 8b 16 39 7c .|..*|.9|.....9|
0160:  b1 06 d2 e6 0a 36 3b 7c 8b ca 86 e9 8a 16 fd 7d .....6;|.......}
0170:  8a 36 2a 7c cd 13 c3 0d 0a 4e 6f 6e 2d 53 79 73 .6*|.....Non-Sys
0180:  74 65 6d 20 64 69 73 6b 20 6f 72 20 64 69 73 6b tem disk or disk
0190:  20 65 72 72 6f 72 0d 0a 52 65 70 6c 61 63 65 20  error..Replace 
01a0:  61 6e 64 20 73 74 72 69 6b 65 20 61 6e 79 20 6b and strike any k
01b0:  65 79 20 77 68 65 6e 20 72 65 61 64 79 0d 0a 00 ey when ready...
01c0:  0d 0a 44 69 73 6b 20 42 6f 6f 74 20 66 61 69 6c ..Disk Boot fail
01d0:  75 72 65 0d 0a 00 49 42 4d 42 49 4f 20 20 43 4f ure...IBMBIO  CO
01e0:  4d 49 42 4d 44 4f 53 20 20 43 4f 4d 00 00 00 00 MIBMDOS  COM....
01f0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa ..............U.
0200:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0210:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0220:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0230:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0240:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0250:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0260:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0270:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0280:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0290:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
02a0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
02b0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
02c0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
02d0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
02e0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
02f0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0300:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0310:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0320:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0330:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0340:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0350:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0360:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0370:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0380:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0390:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
03a0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
03b0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
03c0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
03d0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
03e0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
03f0:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

LSIC86でコンパイルしたほうは、ディスクの読み込みで止まる


 1 






TopPage