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

Posted on 2001-06-05
Medium Priority
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

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

.stack 16384

  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


  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
Question by:rgoff
  • 4
LVL 24

Expert Comment

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?
LVL 24

Accepted Solution

SunBow earned 1200 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.
LVL 24

Expert Comment

ID: 6156947
See also:
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.
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

LVL 24

Expert Comment

ID: 6161103
LVL 16

Expert Comment

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?


Author Comment

ID: 6292399
Dear SunBow: Thank you for your info. If you would like to
provide any additional information, my email address is:

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

589 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