Page 1 of 1
Transferring control beyond 512 bytes
Posted: Thu Jan 15, 2009 3:04 pm
by Rituraj
i m trying to design an os.The thing is that my bootloader program is successfully loaded.
however i m unable to figure out how to transfer data beyond the first 512 bytes(of my usb flash drive) into the RAM as this is where the code for the actual os is intended to be stored.
i have one more problem, what is the memory addressing mode for a usb flash drive...as in CHS or LBA?
Re: Transferring control beyond 512 bytes
Posted: Thu Jan 15, 2009 3:17 pm
by gzaloprgm
If your usb drive gets detected as hdd, some bios support
Code: Select all
dap: db 0x10
res: db 0x00
howmany:dw 0x0000
off: dw 0x0000
segm: dw 0x0000
lba: dq 0x0000000000000000
-----
ReadSectors:
mov WORD [off], bx
mov WORD [segm], es
mov WORD [howmany], cx
mov WORD [lba], ax
mov AH, 42h
mov BYTE DL, [drive]
mov SI, dap
int 13h
jc errorf
ret
Off : Seg is where you wanna the sectors to be loaded.
Howmany says how many sectors you want to copy.
Lba holds the lba address of the disk (in this case the usb drive)
Otherwise, if it gets detected as floppy, you can do it like loading files from floppy (remember to save DL- drive number on boot).
Cheers,
Gonzalo
Re: Transferring control beyond 512 bytes
Posted: Thu Jan 15, 2009 3:34 pm
by Rituraj
thanks but how do i find out the lba address of the disk?
Re: Transferring control beyond 512 bytes
Posted: Thu Jan 15, 2009 3:36 pm
by JohnnyTheDon
LBA 0 is the first sector, so LBA 1 is the second sector.
Re: Transferring control beyond 512 bytes
Posted: Thu Jan 15, 2009 3:42 pm
by nos4A2
But what about using addressing similar to floppy disk drives...like cylinder,head and sector...does that work for pendrives
Re: Transferring control beyond 512 bytes
Posted: Thu Jan 15, 2009 3:47 pm
by JohnnyTheDon
Yeah, just use the normal int 13h ah=02h interface.
Re: Transferring control beyond 512 bytes
Posted: Thu Jan 15, 2009 3:53 pm
by nos4A2
[BITS 16]
[ORG 0x7C00]
MOV AL, 65
CALL PrintCharacter
CALL readsector
JMP 0x1000
readsector:
Code: Select all
MOV AH,0x02
mov al,0x01
mov ch,0x00
mov cl,0x04
mov dh,0x00
mov dl,0x80
MOV bx,0x1000
MOV es,bx
MOV bx,0x0000
INT 0x13
JNC 0x1000
RET
see this is the code i am using for the transfer....but it just prints A and pauses(CALL printcharacter prints the first A)...i have a code to print a character in the next sector, which does not execute at 0x1000
Re: Transferring control beyond 512 bytes
Posted: Thu Jan 15, 2009 4:01 pm
by JohnnyTheDon
Don't set DL. When your code starts, DL contains your boot drive. By setting it to 0x80, it is loading the sector from the first hard drive. Make sure that printcharacter doesn't edit DL and remove mov dl, 0x80.
Re: Transferring control beyond 512 bytes
Posted: Thu Jan 15, 2009 4:05 pm
by nos4A2
thanks ill try it out and post the results soon.....
Re: Transferring control beyond 512 bytes
Posted: Thu Jan 15, 2009 4:35 pm
by Firestryke31
Personally, the very first thing I do (after setting segments and deciding on an initial stack address) is set both SP and BP to my stack address. The very second thing I do is push dx. That way, as long as BP doesn't change, and you don't overwrite that value in the stack, you can just use mov dl, [bp-2] (or the AT&T equivalent, if you use that) to retrieve the boot drive. That frees up dx a bit, so you only have to preserve BP and the stack. If you do it right, you can even save it right up until you reach protected mode (where it won't help you as much)!
But that's just the way I designed my bootloader. Your design may vary, and this might not work so well for you.
Re: Transferring control beyond 512 bytes
Posted: Fri Jan 16, 2009 4:30 am
by Combuster
Also, you're jumping to a different location than where you loaded your stuff... (and yuck @ JNC number)
Re: Transferring control beyond 512 bytes
Posted: Sat Jan 17, 2009 2:16 am
by nos4A2
which location should it jump to...i am givin ES:BX as 1000:0000 so i am jumping to 0x1000...some help??? also i thought i would meddle with the stack once i am able to tranfer control...is it absolutely compulsory to set the stack....And i think the INT 13 parameters are wrong...the code i want to jump to is in sector 1 of the flash drive....what is the cylinder, head and sector number for this sector?????
Re: Transferring control beyond 512 bytes
Posted: Sat Jan 17, 2009 3:51 am
by nos4A2
IT WORKED....i was givin the sector number wrong....mov ch,0x02....thanks a lot
Re: Transferring control beyond 512 bytes
Posted: Sat Jan 17, 2009 3:51 am
by Hyperdrive
nos4A2 wrote:i am givin ES:BX as 1000:0000 so i am jumping to 0x1000...
Wrong, 0x1000:0x0000 is 0x10000.
Re: Transferring control beyond 512 bytes
Posted: Sat Jan 17, 2009 11:13 am
by Firestryke31
nos4A2 wrote:...is it absolutely compulsory to set the stack...
It's an extremely good idea to do so, even if you don't do it the way I said to, because you have no idea how the BIOS set it up. If you set it up yourself you know exactly how much space you have and don't have to worry about messing up something set up by the BIOS. Plus it's 3 instructions (4 if you want to use BP like I said):
Code: Select all
mov ax, StackSegmentValue ; I use 0, but it can be _almost_ anything
mov ss, ax
mov sp, StackValue ; I use 0x8000, some use 0x7C00
mov bp, sp ; optional