LBACHS

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

LBACHS

Post 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   
???
I thought I wasn't thinking, I thought wrong.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Try it and find out :evil: .
User avatar
Zenith
Member
Member
Posts: 224
Joined: Tue Apr 10, 2007 4:42 pm

Post 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.
"Sufficiently advanced stupidity is indistinguishable from malice."
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

Post by bloodhound23 »

The reason I asked was that I was slightly confused with the formulas.
I thought I wasn't thinking, I thought wrong.
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

Post 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!
I thought I wasn't thinking, I thought wrong.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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).
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

Post 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.
I thought I wasn't thinking, I thought wrong.
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

Post 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:
Attachments
stage1.asm
This is the whole bootloader so far.
(1.74 KiB) Downloaded 51 times
I thought I wasn't thinking, I thought wrong.
octavio
Member
Member
Posts: 94
Joined: Wed Oct 25, 2006 5:12 am
Location: Barcelona España
Contact:

Post 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.
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

Post 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.
I thought I wasn't thinking, I thought wrong.
octavio
Member
Member
Posts: 94
Joined: Wed Oct 25, 2006 5:12 am
Location: Barcelona España
Contact:

Post 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.
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

Post by bloodhound23 »

How do I use lba? Bios uses CHS.
I thought I wasn't thinking, I thought wrong.
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

Post by bloodhound23 »

I truly don't get it after going through the INT list.
I thought I wasn't thinking, I thought wrong.
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

Post by bloodhound23 »

Anyway would there be a problem if I used this to boot from USB?
I thought I wasn't thinking, I thought wrong.
User avatar
bloodhound23
Member
Member
Posts: 115
Joined: Wed Jan 23, 2008 7:13 pm
Contact:

Post by bloodhound23 »

I just looked at the grub stage 1 source. Helps like you wouldn't belive
I thought I wasn't thinking, I thought wrong.
Post Reply