C, Inline asm and floppy reading

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
User avatar
Fergo
Member
Member
Posts: 27
Joined: Fri Oct 19, 2007 3:11 pm
Contact:

C, Inline asm and floppy reading

Post by Fergo »

Hello everyone.
I'm new into the OS Development (something about 6 months or less) and I now have some questions about reading a floppy sector using inline assembly in C. I don't have enough knowledge to work in PMode yet, so I'm "practicing" in Real Mode first.
I have my bootloaded coded in asm, of course, where I use INT13 to read sectors from floppy to memory. In that case, ES:BX points to the address where the data from floppy will be stored. The problem is that I'm writing my "mini OS" in C (using Turbo C++ 3) and I thought that was useful to code a function that receives the CHS and a destination pointer (possibly a 512 byte vector). I'm at the university now so I don't have the exact code here, but I don't know how to control the SEG:OFF of the destination (or how to know where I'm loading data to). I did something like this:

Code: Select all

unsigned char flBuff[512]

void readCHS(unsigned char Cylinder, unsigned char Head, unsigned char Sector, unsigned char Buffer[]) {
	asm mov ah, 0x02
	asm mov al, 6
	asm mov ch, Cylinder
	asm mov cl, Sector
	asm mov dh, Head                               
	asm mov bx, 0x09C0  						
	asm mov es, bx
	asm mov bx, Buffer
	asm int 0x13
}
As I said, I'm not home now, so I'm not sure about this code, but I was hoping for some recommendations on how to do it the best way. I'm also open for recommendations on memory mapping for a RMode OS. I'm using this, but I'm no so happy with it:

Bootsector: 07C0:0000
Stack: 09C0:0000 (512 bytes "above" the BS) to 09C0:0FFF
Kernel: 09C0:1000

Regards,
Fergo.
Image
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Re: C, Inline asm and floppy reading

Post by xyjamepa »

you can do this:

Code: Select all

void resetflop()
{
  asm xor ah,ah;
  asm mov dl,0x00;
  asm push ds
  asm int 0x13;
  asm pop es
}

int flopread(char h,char t,char s,char n,char *p)
{
  char num;
  asm mov cx,0x3;
 lp:
  asm push cx;
  asm mov ah,0x02
  asm mov al,n
  asm mov ch,t
  asm mov cl,s
  asm mov dh,h
  asm mov dl,0x0
  asm mov bx,p
  asm push ds
  asm int 0x13
  asm pop es
  asm jnc nerror;
  num=0;
  asm mov num,ah
  resetflop();
  asm pop cx;
  asm dec cx;
  asm jnz lp;
  asm mov num,ah
  return num;
 nerror:
  asm mov num,ah
  return num;
}
or even simpler:

Code: Select all

void   read(char h,char t,char s,char n)
{
  char buffer[512];
   asm mov ah,0x02
   asm mov al,n
   asm mov cl,s
   asm mov ch,t
   asm mov dh,h
   asm mov dl,0x00
   asm lea bx,buffer
   asm int 0x13
}
both of codes worked well for me with TC 3.0
The man who follows the crowd will usually get no further than the crowd.
The man who walks alone is likely to find himself in places
no one has ever been before.
User avatar
Fergo
Member
Member
Posts: 27
Joined: Fri Oct 19, 2007 3:11 pm
Contact:

Re: C, Inline asm and floppy reading

Post by Fergo »

Thanks abuashraf! Just to questions though:

1) Both codes are assuming that ES is already pointing to the correct segment (09C0 in my case)?
2) Why do you first push DS, call the interruption and then pops ES? (basically doing ES = DS)

Thanks a lot again, I really appreciate.

Regards,
Fergo
Image
User avatar
xyjamepa
Member
Member
Posts: 397
Joined: Fri Sep 29, 2006 8:59 am

Re: C, Inline asm and floppy reading

Post by xyjamepa »

Both codes are assuming that ES is already pointing to the correct segment (09C0 in my case)?
Yes
Why do you first push DS, call the interruption and then pops ES? (basically doing ES = DS)
When you execute the int ,the content of ES:BX will be changed to hold another address
which is the address of your data.You can pop to ES,it's not a problem as long as ES is
an extra data segmennt.
The man who follows the crowd will usually get no further than the crowd.
The man who walks alone is likely to find himself in places
no one has ever been before.
User avatar
Fergo
Member
Member
Posts: 27
Joined: Fri Oct 19, 2007 3:11 pm
Contact:

Re: C, Inline asm and floppy reading

Post by Fergo »

Oh, I've got it. Thank you.

Regards,
Fergo
Image
Post Reply