943,522 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 841
  • C RSS
Feb 8th, 2009
0

Individual Sector Reading in C

Expand Post »
Hi All,
I am currently trying to write a program that can read individual sectors on a floppy disk using C. I am building the program to run in a 16-bit DOS environment using the Digital Mars compiler with the 16bit DOS add-on. So far I know how to access the individual sectors but what I want to do is then extract the data and put it into a file. I think that the way to do this will be to use malloc to provide the space to put the data into from the registers but I am getting confused as to how to get this data into there in the start (wether to use a struct or what) and also how to output the data and in what form (%c, %d, etc.).
If you could offer any advice it would be greatly appreciated.
Dinklebaga
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dinklebaga is offline Offline
7 posts
since Dec 2008
Feb 8th, 2009
0

Re: Individual Sector Reading in C

Click to Expand / Collapse  Quote originally posted by dinklebaga ...
Hi All,
I am getting confused as to how to get this data into there in the start (wether to use a struct or what) and also how to output the data and in what form (%c, %d, etc.).
If you could offer any advice it would be greatly appreciated.
Dinklebaga
For reading a sector, simply you should define a buffer such as
  1. char buff[512];
then use "%X" to print the hex format.
Reputation Points: 41
Solved Threads: 3
Newbie Poster
xitrum69 is offline Offline
15 posts
since Aug 2008
Feb 9th, 2009
0

Re: Individual Sector Reading in C

Hi xitrum,
I decided to go down a different route using malloc to allocate it a section of memory of 512 or multiples of depending on how many sectors i wanted to read and then allocating this pointer to the es register (where the memory location for the sector to be read into usually goes) and so far it isn't kicking out an coding error during compilation but when I am trying to simply print this section of memory out using the pointer as reference the results are not what I am expecting. Below is the code that i have so far:

  1. #include <dos.h>
  2. #include <stdio.h>
  3.  
  4. /* reads a given sector on the cylinder, head and drive (cyl, hd & dr)*/
  5.  
  6. read_sector(cyl, hd, dr)
  7. char cyl, hd, dr;
  8. {
  9. int status;
  10. int *memsectptr;
  11. int *tempptr;
  12. int sectorsize = 512;
  13. int noofsectors = 1;
  14. int i;
  15. char buff[512];
  16. *memsectptr = malloc(sectorsize); /*open 512 bytes of data in memory to read in sector */
  17. tempptr = memsectptr;
  18. _AH = 0x02; /* read sectors instead of verify command */
  19. _AL = noofsectors; /* read one sector only */
  20. _CH = cyl;
  21. _CL = 1; /* starts at sector 1*/
  22. _DH = hd;
  23. _DL = dr;
  24. //_BX = 0x00; /* no offset for data to be stored */
  25. ES:_BX = *memsectptr; /*Add memory location in to read data to */
  26. geninterrupt(0x13);
  27. for(i=0; i<(sectorsize * noofsectors); i++)
  28. {
  29. printf("%X", memsectptr);
  30. }
  31. free(memsectptr);
  32. status = _AH;
  33. return(status);
  34. }
  35.  
  36. /* takes the status code returned by verify track and prints message associated with it */
  37.  
  38. pr_error(error)
  39. int error;
  40. {
  41. switch(error)
  42. {
  43. case 0x01:
  44. printf("Invalid Command\n");
  45. break;
  46. case 0x02:
  47. printf("Address Mark Not Found\n");
  48. break;
  49. case 0x03:
  50. printf("Disk Write Protected\n");
  51. break;
  52. case 0x04:
  53. printf("Sector Not Found\n");
  54. break;
  55. case 0x05:
  56. printf("Reset Failed\n");
  57. break;
  58. case 0x06:
  59. printf("Floppy Disk Removed\n");
  60. break;
  61. case 0x08:
  62. printf("DMA Overrun\n");
  63. break;
  64. case 0x09:
  65. printf("DMA Crossed 64k Boundary\n");
  66. break;
  67. case 0x0C:
  68. printf("Media Type Not Found\n");
  69. break;
  70. case 0x10:
  71. printf("CRC Error\n");
  72. break;
  73. case 0x20:
  74. printf("Controller Failed\n");
  75. break;
  76. case 0x40:
  77. printf("Seek Failed\n");
  78. break;
  79. case 0x80:
  80. printf("\n status %d\n", error);
  81. }
  82. }
  83.  
  84. /* main calls verify_track for all values of cyl (0-79) and both values for head (0-1). If function succeeds it returns 0
  85. otherwise it returns the relevant error code to printf and terminates program.*/
  86.  
  87. main()
  88. {
  89. int status = 0;
  90. char head, cylinder;
  91. char drive = 0;
  92. head = 0;
  93. cylinder = 0;
  94. status = read_sector(cylinder, head, drive);
  95. if( status != 0)
  96. {
  97. printf("\nError at head %d, cylinder %02d :", head, cylinder);
  98. pr_error(status);
  99. }
  100. else
  101. {
  102. printf("\nOperation completed successfully. See output\n");
  103. }
  104.  
  105.  
  106. }

Thanks, Dinklebaga
Last edited by dinklebaga; Feb 9th, 2009 at 11:52 am. Reason: syntax highlighting
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dinklebaga is offline Offline
7 posts
since Dec 2008
Feb 9th, 2009
0

Re: Individual Sector Reading in C

hix you got some mistakes in C pointer programming.
1/ You declared a pointer variable and allocated it by using *memsectptr ????
  1. int *memsectptr;
  2. *memsectptr = malloc(sectorsize);
  3. ....
  4. ES:_BX = *memsectptr;
the code should be
  1. int *memsectptr;
  2. memsectptr = malloc(sectorsize);
  3. ....
  4. ES:_BX = memsectptr;

and the following code will print data in a sector
  1. for(i=0; i<(sectorsize * noofsectors); i++)
  2. {
  3. printf("%X", *(memsectptr+i));
  4. }

xitrum
Reputation Points: 41
Solved Threads: 3
Newbie Poster
xitrum69 is offline Offline
15 posts
since Aug 2008
Feb 10th, 2009
0

Re: Individual Sector Reading in C

Click to Expand / Collapse  Quote originally posted by xitrum69 ...
hix you got some mistakes in C pointer programming.
1/ You declared a pointer variable and allocated it by using *memsectptr ????
  1. int *memsectptr;
  2. *memsectptr = malloc(sectorsize);
  3. ....
  4. ES:_BX = *memsectptr;
the code should be
  1. int *memsectptr;
  2. memsectptr = malloc(sectorsize);
  3. ....
  4. ES:_BX = memsectptr;

xitrum
Thanks for your help with this xitrum. Unfortunately with this first bit which you told me was incorrectly referenced, when I remove the * from in front of the pointers i receive the error "Error: need explicit cast to convert from int to int*, or in the case of the second: to convert from int* to short volatile. Forgive me for being naiive but I don't understand the differences/consequences of leaving the * in place as they are kicking out an error?

Also when running the code with the *'s in place with the second modification I am receiving a better output (characters are now different from the first) but ur seems to me just to be an illogical memory dump making me think that possibly the program isn't actually reading the sector into memory in the first place?

Thanks,
Dinklebaga
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dinklebaga is offline Offline
7 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Regarding XDR
Next Thread in C Forum Timeline: Need Help understanding Pointer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC