Solved

How do I use Int 13h (BIOS call) to directly read from and write to hard disks?

Posted on 2001-06-05
6
638 Views
Last Modified: 2008-02-01
I am attempting to create a program that will allow me to make a cloned image of my hard disk. In other words, my system has 2 hard drives, in which one disk is my boot drive, and the second drive is supposed to be a mirror of the first drive. Both drives are alike, with 16 heads and 63 sectors per cylinder each.

I have included an assembly language (Turbo Assembler) listing of my program, below. Any insight as to why my program is not functing properly would be greatly appreciated. Please note that my e-mail address has changed: it is now RAYIDEA464@CS.COM

.model small
.386
.data

  disk_buffer     dw     16128     dup(?) ;512 bytes * 63

.stack 16384
.code

  mov     sp,16384                   ;Place stack hi

begin:    jmp short main
main      proc near

  mov     ax,@data
  mov     ds,ax
  mov     es,ax                      ;Point ES to disk buf

  mov     ch,0                       ;1st cylinder of disk
  mov     cl,1                       ;1st sector of disk
  mov     dh,0                       ;1st read-write head

Retry:

  mov     ah,2                       ;Read a sector to mem
  mov     al,63                      ;Entire track (cyl)
  mov     bx,offset disk_buffer      ;Sectors go to memory
  mov     dl,80h                     ;1st hard disk.
  push    si
  push    di
  int     13h
  pop     di
  pop     si

  mov     ah,3                       ;Write sect to drive
  mov     al,63                      ;Entire track (cyl)
  mov     bx,offset disk_buffer      ;Sects go to 2nd disk
  mov     dl,81h                     ;2nd hard disk
  push    si
  push    di
  int     13h
  pop     di
  pop     si

  inc     ch
  cmp     ch,255
  jb      Retry

  inc     dh
  mov     ch,0

  cmp     dh,15
  jbe     Retry

  mov     ax,4c00h                   ;Exit to
  int     21h                        ;DOS    

main endp
end  begin
0
Comment
Question by:rgoff
  • 4
6 Comments
 
LVL 24

Expert Comment

by:SunBow
ID: 6156071
You skipped two important clues, one, what makes you think it is not working (error msg?), two, what you think is working about it?
0
 
LVL 24

Accepted Solution

by:
SunBow earned 300 total points
ID: 6156305
I lean towards wanting feedback, display. In such code I would make a call to a display routine to show the error code, right after the compare(s), and be in less of a hurry to retry. CY indicates success, as well as a clearing of AH. If CY or AH <>0 then AH has error code.

FWIW: per M Tischer, Abacus, "The contents of the BX, CX, DX, SI, DI, BP registers and the segment registers are not affected by this function. The contents of all other registers may change."

(in truth, I forget my own such coding days of what to push/pop, but moving that code or rem'ing it helps you do the Sherlock technique of eliminating possibles, leaving the case of "then it must be")

I appreciate the in-line comments, leaving this one documented fairly well.

FWIW: For another rainy day, another quote: "bits 6 & 7 of the CL register (sector number) form bits 8 & 9 of the cylinder number" (achieves up to 1023 cylinders at a time). If several sectors are being read and the system reaches the last sector of the cylinder, reading continues on the first sector of the next cylinder of the next read/write head.

On occasion, it is wise to run your own breakout routine to display register content, like immediately before and after the interrupt(s). I think Turbo will do it, itself, my goal would be for the before/after displays to both be on screen at the same time during the debug. Easier to then see what, if anything, changes.

btw: You need to really validate each parameter succinctly. Even the 80H, 81h can have been changed for newer media or concentrated configurations. Some controllers & drives remap things like cylinders and heads to pack in more in a different way. This is not important for cloning/copying, per se, but it is important to validate the quantity available of each parameter. I'll assume for now you have done so, and are accurate on the counts. Or can adjust that later after some other 'bug' has been identified.
FWIW.
0
 
LVL 24

Expert Comment

by:SunBow
ID: 6156947
See also:
http://turing.csc.smith.edu/~thiebaut/ArtOfAssembly/CH13/CH13-2.html#HEADING2-73
Noting, only 128 sectors can be accessed at a time (64K DMA limit) and that DMA can only transfer to one memory segment at a time, up to 64K but not across border. ES:BX buffer must fit into the 64K starting with ES or DMA may report error (likely # 09H). You might consider going flexible, increasing buffer to max then commenting how much is used for the special case.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 24

Expert Comment

by:SunBow
ID: 6161103
Newz?
0
 
LVL 16

Expert Comment

by:t0t0
ID: 6263359
SunBow suggests displaying results at critical stages as I do.

After the call to Int 13H, examine the carry flag. This should be clear if the read is successful. AH should also be 00H and AL will be set to the number of sectors transfered.

If the carry flag is set, look at AH to determine the status in Int 13H, Function 01H.

Be aware of ECC corrected data errors (AH = 11H).  In this case, AL contains the burst length.  The data returned is probably good but there's a small chance the data was not corrected properly.

(Another common mistake is not remembering that the upper 2 bits of the 10-bit cylinder number are placed in the upper 2 bits of CL).

How about using Function 04H to verify the destination?

How about performing a compare of the contents of both drives after each write to confirm they are the same.  This could easily be added to the code after the write portion and the results displayed on the screen.

(Another common oversight is with multi-sector transfers where the operation was terminated after the sector containing the read error).


Other than that, I'm gonna have to get my text books out.... and nobody likes to do that, do they?



0
 

Author Comment

by:rgoff
ID: 6292399
Dear SunBow: Thank you for your info. If you would like to
provide any additional information, my email address is:
RAYIDEA464@CS.COM
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now