flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Copy sectors routine

Author
Thread Post new topic Reply to topic
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
I've written a read loop to read 64 sectors from the 2nd sector onwards to accomodate all the IDT stuff and allow me to have plenty of space to expand my second stage loader and kernel, but for some reason my copy routine doesn't work Sad

I worked out by hand that the loop should do this for the int 0x13 read calls:
Code:
        ;   ax   es   bx   cx   dx   si
        ;  0211 0800 0000 0002 0000 FFC0
        ;  0212 0A20 0000 0001 0100 FFD1
        ;  0212 0C60 0000 0101 0000 FFE3
        ;  020B 0EA0 0000 0101 0100 FFF5
        ; (020C 1000 0000 010C 0100 0000)
    


Using this code:
Code:
        mov ax,0x0211       ; copy 17 sectors
        mov bx,0x0800       ; to 0x0800:0x0000 (es and bx are swapped later)
        mov cx,0x0002       ; from sector 2, track 0
        mov dh,0x00         ; head 0
        mov dl,[DriveID]    ; boot drive

    push ax bx cx dx        ; save registers

        mov si,0xFFC0       ; sector counter (0x0040 sectors)
        mov di,0xFFFB       ; attemp counter (0x0005 attemps)

    ReadSectors:

        jif di,e,0,Error    ; 5 attempts?

        xor ah,ah           ;\
        mov dl,[DriveID]    ; reset boot disk
        int 0x13            ;/
        jc ReadSectors      ; loop until ready

        inc di              ; next attempt

    ReadNext:

    pop dx cx bx ax         ; restore registers
    push ax bx cx dx        ; resave registers

    push es                 ; save es
        mov es,bx           ;-swap es and bx
        xor bx,bx           ;/

        int 0x13            ; read rectors
    pop es                  ; restore es
        jc ReadSectors      ; read fail?

    pop dx cx bx ax         ; restore registers

        xor ch,ch           ;-sectors just copied
        mov cl,al           ;/

        shr bx,5            ;\
        add bx,cx           ; update segment value
        shl bx,5            ;/

        add si,cx           ;-update sector counter
        mov cx,si           ;/
        mov dx,0x0040       ;
        add cx,dx           ; find sector index
        sub dx,cx           ; find remaining sectors

        jif dl,b,0x12,@f    ; fewer then 18 sectors left?
        mov al,0x12         ; only copy 18 sectors
        jmp cont
    @@:
        mov dl,al           ; copy remaining sectors
    cont:

    push ax bx              ; save registers

        mov ax,cx           ; calculate...
        inc ax              ;
        mov bx,0x0012       ;
        div bx              ; ...starting sector
        mov cl,dl           ;
        inc cl              ;
        mov bx,0x0002       ;
        div bx              ; ...track and head
        mov ch,al           ;

    push cx                 ; save register

        mov dh,dl           ; ... head
        mov dl,[DriveID]    ; set drive

    push dx                 ; save register

        mov di,0xFFFB       ; allow 5 attempts for next operation

        jif si,ne,0,ReadNext ; all sectors copied?

    pop dx cx bx ax         ; clear stack

        xor al,al           ;\
        mov dx,0x03F2       ; turn off motor
        out dx,al           ;/
    


The code doesn't call error or (AFAIK) copy any sectors at all, it just hangs. Any ideas what I've done wrong? Embarassed

Many thanks in advance!

COD3B453
Post 02 Aug 2006, 10:55
View user's profile Send private message Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 490
Location: Belarus
zhak
wow! your code is tooo complicated, i think. i suggest you to read one sector at a time. here's an example, but i didn't even compile it (i do not have the ability to test my code when i'm in office) so testing is up to you. the idea is above all at last Smile
Code:
    mov si, 0100h
    mov bp, 64          ;num of sectors to read
    mov ax, 800h
    mov es, ax
    xor bx, bx
    xor dx, dx
    mov dl, [DriveID]
    mov cx, 2
  .read_sector:
    mov di, 5
  .try_again:
    mov ax, 0201h
    int 13h
    jc .error
    dec bp
    jz .end_read
    add bx, 512
    inc cl
    cmp cl, 19
    jnz .read_sector
    test dx, si
    jz @f
    add cx, si
  @@:
    xor dx, si
    mov cl, 1
    jmp .read_sector
  .error:
    mov ax, 0
    int 13h
    jc .read_failed
    dec di
    jnz .try_again

  .read_failed:
    ...             ;unable to read the disk
    
  .end_read:
    ...             ;all done successfully    


Last edited by zhak on 02 Aug 2006, 15:02; edited 1 time in total
Post 02 Aug 2006, 12:59
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Thanks zhak but that too didn't work (though it did give me a very amusing space invaders kinda screen dump Very Happy ) so I'm going for the lazy method on this one and doing 4 track reads out long
Post 02 Aug 2006, 14:33
View user's profile Send private message Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 490
Location: Belarus
zhak
hm, interesting.... i compiled a test floppy disk and executed it on bochs... all ok. all 64 sectors were read successfully....

i did a mistake in source code:
should be not

MOV ES, 800h

but

MOV AX, 800h
MOV ES, AX
Post 02 Aug 2006, 15:05
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.