Hi,
It's your code (taken from your link) that I used for my last post, showing what would be in each register and why you're getting a "divide by zero" error. Posting the exact same (faulty) code won't change this...
GLneo wrote:
hmmm... i dont know much about div in asm :-\ but heres my code:
Code: Select all
mov dl, 18h
div dl
inc ah
mov cl, ah
Sector = (LBA mod SectorsPerTrack)+1
At this point:
cl = LBA mod SectorsPerTrack+1
ah = LBA mod SectorsPerTrack+1
al = LBA / SectorsPerTrack
dl = 18h still
At this point:
cl = LBA mod SectorsPerTrack+1
ax = 02h
dl = 18h still
dh = LBA / SectorsPerTrack
Also at this point, if
LBA is less than
SectorsPerTrack, then "
LBA / SectorsPerTrack" will be zero, and therefore
dh will be zero.
The next instruction is "
div dh", or (if
LBA is less than
SectorsPerTrack), "divide by zero".
For the code in your boot sector (from your link),
LBA is always equal to 02h as you do a "mov ax, 02h" immediately before this code. Therefore
LBA is always less than
SectorsPerTrack, and you'll always get a "divide by zero" error.
The problem with dividing by zero is that the answer is infinity. All pocket calculators and CPUs that I've ever seen have trouble with infinity. An 80x86 CPU will generate a "Divide Error Exception" (exception 0x00).
Often, what your code is meant to be doing and what it actually is doing is completely different. Using your mind (and the assistance of Intel's manuals if necessary), it's often possible to step though the code one instruction at a time (while keeping track of what each register or variable contains) until you find the bug.
I'd hope that if you realize that you "dont know much about div in asm", and if you're having trouble with bugs in code that mainly relies on "div in asm", then as a programmer you'd have the skills needed to find details of this instruction (in either Intel's reference material or elsewhere) and the sense to learn about "div in asm".
Cheers,
Brendan