Lecture 2 Scribe Notes

Abstractions and Bootstrapping


What would happen if we had no Operating System?

Example Problem: English professor needs to count the words in a file.

Constraints:

  1. Paranoia – Use only our own code*

  2. We want it to consume few resources

  3. Simplest User Interface (UI) – power on to run

*Except for firmware. We trust Dell has supplied quality code.


Machine Specs:


File Format:


Possible Future Extensions:


Output Format:

Power on to use. Immediately display word count.

graphics1





Problem: RAM is initialized to 0's after power on. How does our program get loaded onto RAM to get run?


What we want:

graphics2












Solution: Use Programmable Read Only Memory (PROM) because after a program is loaded to PROM once, it will remain there after power off.


So what could we do with PROM?

  1. Load our program to PROM

  1. Use Dell's firmware on PROM, which is a program that will eventually load our program


Bootstrapping/BIOS

“Pull oneself over a fence by one's bootstraps.” - Teddy Roosevelt


Program in PROM needs to get our program off the disk and into main memory. This program is the Basic I/O System (BIOS).

BIOS:

  1. Tests system (hardware checks).

  2. Looks for devices.

  3. Find one that is bootable. Usually, this is the Master Boot Record (MBR), identified by the 2 bytes, 0xaa55.

Object1




  1. Read MBR to address 0x7c00 of main memory.

  2. Set instruction pointer (IP) to 0x7c00.

Object2







Programmed I/O


void read_sector (int s, char* a) {

// s: sector #

// a: RAM address


while((inb(0x1f7) & 0xc0) != 0x40) { // 0xc0: 11000000 0x40: 01000000

continue;

}

wait_for_ready();

outb(0x1f2, 1);

outb(0x1f3, s & 0xff);

outb(0x1f4, (s>>8) & 0xff);

outb(0x1f5, (s>>16) & 0xff);

outb(0x1f6, (s>>24) & 0xff);

outb(0x1f7, 0x20);

wait_for_ready();

insl(0x1f0, a, 128); // insl reads 128 32-bit words from register 0x1f0 to a

}


And finally, our Word Count Program:

int main (void) {

// Word Count Program

int words = 0;

bool inword = false;

int s = 0x100000;

for ( ; ; s++) {

char buf [512];

read_sector(s, buf);

for (int j = 0; j < 512; j++) {

if (buf [j] == '\0') {

words += inword;

writeout (words);

return;

}

bool thisalpha = isalpha ( (unsigned char) buf [j]);

words += inword & ~thisalpha; // This yields 1 when we cross a word boundary

}

}

}


void writeout (int n) {

// Print to screen

char* screen = 0xb8000 + (25*80*2/2);

do {

*--screen = (n % 10) + '\0';

*--screen = 7; // grey on black

} while ( (n /= 10) != 0);

}