flat assembler
Message board for the users of flat assembler.

Index > OS Construction > int 13, 2 es:bx incrementation

Author
Thread Post new topic Reply to topic
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
When I read a sector into memory with int 13, 2 does it increment es:bx so the next read will not overwrite the previous sector, if not, how much should I add to it.
Code:
format binary as 'img'
org 7c00h
    cli
    mov ax, cs
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    mov sp, 0ffffh
    sti
    mov [drive_number], dl
    add dl, 'A'
    mov [drive_letter], dl
    mov si, loading
    call print_str
next_read:
    mov ax, next_sector
    mov es, ax
    xor bx, bx
    mov ah, 02h
    mov al, 01h
    mov ch, 00h
    mov cl, [current_sector]
    mov dh, 00h
    mov dl, [drive_number]
    int 13h
    cmp ah, 00h
    jne error
    inc [current_sector]
    inc [successful_attempts]
    cmp [successful_attempts], 01h; Change this for more reads.
    jb next_read
    mov si, loaded
    call print_str
    jmp next_sector:0000h         ; Where ever the pointer is supposed to point
error:                            ; If it ends up here, there's a problem.
    mov ax, 0040h
    mov ds, ax
    mov bx, 0000h
    mov [0072h], bx
    jmp 0ffffh:0000h


print_str:                       
    pushad
    mov ah, 0eh
@@:
    lodsb
    cmp al, 00h
    je @f
    int 10h
    jmp @b
    @@:
    popad
    retn



drive_letter rb 1
drive_number rb 1

loaded db 'Sectors loaded successfully.', 0dh, 0ah, 00h
loading db 'Loading...', 0dh, 0ah, 00h

current_sector db 2
reads_to_attempt db 1
successful_attempts db 0


rb 510 + $$ - $
db 055h, 0aah

next_sector:    

Also, if you don't mind, what is this for? I got it from Bitshifter's boot loader.
Code:
cli
    mov ax, cs
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    mov sp, 0ffffh
    sti    


Last edited by Tyler on 26 Dec 2009, 01:13; edited 1 time in total
Post 26 Dec 2009, 00:48
View user's profile Send private message Reply with quote
Coddy41



Joined: 18 Jan 2009
Posts: 384
Location: Ohio, USA
Coddy41
Tyler wrote:
When I read a sector into memory with int 13, 2 does it increment es:bx so the next read will not overwrite the previous sector, if not, how much should I add to it.

I do not think it does it automaticly unless you have a magic processer. And I
cannot tell you how much to do it, that all depends on the size of the kernel ect.

eg:
you could load your kenel at 1000:0000 and apps at 6000:0000 ect.

I may be misunderstanding the question though.

_________________
Want hosting for free for your asm project? You can PM me. (*.fasm4u.net)
Post 26 Dec 2009, 01:04
View user's profile Send private message Visit poster's website Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
From what I know(just from others' examples) you're supposed to read one sector at a time with a boot loader. How much do I add to es(indirectly) from one read to the next, so as to not overwrite the last sector?
Post 26 Dec 2009, 02:20
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
Quote:

jmp next_sector:0000h ; Where ever the pointer is supposed to point

mmm, what a pity fasm doesn't say nothing about that...

Anyway, you'll have to increment BX by 512 on every read and after 128 reads you'll need to increment ES by 1000h too.

I think it will be easier if you copy the sectors starting at 1000:0000 (hex) address. Something like this:

Code:
;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

mov ax, $1000
mov es, ax
xor bx, bx
mov cx, 1
mov dh, 0
mov si, NUMBER_OF_SECTORS ; 0 < NUMBER_OF_SECTORS < 64

next_read:
mov di, 3 ; number of tries

.read:
mov ax, $0201
int $13
jc .fail

; Prepare for next sector
inc cl
add bx, 512
dec si
jnz next_read

jmp $1000:0000 ; Jump to second stage (loaded sectors)

.fail:
; Check if we have another try
dec di
jnz .read

; Handle the fatal error here
    


[edit]If you plan to place the second stage code in the same file the boot loader is then don't forget to use "org 0" before the code/data begins.
Post 26 Dec 2009, 04:09
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler
LocoDelAssembly wrote:

Tyler wrote:

jmp next_sector:0000h ; Where ever the pointer is supposed to point


Ooops, I put that there while I was guessing at what it should be. Forgot to delete it. Embarassed
Anyway, thanks, that's what I wanted to know. Nice code too, you did it a lot more efficiently than I did.
Post 26 Dec 2009, 05:34
View user's profile Send private message Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 692
Location: Adelaide
sinsi
A way to load more than 64k is to leave BX=0 and increment ES
Code:
mov ax,es
add ax,512/16  ;(bytes per sector) / (bytes per paragraph)
mov es,ax    

You will also need to increment DH (head) if CL (sector) goes past 'sectors per track' (and then CH (track) once DH goes past 'number of heads').
Post 26 Dec 2009, 05:43
View user's profile Send private message Reply with quote
Mac2004



Joined: 15 Dec 2003
Posts: 313
Mac2004
Tyler: One possible approach to solve this 64kb barrier problem is to use a temporary buffer
and copy the read sector from the temporary bufffer to the desired location right after
you've read each sector.

regards
Mac2004
Post 27 Dec 2009, 09:22
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
Mac2004,

Buffering is good to support arbitrary-length reads, but Tyler wants to read sectors as a whole.

___
Tyler,

Modern BIOSes handle DMA-across-64KiB boundary (unrelated to mentioned "64kb barrier", it's from design of DMA in PC), yet you may opt to be on the safe side (since there isn't check for error 9 in any shown code).

CF is more reliable indicator of int 13h failure than ah being zero. I recall at least one such BIOS (that returns ah!=0 even if there is no error, CF was cleared).

mov sp, 0ffffh looks strange. Maybe it's there to catch stack overflow (386+ will shutdown because of triple fault on push when sp==1). In other case, it looks misaligned at least.

There is a trick to execute some code 2**n times without counter, using stack:
Code:
add4:     call    add2
add2:       call    add1
add1:       add     ax, 1
       ret    
You may use it for retries.
Post 27 Dec 2009, 12:03
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.