flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > OS Construction > int 13h FN 02h: Bochs bug or my misunderstanding?

Author
Thread Post new topic Reply to topic
DimonSoft



Joined: 03 Mar 2010
Posts: 68
Location: Belarus
int 13h FN 02h: Bochs bug or my misunderstanding?
While debugging a floppy boot sector I found a behaviour of Bochs that seems wrong to me. I traced it down to the following piece of code:


Code:

        format binary as 'bin'
        use16
        org $7C00 + 62

; BIN-file gets placed at offset 62 in the IMG-file
; The FAT12 BootSector header contains a proper jump to the address

EntryPoint:
        jmp     $0000:RealEntryPoint

RealEntryPoint:
        xor     axax
        mov     dsax
        mov     esax
        pushf
        cli
        mov     ssax
        mov     sp$7FFE
        popf
        mov     bxdx

        int     13h
        jc      .DiskError

        push    $1000
        push    1
        push    36
        push    bx
        call    ReadSectors
        jc      .DiskError
        cli
        hlt

.DiskError:
        movzx   axah
        push    ax
        push    strDiskError
        call    WriteString
        call    WriteHex
        cli
        hlt

; ReadSectors(nDrive, nLBA, nCount, pDest)
ReadSectors:
        push    bp
        mov     bpsp
        push    bx si

        mov     bx, [bp + 10]
        mov     si, [bp + 8]
        test    sisi
        jz      .EndProc
.ReadLoop:
        mov     ax, [bp + 6]
        mov     cx18
        xor     dxdx
        div     cx
        inc     dx                              ; nSector
        test    ax1
        jz      @F
        inc     dh                              ; nHead
@@:
        shr     ax1                           ; nCylinder (nTrack)
        shl     ax6
        or      aldl
        mov     cxax
        mov     ax$0201
        mov     dl, [bp + 4]
        int     13h
        add     bx512
        dec     si
        jnz     .ReadLoop

.EndProc:
        pop     si bx
        pop     bp
        ret     8

; WriteString(strText)
WriteString:
        push    bp
        mov     bpsp
        push    si

        mov     si, [bp + 4]
        movzx   cxbyte [si]
        test    cxcx
        jz      .EndProc
        inc     si
.WriteLoop:
        lodsb
        mov     ah$0E
        int     10h
        loop    .WriteLoop
.EndProc:
        pop     si
        pop     bp
        ret     2

; WriteHex(wValue)
WriteHex:
        push    bp
        mov     bpsp
        mov     ax, [bp + 4]
        mov     cx4
.PrepareLoop:
        mov     dxax
        and     dx$0F
        shr     ax4
        cmp     dl9
        jbe     @F
        add     dl'A' - '0' - 10
@@:
        add     dl'0'
        push    dx
        loop    .PrepareLoop

        push    word '$'
        mov     cx5
.PrintLoop:
        pop     ax
        mov     ah$0E
        int     10h
        loop    .PrintLoop
        pop     bp
        ret     2

struc String [data]
{
  common
    local ..start..length
    . db ..length
    ..start db data
    ..length = $ - ..start
}

strDiskError            String  "DISK ERROR: "




I tried to run the code with VMWare and Bochs. It doesn’t show "DISK ERROR" message in both cases, but for some reason Bochs says:


Code:
[FLOPPYattempt to read/write sector 65 past last sector 18



when int 13h instruction gets executed. The register values before int 13h are (copy-pasted from Bochs):


Code:

rax00000000_00000201 rcx00000000_00090041
rdx00000000_00000000 rbx00000000_00001000
rsp00000000_00007ff0 rbp00000000_00007ff4
rsi00000000_000e0001 rdi00000000_0000ffac




The FN 02h docs say that the register values would get interpreted as C = 1, H = 0, S = 1, which is what I expect.

Am I right that it’s a bug in Bochs? Or is there something I don’t know/understand here?
Post 14 Sep 2015, 10:49
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14673
Location: Origae-6
On another matter: I don't see where you are incrementing the sector number stored at [bp+6].
Post 14 Sep 2015, 11:18
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 68
Location: Belarus

revolution wrote:
On another matter: I don't see where you are incrementing the sector number stored at [bp+6].


Oops, my fault, thanks. It was first called ReadSector, failed to wrap it in a loop properly Smile

---

But since it is indeed on another matter, the answer to the main question is still wanted. I hope someone has some info to share on the topic.
Post 14 Sep 2015, 11:43
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 14673
Location: Origae-6
Looking at that further I see that you are actually trying to read cylinder 256. The higher order cylinder bits are in CL.

Also: You never detect when a read fails, you just keep reading sectors.
Post 14 Sep 2015, 13:18
View user's profile Send private message Visit poster's website Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 68
Location: Belarus

revolution wrote:
Looking at that further I see that you are actually trying to read cylinder 256. The higher order cylinder bits are in CL.

Also: You never detect when a read fails, you just keep reading sectors.


Thanks, that should be it! For some reason at some point I started thinking it takes lower 2 bits in bits 6–7 of CL. Looked again into the docs with more attention and…

There’s even more to do here, I guess. In some articles they say, it is also worth trying at least 3–5 times for each sector with controller resets in between until either the operaration succeeds or all the attempts fail. This was just the piece of code I traced the problem down to.
Post 14 Sep 2015, 17:50
View user's profile Send private message Reply with quote
mbr_tsr



Joined: 03 Apr 2011
Posts: 4597
boot sectors are VERY easy...
needs sequential access to sectors, i forget a floppy has 0x7c00 512 sectors, it is easy to use FAT 12, or divide the disk into files, just need to use the ASM i made, mine is the best, it will work, AMMM, and have a C:\boot sector load a 300k EXE as the kernel, all the C: hard disk int 13h 0 reset the drive 80h KRACK Kanez. REALLY, they a SUPER tine 512 byte command entry,,,,then a lot of Judas Priest...then after a few kernels, you reaize the horror of missing gosubs....the horror of my godsbogs int 0xFE...Wang fang Chan Ching Konk Gonk....Can't Bet the Real Thing, Cokea' kole Klassic{U}N{We} don't forget to hand code your int 13h drive retries, 5 works for me Smile!
Post 20 Jul 2016, 05:35
View user's profile Send private message Reply with quote
mbr_tsr



Joined: 03 Apr 2011
Posts: 4597
Post 20 Jul 2016, 05:43
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 68
Location: Belarus
Re: boot sectors are VERY easy...

mbr_tsr wrote:
needs sequential access to sectors, i forget a floppy has 0x7c00 512 sectors, it is easy to use FAT 12, or divide the disk into files, just need to use the ASM i made, mine is the best, it will work, AMMM, and have a C:\boot sector load a 300k EXE as the kernel, all the C: hard disk int 13h 0 reset the drive 80h KRACK Kanez. REALLY, they a SUPER tine 512 byte command entry,,,,then a lot of Judas Priest...then after a few kernels, you reaize the horror of missing gosubs....the horror of my godsbogs int 0xFE...Wang fang Chan Ching Konk Gonk....Can't Bet the Real Thing, Cokea' kole Klassic{U}N{We} don't forget to hand code your int 13h drive retries, 5 works for me Smile!


Could anyone explain me what does this stream of pure mind thrown out into a topic from about a year ago actually mean? And why would anyone miss gosubs in assembly, like if they don’t exist in it?
Post 20 Jul 2016, 08:24
View user's profile Send private message Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 254
Re: boot sectors are VERY easy...

DimonSoft wrote:
Could anyone explain me what does this stream of pure mind thrown out into a topic from about a year ago actually mean? And why would anyone miss gosubs in assembly, like if they don’t exist in it?

Mental illness. Don't mind him.
Post 20 Jul 2016, 20:35
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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2016, Tomasz Grysztar.