flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Lost in offsets (int 13h)

Author
Thread Post new topic Reply to topic
DustWolf



Joined: 26 Jan 2006
Posts: 373
Location: Ljubljana, Slovenia
DustWolf
Hello,

I have written a custom MBR and am using it as a 'floppy' in VMWare.

All the code is longer than 512 bytes, so what I have done is locate code inside the first 512 bytes at 7C00h that uses INT 13h to load up sector 2 (which is the next 512 bytes basically) into 7E00h and then jumps to it.

All my code is in one FASM file and I am using:
Code:
times   (7E00h-$)       db ?    

...to pad the first half of the code so that when INT 13h loads up the data, it is exactly where it should be if all of the data was loaded up in one go.

Unfortunately something appears to be wrong here. When I boot up my program, the first half exectues without a problem, INT 13h succeeds, the second half of the program also does exactly what it should, but my data pointers for data in the lower half are all wrong.

I assume what has happened is that the padding added more data and there is a misalignment. Am I padding my code wrong?
Post 23 Jun 2008, 22:21
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
yes, you pad your code wrong.
see on some examples like mines to know how to do.
Post 24 Jun 2008, 05:22
View user's profile Send private message Visit poster's website Reply with quote
DustWolf



Joined: 26 Jan 2006
Posts: 373
Location: Ljubljana, Slovenia
DustWolf
Care to provide a handy link? I'm not sure what to look for when you put it like that.
Post 24 Jun 2008, 17:33
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger Reply with quote
TNick



Joined: 29 Jan 2008
Posts: 13
Location: Brasov, Romania
TNick
Well... I really do not know what link should I post... maybe this one, but I guess you already know about this. Smile

The problem is that:
- at the start of your file, you should place a
Code:
org 0x7c00    

wich will inform the assembler that, even if, in file, you are at offset 0, it should treat the code as being placed at that address.
At this point $ is equal with 0x7c00. As you write code, $ will increase. At the end of what you need to be placed in first sector, you may add something like this:
Code:
if ($ - 0x7c00) > 510 ; if we are above allowed size
 display 'Too much data in Sector 1!!!',13,10; this will alert you
else if ($ - 0x7c00) < 510 ; if we are below that size
  db (510 - ($ - 0x7c00)) dup 1 ; fill the space with 1 ...  or 0 Smile
end if
dd 0x55,0xAA ; and place a signature
Sector2:
    

After this, you may place data and code that you want in sector 2 ... and 3

... and, if you want to know how manny sectors you need to read, place this at the very end:
Code:
if (512 - (($ - Sector2) and 0x1FF)) > 0
   db (512 - (($ - Sector2) and 0x1FF)) ; fill up to make a full sector
end if
SctToRead = ($ - Sector2) / 0x200 ; or 512 = the size of one sector
    


HTH

Nick
Post 24 Jun 2008, 17:51
View user's profile Send private message Reply with quote
DustWolf



Joined: 26 Jan 2006
Posts: 373
Location: Ljubljana, Slovenia
DustWolf
Using this code I get the exact same problem as with my code.

Must be something stupid... here is my code:
Code:
     mov     cx,2            ;sector=2 cylinder=0
        mov     dh,0            ;head=0
     mov     dl,0            ;drive=floppy A (80h=hdd)
   mov     es,cx           ;segment 0    


All the code runs fine but in the lower half, si seems to point at offset 0x200 instead of 0x219 (I used a hex editor on binary and compared to the display on screen). If I put my lower half data (bootMsg) with the other data higher up, it works fine.

EDIT: Nevermind! I can't believe I spent two days trying to figure that out! XP

So optimizing code before you finnish it is bad...

ps.: for the record this code works fine:
Code:
        times   (7DFEh-$)       db ?
                db      55h,0AAh    
Post 24 Jun 2008, 23:26
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
there are problems:

you don't write the right values.

first:
Code:
mov cx,2
mov dx,0
mov bx,0
mov ax,segment
mov es,ax
mov al,sectorcount
mov ah,diskoperation
int 13h
    


and second:
Code:
free =  510-(padding-$$)
padding rb free
        dw 0aa55h
    


this code uses a label to calculate the amount of byte to pad.

this is very usefull in case of modularised boot sector.

you can participate in one of my multiple threads on the subject if you want.
my current researchs on OO disk operations
bootloader created with the help of many coders
this one is exactlly like the current thread
this one is about how to boot, and switch to Protected mode
and this one is about an hardcore project
Post 26 Jun 2008, 19:40
View user's profile Send private message Visit poster's website 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. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.