# OSDev.org

The Place to Start for Operating System Developers
 It is currently Mon Sep 21, 2020 12:35 am

 All times are UTC - 6 hours

 Page 1 of 1 [ 6 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Additional BrokenThorn Issue?Posted: Fri Sep 11, 2020 8:18 pm

Joined: Fri Sep 11, 2020 8:06 pm
Posts: 2
I don't see it listed here, but I thought I'd have someone check that the issue is with the page and not me: https://wiki.osdev.org/Brokenthorn%27s_Known_Bugs

In OSDev6, BrokenThorn says the LBA to CHS conversion formulas are:

Quote:
absolute sector = (LBA % sectors per track) + 1
absolute head = (LBA / sectors per track) % number of heads
absolute track = LBA / (sectors per track * number of heads)

I believe the "absolute track" formula is incorrect and the correct set of equations should be:

Quote:
absolute sector = (LBA % sectors per track) + 1
absolute head = (LBA / sectors per track) % number of heads
absolute track = (LBA / sectors per track) / number of heads

Adding this would reduce confusion with people wondering why the code never multiplies by "number of heads".

I don't have permissions to edit the Wiki yet, but if this looks correct, I can add the reference.

Top

 Post subject: Re: Additional BrokenThorn Issue?Posted: Sat Sep 12, 2020 1:46 am
 Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1026
There is no difference?

Try this code:
Code:
#include <stdio.h>

int main(void)
{
int a, b, c;
puts("Begin...");
for (a = 0; a < 128; a++)
for (b = 1; b < 128; b++)
for (c = 1; c < 128; c++)
{
int r0 = (a / b) / c;
int r1 = a / (b * c);
if (r0 != r1)
printf("(%d / %d) / %d != %d / (%d * %d)\n", a, b, c, a, b, c);
//printf(".");
}
puts("End.");
return 0;
}

Output:
Quote:
Begin...
End.

Top

 Post subject: Re: Additional BrokenThorn Issue?Posted: Sat Sep 12, 2020 1:49 am
 Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 619
The two are identical. Unless I missed something, dividing by a product is the same as dividing by each of the factors. Mathematically:
Code:
a / b / c = (a / b) / c = (a / b) * (1 / c) = (a * 1) / (b * c)

That doesn't hold with integer division in C, yes, because of the round-off error introduced in step 2, but the ends still apply. I am sure, someone better versed in number theory than I could prove this proposition. But for now I have simply written a program to prove it for me within the range given and it failed to find any counter examples. And that is good enough for me.
pretzelpirate wrote:
I don't have permissions to edit the Wiki yet, but if this looks correct, I can add the reference.
Enter your user control panel, click "user groups", click "Wiki", and click "Join". Done.

_________________
Life is beautiful. Nobody said easy.

Top

 Post subject: Re: Additional BrokenThorn Issue?Posted: Sat Sep 12, 2020 6:49 am

Joined: Fri Sep 11, 2020 8:06 pm
Posts: 2
It looks like you're all correct, and I should avoid posting late at night.

I don't quite understand why BrokenThorn used a less obvious formula that didn't match the specific steps that were being done in code, but perhaps this post will help other people who wonder why the code and comments don't match.

Thanks everyone!

Top

 Post subject: Re: Additional BrokenThorn Issue?Posted: Sat Sep 12, 2020 11:17 am
 Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 619
pretzelpirate wrote:
I don't quite understand why BrokenThorn used a less obvious formula
Probably a micro-optimization. This way avoids one division, and some programmers, particularly low-level OS programmers, have a pathological aversion to division, believing it to be slow. In this case, though, when you are trying to access a floppy drive, a division is going to be the least of your worries, timewise.

_________________
Life is beautiful. Nobody said easy.

Top

 Post subject: Re: Additional BrokenThorn Issue?Posted: Sat Sep 12, 2020 11:58 am
 Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 2287
It could be left over from analyzing a hard disk bootloader that uses only 16-bit instructions. It's possible for (LBA / sectors per track) to overflow a 16-bit DIV instruction in cases where (LBA / (sectors per track * number of heads)) would not.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 6 posts ]

 All times are UTC - 6 hours

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Operating System Development    OS Development    OS Design & Theory    Announcements, Test Requests, & Job openings Everything Else    General Programming    General Ramblings    Auto-Delete Forum OSDev.org    OSDev Wiki    About this site