代码是multiboot自制内核的一部分,如下:
经过测试,在读取双字节时都是0,而读取的内容实际上不全是0,是啥问题呢?
```c
#define IDE_READ_MAX_SECRORS 256
#define IDE_READ_READNUM_PORT 0x1f2
#define IDE_READ_LBA_PORTA 0x1f3
#define IDE_READ_LBA_PORTB 0x1f4
#define IDE_READ_LBA_PORTC 0x1f5
#define IDE_READ_LBA_PORTD 0x1f6
#define IDE_READ_DEVICE_PORT 0x1f6
#define IDE_READ_MODE_PORT 0x1f6
#define IDE_FUN_MODE 0x1f7
#define IDE_FUN_MODE_READ 0x20
#define IDE_STAGE_PORT 0x1f7
#define IDE_READ_DATA_PORT 0x1f0
unsigned char readSectors(unsigned char * buf,
unsigned int device,
unsigned long lba,
unsigned char numOfSectors){
outb(IDE_READ_READNUM_PORT,numOfSectors);
outb(IDE_READ_LBA_PORTA,lba);
outb(IDE_READ_LBA_PORTB,lba>>8);
outb(IDE_READ_LBA_PORTC,(lba>>16)&0xff);
outb(IDE_READ_LBA_PORTD,0xe0|(device<<4));
outb(IDE_FUN_MODE,IDE_FUN_MODE_READ);
char stat = 0;
int a = 0;
while(((stat&(1<<4))>>4)!=1)
stat = inb(IDE_STAGE_PORT);
unsigned short i;
for(i=0;i<numOfSectors*512;i+=2)
{
short wordData = inw(IDE_READ_DATA_PORT);
buf[i] = wordData & 0xff;
buf[i+1] = wordData >> 8;
}
return numOfSectors;
}
io.h 的部分:
#ifndef _IO_H
#define _IO_H
#define outb(value,port) \
__asm__ ("outb %%al,%%dx"::"a" (value),"d" (port))
#define inb(port) ({ \
unsigned char _v; \
__asm__ volatile ("inb %%dx,%%al":"=a" (_v):"d" (port)); \
_v; \
})
#define outb_p(value,port) \
__asm__ ("outb %%al,%%dx\n" \
"\tjmp 1f\n" \
"1:\tjmp 1f\n" \
"1:"::"a" (value),"d" (port))
#define inb_p(port) ({ \
unsigned char _v; \
__asm__ volatile ("inb %%dx,%%al\n" \
"\tjmp 1f\n" \
"1:\tjmp 1f\n" \
"1:":"=a" (_v):"d" (port)); \
_v; \
})
static __inline void
outw (unsigned short int __value, unsigned short int __port)
{
__asm__ __volatile__ ("outw %w0,%w1": :"a" (__value), "Nd" (__port));
}
static __inline unsigned short int
inw (unsigned short int __port)
{
unsigned short _v;
__asm__ __volatile__ ("inw %w1,%0":"=a" (_v):"Nd" (__port));
return _v;
}
#define NULL -1
#endif
```