Transferring control beyond 512 bytes

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Rituraj
Posts: 2
Joined: Thu Jan 15, 2009 2:58 pm

Transferring control beyond 512 bytes

Post 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?
User avatar
gzaloprgm
Member
Member
Posts: 141
Joined: Sun Sep 23, 2007 4:53 pm
Location: Buenos Aires, Argentina
Contact:

Re: Transferring control beyond 512 bytes

Post 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
Visit https://gzalo.com : my web site with electronic circuits, articles, schematics, pcb, calculators, and other things related to electronics.
Rituraj
Posts: 2
Joined: Thu Jan 15, 2009 2:58 pm

Re: Transferring control beyond 512 bytes

Post by Rituraj »

thanks but how do i find out the lba address of the disk?
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Transferring control beyond 512 bytes

Post by JohnnyTheDon »

LBA 0 is the first sector, so LBA 1 is the second sector.
nos4A2
Posts: 5
Joined: Thu Jan 15, 2009 3:40 pm

Re: Transferring control beyond 512 bytes

Post by nos4A2 »

But what about using addressing similar to floppy disk drives...like cylinder,head and sector...does that work for pendrives
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Transferring control beyond 512 bytes

Post by JohnnyTheDon »

Yeah, just use the normal int 13h ah=02h interface.
nos4A2
Posts: 5
Joined: Thu Jan 15, 2009 3:40 pm

Re: Transferring control beyond 512 bytes

Post 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
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Transferring control beyond 512 bytes

Post 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.
nos4A2
Posts: 5
Joined: Thu Jan 15, 2009 3:40 pm

Re: Transferring control beyond 512 bytes

Post by nos4A2 »

thanks ill try it out and post the results soon.....
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Transferring control beyond 512 bytes

Post 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.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Transferring control beyond 512 bytes

Post by Combuster »

Also, you're jumping to a different location than where you loaded your stuff... (and yuck @ JNC number)
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
nos4A2
Posts: 5
Joined: Thu Jan 15, 2009 3:40 pm

Re: Transferring control beyond 512 bytes

Post 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?????
nos4A2
Posts: 5
Joined: Thu Jan 15, 2009 3:40 pm

Re: Transferring control beyond 512 bytes

Post by nos4A2 »

IT WORKED....i was givin the sector number wrong....mov ch,0x02....thanks a lot
Hyperdrive
Member
Member
Posts: 93
Joined: Mon Nov 24, 2008 9:13 am

Re: Transferring control beyond 512 bytes

Post by Hyperdrive »

nos4A2 wrote:i am givin ES:BX as 1000:0000 so i am jumping to 0x1000...
Wrong, 0x1000:0x0000 is 0x10000.
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: Transferring control beyond 512 bytes

Post 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
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
Post Reply