Problems of Computer Systems (continued):

4.) Complexity

 

                                               Moore's law (transistors)                                         Kryder's law (harddrive) : allows us to put more complicated data structures in disk

db


Computer-ception:

Building computers with computers:

c


Say we want a small program with these ideas in mind:

 

Quick disk review:

http://en.wikipedia.org/wiki/Cylinder-head-sector


Disk Controller and how it works:

a
To Tell disk controller a command:

  1. Wait for it to beocme ready
  2. Store # of sectors you want to read into
  3. Store which sector you want to read
  4. Store read command into register
  5. Wait for it to be ready again
  6. Slurp data from disk controller cache through CPU into RAM

The Machine:


Problems with Bootstrapping:

Trying to get some code that starts rest of program:

Instead, we use a BIOS on a ROM:

Pros of a BIOS:

Set program counter to the location in ROM and run the BIOS

So, we could make our own BIOS that runs the word count program or...

Some features of BIOS

  1. Self tests
  2. Looks for devices

 

Looks for special pattern in 1st sector indicating the device is bootable

512 bytes per sector (classic boot)

1st sector - Master Boot Record (MBR) runs some code

e

The other 64 bytes are for the partition descriptor:

16 bytes include:

We could use boot program to run an actual program... but doesn't give us enough flexibility.

So, we use a Volume Boot Record (VBR)

Place size of VBR in BOOT program: 


How do we write the program?

In C: MBR.c -> cc -> mbr.o -> ld -> mbr.executable (512 bytes)

Assume wordcount program is 10KiB, so we need to read 20 sectors

 

static void wait_for_ready(void){
	while(inb(ox1f7) && 0xc0 != 0x40)
	continue;
}

 

int main(void){
	for(int i = 0; i < 20; i++){
		read_sector(i+1, 0x10000 + i*512);
	}
	goto 0x10000
}

 

static void read_sector(int s, char* a, int num_sec){
	wait_for_read();

	outb(0x1f2, num_sec)
	for(int i = 0x1f6; i > 0x1f3; i--){
		outb(i, s&oxff);
		s >> = 8;
	}

	outb(0x1f7, 0x20)
	wait_for_ready();
	insl(0x1f0,a,128);
}

so we have loaded the wordcount program at memory 0x10000 wc.c:

void main(void){
	unsigned long nw = 0
	int sec_num = 21;
	bool inw = false;

	for(;;){
		char buf[512];
		read_sector(s, buf);
		for(int i = 0; i < 512; i++){
			if(buf[i] == '\0'){
			finish(nw);
			for(;;); //must unplug machine to turn off
			}
		bool inw1 = (buf[i] | 'a'-'A') - 'a' < 26
		nw += inwl & ~inw;
		inw = inw1;
		}
	}
}

 

void finish(int n){
	short *p = (short)* 0xb8014;
	do {
		*p-- = '0' + n % 10;
		n /= 10
	} while(n);
}