REP MOVSB mysterie

Hey,

I am (like so many others) developing a little OS. Now, despite all odds, this is not a question about how to do such.
I found myself a little mysterie whilst pulling out my hair.
It took my about 10 re-compiles with each subtle relocation of the "cli, hlt" part to narrow down. Not to mention countless trials with magic numbers to detect whether my code memory area was being overwritten...

I narrowed it down to the following piece of code.
I'll have to explain it a bit thought: my bootloader is still loaded at 7c00 (untouched) and this is a kernel loader directly loaded by the bootloader in question to 500 (0050:0000). Now I am interested in retrieving a record of information (the bios parameter block - it's a FAT12 floppy bootloader).
CS, DS, ES and SS are all set up to segment 0050
StartOfBPB is the location of the BPB in the kernel loader, whereas the BPB begins at byte 3 of the bootloader.
[code]
push ds
mov di, StartOfBPB ;ES:DI = StartOfBPB
mov ax, 0x07c0 ;
mov ds, ax ;DS:SI = 07c0:0003
mov si, 0x0003 ;
mov cx, [SizeOfBPB] ;Copy SizeOfBPB bytes
rep movsb
pop ds
[/code]
This code itself does not give me any error, but a print function afterwards gives me very weird character outputs and 'most' code after it a "Reboot and Select proper Boot device or Insert Boot Media in selected Boot device" error.

Now out of pure frustration and confusion I used the following code:
[code]
mov di, StartOfBPB ;ES:DI = 0050:StartOfBPB
mov si, 0x7703 ;DS:SI = 0050:7703 = 07c0:0003
mov cx, [SizeOfBPB] ;Copy SizeOfBPB bytes
rep movsb
[/code]
And it works without any error! I checked on of the string-types of the BPB and it is updated to the value found in the bootloader.

I'm stumped, in a word.

Help, anyone?
Best Regards,
Richard

The way I see it... Well, it's all pretty blurry

Comments

  • Hello,

    Are you asking why the first version fails to work?

    I might be wrong, but IIRC, DS is used whenever you reference memory by default, such as a variable. So...[code]
    mov cx, [SizeOfBPB] ;Copy SizeOfBPB bytes
    [/code]
    This copies DS::offset address of SizeOfBPB into CX. Normally this is okay, however, because you modified DS prior to using the above, DS now = 0x7c0, so it references an incorrect location, and now contains who knows what.

    What happens if you move it above the loop?:[code]mov cx, [SizeOfBPB] ;Copy SizeOfBPB bytes
    push ds
    mov di, StartOfBPB ;ES:DI = StartOfBPB
    mov ax, 0x07c0 ;
    mov ds, ax ;DS:SI = 07c0:0003
    mov si, 0x0003 ;
    rep movsb
    pop ds[/code]Everything else looks okay to me...

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.mt2002.sitesled.com]Website :: OS Development Series[rightbr][/link][/size]
  • : Hello,
    :
    : Are you asking why the first version fails to work?
    :
    : I might be wrong, but IIRC, DS is used whenever you reference memory
    : by default, such as a variable. So...
    :

    Oh my *slaps himself*.
    I should have seen that.

    I really shouldn't allow myself to tamper with DS until the last moment.
    I've had code that constantly changed the DS before and everything kept going wrong and I ended up rewriting it so that DS was unchanged as much as possible.

    Thanks for the moment of clarity

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories