Page 1 of 2

LBACHS

Posted: Fri Feb 08, 2008 6:34 pm
by bloodhound23
Is this lbachs function correct:

Code: Select all

LBACHS:;ax = lba
div BYTE[sectorspertrack]
push ax
mov [sector],ah
div  BYTE[numheads]
mov [cylinder],al
mov [head],ah
ret   
???

Posted: Fri Feb 08, 2008 6:40 pm
by pcmattman
Try it and find out :evil: .

Posted: Fri Feb 08, 2008 6:46 pm
by Zenith
But before you go and say 'it doesn't work!!!', you forgot to increment AX (LBA starts at 0, CHS starts at 0,0,1). Just add 'inc ax' to the beginning.

Posted: Fri Feb 08, 2008 6:46 pm
by bloodhound23
The reason I asked was that I was slightly confused with the formulas.

Posted: Fri Feb 08, 2008 6:48 pm
by bloodhound23
karekare0 wrote:But before you go and say 'it doesn't work!!!', you forgot to increment AX (LBA starts at 0, CHS starts at 0,0,1). Just add 'inc ax' to the beginning.
thanks!

Posted: Fri Feb 08, 2008 6:48 pm
by pcmattman
Also, I highly suggest doing such things as this in C and not in ASM as it's much easier to implement an algorithm such as the LBA->CHS conversion in C. Also, the compiler will automatically optimize your code (if -O0 is not set on the command line).

Posted: Fri Feb 08, 2008 6:50 pm
by bloodhound23
Now my bootloader totals 73 bytes, I think that leaves me plenty of room to load stage2 now that I have my read and LBACHS done.

Posted: Sat Feb 09, 2008 3:41 pm
by bloodhound23
pcmattman wrote:Also, I highly suggest doing such things as this in C and not in ASM as it's much easier to implement an algorithm such as the LBA->CHS conversion in C. Also, the compiler will automatically optimize your code (if -O0 is not set on the command line).
It's a bootloader and when you only have 512 bytes I take it it's hard to write it in C. This isn't stage2 yet.
This is it now:

Code: Select all

LBACHS:;ax = lba
inc ax
div BYTE[sectorspertrack]
push ax
mov [sector],ah
div  BYTE[numheads]
mov [cylinder],al
mov [head],ah
ret
EDIT:

Posted: Sat Feb 09, 2008 5:08 pm
by octavio
bloodhound23 wrote:
pcmattman wrote:Also, I highly suggest doing such things as this in C and not in ASM as it's much easier to implement an algorithm such as the LBA->CHS conversion in C. Also, the compiler will automatically optimize your code (if -O0 is not set on the command line).
It's a bootloader and when you only have 512 bytes I take it it's hard to write it in C. This isn't stage2 yet.
This is it now:

Code: Select all

LBACHS:;ax = lba
inc ax
div BYTE[sectorspertrack]
push ax
mov [sector],ah
div  BYTE[numheads]
mov [cylinder],al
mov [head],ah
ret
EDIT:
The first error is the useless push ax instruction
the second is that this code is very limited,enought for a floppy but not for
a hard disk.the formula is ok.
this is the code i use:

Code: Select all

;eax=lba address
edx=0 ebx=w[esi+info\sectors_x_heads] div ebx ;lba->chs
  xchg edx,eax div b[esi+info\n_sectors] inc ah
;ah=sector number
;al=head
;edx=cylinder
And C is much easier than Assembly ... unless you are a assembly programmer.

Posted: Sat Feb 09, 2008 5:15 pm
by bloodhound23
why for a hardisk? Maybe because I don't use the max cylinder info?

EDIT: push ax is left over from the old code I had that I revised
EDIT2: It's not hard to code it in C, just hard to keep it at 512 bytes.

Posted: Sat Feb 09, 2008 5:29 pm
by octavio
bloodhound23 wrote:why for a hardisk? Maybe because I don't use the max cylinder info?

EDIT: push ax is left over from the old code I had that I revised
Because the numbers are very small. After the first division
al=heads*cylinders but you need at least 16 bits just for the cylinder.
ej: a 4GB disk has 8912 cylindrs 15 heads and 63 sectors.
Also with modern hard disk you will have to use lba instead of chs.

Posted: Sat Feb 09, 2008 5:39 pm
by bloodhound23
How do I use lba? Bios uses CHS.

Posted: Sat Feb 09, 2008 5:57 pm
by bloodhound23
I truly don't get it after going through the INT list.

Posted: Sat Feb 09, 2008 6:08 pm
by bloodhound23
Anyway would there be a problem if I used this to boot from USB?

Posted: Sat Feb 09, 2008 6:21 pm
by bloodhound23
I just looked at the grub stage 1 source. Helps like you wouldn't belive