flat assembler
Message board for the users of flat assembler.

Index > 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: 1228
Location: Belarus
DimonSoft 14 Sep 2015, 10:49
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     ax, ax
        mov     ds, ax
        mov     es, ax
        pushf
        cli
        mov     ss, ax
        mov     sp, $7FFE
        popf
        mov     bx, dx

        int     13h
        jc      .DiskError

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

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

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

        mov     bx, [bp + 10]
        mov     si, [bp + 8]
        test    si, si
        jz      .EndProc
.ReadLoop:
        mov     ax, [bp + 6]
        mov     cx, 18
        xor     dx, dx
        div     cx
        inc     dx                              ; nSector
        test    ax, 1
        jz      @F
        inc     dh                              ; nHead
@@:
        shr     ax, 1                           ; nCylinder (nTrack)
        shl     ax, 6
        or      al, dl
        mov     cx, ax
        mov     ax, $0201
        mov     dl, [bp + 4]
        int     13h
        add     bx, 512
        dec     si
        jnz     .ReadLoop

.EndProc:
        pop     si bx
        pop     bp
        ret     8

; WriteString(strText)
WriteString:
        push    bp
        mov     bp, sp
        push    si

        mov     si, [bp + 4]
        movzx   cx, byte [si]
        test    cx, cx
        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     bp, sp
        mov     ax, [bp + 4]
        mov     cx, 4
.PrepareLoop:
        mov     dx, ax
        and     dx, $0F
        shr     ax, 4
        cmp     dl, 9
        jbe     @F
        add     dl, 'A' - '0' - 10
@@:
        add     dl, '0'
        push    dx
        loop    .PrepareLoop

        push    word '$'
        mov     cx, 5
.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:
[FLOPPY] attempt 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:
rax: 00000000_00000201 rcx: 00000000_00090041
rdx: 00000000_00000000 rbx: 00000000_00001000
rsp: 00000000_00007ff0 rbp: 00000000_00007ff4
rsi: 00000000_000e0001 rdi: 00000000_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 Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20489
Location: In your JS exploiting you and your system
revolution 14 Sep 2015, 11:18
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: 1228
Location: Belarus
DimonSoft 14 Sep 2015, 11:43
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 Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20489
Location: In your JS exploiting you and your system
revolution 14 Sep 2015, 13:18
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: 1228
Location: Belarus
DimonSoft 14 Sep 2015, 17:50
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 Visit poster's website Reply with quote
mbr_tsr



Joined: 03 Apr 2011
Posts: 12092
mbr_tsr 20 Jul 2016, 05:35
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: 12092
mbr_tsr 20 Jul 2016, 05:43
Post 20 Jul 2016, 05:43
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 20 Jul 2016, 08:24
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 Visit poster's website Reply with quote
Trinitek



Joined: 06 Nov 2011
Posts: 257
Trinitek 20 Jul 2016, 20:35
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


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.