Problem reading sector into array...

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
Risc
Posts: 11
Joined: Sun Jun 14, 2009 8:29 pm

Problem reading sector into array...

Post by Risc »

Hi! It's me again :)

Right now I'm having trouble reading a sector from disk into an array from my C-code (Turbo C with inline TASM).
The problem seems to be getting the data into the array (I think the controller reads fine). Take a look:

Code: Select all

char* fl_read_sector(char sector)
{
	char buffer[512];
	
	/* Store registers */
	asm push es
	asm push ax
	asm push bx
	asm push cx
	asm push dx

	/* Where shall we store the data? */
	asm push ds                              <----- THIS SECTION IS VERY VAGUE.... 
	asm pop es                               <----- ...
	asm mov bx, offset buffer            <----- ...
	
	fl_read_sectors_loop:
	
	/* Set up read */
	asm mov ah, 0x02
	asm mov al, 1				/* 1 sector to read */
	asm mov ch, 1				/* First track */	
	asm mov cl, sector		/* Sector to read */
	asm mov dh, 0  			/* First head */
	asm mov dl, 0				/* Default for floppy (drive number) */
	
	/* Do it! */
	asm int 0x13
	asm jc fl_read_sectors_loop	/* Retry on error */
	
	/* Restore registers */
	asm pop dx
	asm pop cx
	asm pop bx
	asm pop ax
	asm pop es

	return buffer;
}
Thanks for any help.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Problem reading sector into array...

Post by ru2aqare »

Risc wrote:Hi! It's me again :)

Right now I'm having trouble reading a sector from disk into an array from my C-code (Turbo C with inline TASM).
The problem seems to be getting the data into the array (I think the controller reads fine). Take a look:

Code: Select all

char* fl_read_sector(char sector)
{
	char buffer[512];

....
	
	/* Where shall we store the data? */
	asm push ds                              <----- THIS SECTION IS VERY VAGUE.... 
	asm pop es                               <----- ...
	asm mov bx, offset buffer            <----- ...

...	
Well there is your problem. The local variable is allocated on the stack, so you can't use offset to get its address (and ss: may not even point to where ds: points). You would need something along the lines of

Code: Select all

    lea bx, [bp - some constant]
Or you could make the local variable static, in which case it is allocated in the data segment, and the same area will be reused on every entry to your function.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: Problem reading sector into array...

Post by bewing »

The "push ds, pop es" pair of instructions is just a simple way of copying the value of segment register ds into es. That usage is extremely common.

It looks to me like the biggest problem you have here is that "buffer" is a completely temporary (ie. "automatic") variable allocation. The entire array is deleted as soon as this function exits. But you try to pass back a pointer to it anyway. You need to make it global or static.

edit: ru2aqare beat me to it. :wink:
Risc
Posts: 11
Joined: Sun Jun 14, 2009 8:29 pm

Re: Problem reading sector into array...

Post by Risc »

Thanks! Doesn't work though. Seems like either nothing is being read to the buffer or I'm writing the data somewhere else (not to the buffer). I rewrote the code and attached a routine at the bottom which prints the contents of the buffer byte-by-byte in decimal form.
Now what I want to do (for the sake of proving that the code runs correctly) is to read the OEM-string from the bootsector. This should be located at sector 0 and 3 bytes in if I'm correct.

Right now I'm passing 0 or 1 to the fl_read_sector function. But whatever sector I choose the buffer seems to contain nothing but zeroes...

Code: Select all

static char buffer[512];

Code: Select all

char* fl_read_sector(char sector)
{
	int i = 0;
	unsigned int bufptr = (unsigned int)buffer;
	
	/* Store registers */
	asm push es
	asm push ax
	asm push bx
	asm push cx
	asm push dx

	/* Where shall we store the data? */
	asm push ds
	asm pop es
	asm lea bx, bufptr
	
	fl_read_sectors_loop:
	
	/* Set up read */
	asm mov ah, 0x02
	asm mov al, 1				/* 1 sector to read */
	asm mov ch, 1				/* First track */	
	asm mov cl, sector		/* Sector to read */
	asm mov dh, 0  			/* First head */
	asm mov dl, 0				/* Default for floppy (drive number) */
	
	/* Do it! */
	asm int 0x13
	asm jc fl_read_sectors_loop	/* Retry on error */
	
	/* Restore registers */
	asm pop dx
	asm pop cx
	asm pop bx
	asm pop ax
	asm pop es

	/* For debug purposes */
	for(i = 0; i < 512; i++)
	{
		printnum(buffer[i]);
		printc(' ');
	}
	return buffer;
}
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Problem reading sector into array...

Post by ru2aqare »

Risc wrote:

Code: Select all

char* fl_read_sector(char sector)
{
	int i = 0;
	unsigned int bufptr = (unsigned int)buffer;

...
	
	asm lea bx, bufptr
	
The lea instruction (among other uses) is used for loading the address of a variable on the stack. You want the contents, not the address of that variable. So:

Code: Select all

    lea bx, [bufptr]
    mov bx, [bx] ; as bufptr is a pointer to the actual buffer
or simply

Code: Select all

   mov bx, offset buffer
Either of these should work, but then again, I'm not familiar with how TC's inline assembler works.
Risc
Posts: 11
Joined: Sun Jun 14, 2009 8:29 pm

Re: Problem reading sector into array...

Post by Risc »

Thanks ru2aqare! It works now! You've been an enormous help with my project :)

Btw, I'm really amazed how helpful the majority of the members seem to be on this forum and to get an answer within the hour :)

Ok ru2aqare I guess I have temporarily given you "god-status". Enjoy it ;)
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: Problem reading sector into array...

Post by ru2aqare »

Risc wrote:Thanks ru2aqare! It works now! You've been an enormous help with my project :)

Btw, I'm really amazed how helpful the majority of the members seem to be on this forum and to get an answer within the hour :)

Ok ru2aqare I guess I have temporarily given you "god-status". Enjoy it ;)
I'm glad to hear I was of any help, it wasn't that difficult a question anyway.
Post Reply