int 13h tutorial

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
H@cker

int 13h tutorial

Post by H@cker »

Im trying to make my own OS, and Im stuck with "int 13h". Is there a tutorial about this? For example, I know that this will read from the floppy disk:
mov ah,02h ;When ah=, int13 reads a disk sector
mov al,2              ;Al is how many sectors to read
mov ch,0              ;The track to read from
mov cl,2              ;Sector Id
mov dh,0              ;Head
mov dl,0              ;Drive (0 is floppy)
mov bx,0x1000    ;Es and Bx put together are where to load the program too (see jmp 0x1000:0x00)
mov es,bx
mov bx,0x00
int 13h               ;Int 13 is all functions for disks

Then, jmp 0x1000:0x00 is called, and the sectors will be started. OK; Fine, but now I would like to read a sector to a DB (Don't know how that's called, but you use it with "test DB 0".). How can I do that. And, that 0x1000:0x00, can I change that into anything, or does it has to be 0x1000:0x00?

So 3 questions:
1) A tutorial about int 13h
2) How can I read the sector into "test DB 0"
3) Does it has to be 0x1000:0x00 in my example, or can I change it into for example 0x1234:0x12
drizzt

Re:int 13h tutorial

Post by drizzt »

1) from Ralph Brown interrupt list:
INT 13 - DISK - READ SECTOR(S) INTO MEMORY
???AH = 02h
???AL = number of sectors to read (must be nonzero)
???CH = low eight bits of cylinder number
???CL = sector number 1-63 (bits 0-5)
??? high two bits of cylinder (bits 6-7, hard disk only)
???DH = head number
???DL = drive number (bit 7 set for hard disk)
???ES:BX -> data buffer
Return: CF set on error
??? if AH = 11h (corrected ECC error), AL = burst length
???CF clear if successful
???AH = status (see #0140)
???AL = number of sectors transferred
Notes:???errors on a floppy may be due to the motor failing to spin up quickly
??? enough; the read should be retried at least three times, resetting
??? the disk with AH=00h between attempts
???the IBM AT BIOS and many other BIOSes use only the low four bits of
??? DH (head number) since the WD-1003 controller which is the standard
??? AT controller (and the controller that IDE emulates) only supports
??? 16 heads
???AWARD AT BIOS and AMI 386sx BIOS have been extended to handle more
??? than 1024 cylinders by placing bits 10 and 11 of the cylinder number
??? into bits 6 and 7 of DH
SeeAlso: AH=03h,AH=0Ah,AH=06h"V10DISK.SYS",AH=21h"PS/1",INT 4D/AH=02h

3) Yes.

2) i haven't understood well your question... you intend to read a byte into a sector?! if so you can simply read the sector and put it into a location in memory. Every sector is 512byte (for standard 1.44MB disks) so if you put it for example at 0x1234:0x12 then [0x1234:0x12] is the first byte of the sector... and so on..
H@cker

Re:int 13h tutorial

Post by H@cker »

Ok, 10x but with question 2 i mean:
Ive got this source:

BlahBlah times 512 DB 0

Now I want to read a cluster, and get it INSIDE BlahBlah. So that I can do for example:
mov si, BlahBlah
call PrintSI ; This is a function to Print the value inside SI
(Might be a wrong, im not very good at assembly, yet)
Schol-R-LEA

Re:int 13h tutorial

Post by Schol-R-LEA »

H@cker wrote: Ok, 10x but with question 2 i mean:
Ive got this source:

BlahBlah times 512 DB 0

Now I want to read a cluster, and get it INSIDE BlahBlah. So that I can do for example:
mov si, BlahBlah
call PrintSI ; This is a function to Print the value inside SI
(Might be a wrong, im not very good at assembly, yet)
OK, well, that's fairly easy. The simpe answer is, you can put it anywhere you want, by setting ES and BX to the address you want before calling the interrupt. In this case, you want ES == DS and BX == *BlahBlah.

To give a bit more detail:
The routine called by INT13h, ah=2h will read the number of bytes held in AL starting from the head, cylinder and sector held in CL, CH, and DL respectively, into memory beginning at (and this is the part you seem to be having trouble with) the segment held in ES and the offset held in BX. So, if you want to write to a 512-byte space allocated within the existing data segment, you would do something like this:

Code: Select all

;;; some code 

   mov cx, 3    ; countdown of read attempts

read_loop:
   xor ah, ah    ; set ah to zero - reset drive function
   int 0x13       ; call drive reset 

   mov ax, ds
   mov es, ax          ; es == ds
   mov bx, BlahBlah ; set BX to the address (not the value) of BlahBlah 
   mov dl, DriveNumber
   mov dh, HeadNumber
   mov al, NumSectors 
   mov ch, CylNumLow
   mov cl, CylNumHigh ; set the high part of the cylinder number, bits 6 and 7 
   and cl, Sector ; set the sector number, bits 0-5
   mov ah, 0x2 ; set function 2h
   int 0x13        ; call the interrupt
   jnc exit   ; if the carry flag is clear, it worked
   loop read_data ; try three times, then give up - leave error msg in al

exit:
;;; whatever other code you need

[segment data]
BlahBlah resb 512 

(This is not tested code - use at your own risk)

It is a Really Good Idea to try the disk read at least three times, as disk controllers tend to be less than perfectly reliable.

If you need a larger area of memory - say, a whole segment's worth - you can simply set up ES in some other location, and set BX to zero (the segment beginning) or some other convenient value as needed.
H@cker

Re:int 13h tutorial

Post by H@cker »

OK! 10X!

One more question: Where can I find the complete ah list (What the input and output is, what it does (for example that mov ah, 2 is read the disk and you can check for errors with jc))? For example: What value do I have to move into ah to get the drive site? Or the cluster size? Or the ammount of clusters?
Schol-R-LEA

Re:int 13h tutorial

Post by Schol-R-LEA »

H@cker wrote: OK! 10X!

One more question: Where can I find the complete ah list (What the input and output is, what it does (for example that mov ah, 2 is read the disk and you can check for errors with jc))? For example: What value do I have to move into ah to get the drive site? Or the cluster size? Or the ammount of clusters?
Most of the information you want can be found in Ralf Brown's Interrupt List; there are also several browsable versions as well.
Post Reply