Solved

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

Posted on 2001-06-05
6
644 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

803 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