UCLA CS 111 - April 2, 2014 - Professor Eggert
Notes by Aman Agarwal, Nicholas van Hoff, Zhao Yang, Kyle Gronich
Complexity is the biggest challenge in OS design. Professor Eggert: Complexity bites us harder than bites elsewhere
We need a way of counting the number of words in a proposal. We need to be very secure. We cannot trust an operating system, because it might contain codes written by a fellow professor to steal his proposal.
Write a standalone program that is OS-agnostic.
Most software on a computer is loaded using other software already running on that computer. Bootstrapping is the process of putting the initial software onto the computer.
Read-only memory (ROM) is a solution to the bootstrapping problem by allowing computers to be shipped with a start up program.
Basic Input/Output System (BIOS) resided on the ROM and servers to:
mbr.c
mbr.c -> compiler -> mbr.o -> ld -> mbr exec
dd if=mbr of=/dev/disk1/0 count=1 size=512
mbr.c
int main(void)
{
for (int i = 0; i <= 20; i++) {
read_sector(1 + i, 0x100000 + i * 512);
}
goto *0x100000;
}
static void read_sector(int s, char *a, int ns)
{
wait_for_ready();
outb(0x1f2, ns);
for (int i = 0x1f6; i >= 0x1f3; i--) {
outb(i, s & 0xff);
s >>= 8;
}
outb(0x1f7, 0x20); //READ command
wait_for_ready();
insl(0x1f0, (int*)a, 128);
}
static void wait_for_ready()
{
while((inb(0x1f7) & 0xc0) != 0x40) // wait for the disk controller to be ready.
continue;
}
static unsigned char inb(int addr) {
asm("inb ..."); //Assembly instruction
}
wc.c
void main(void)
{
unsigned long nw = 0;
bool inw = false;
int s = 21;
for(;;)
{
char buf[512];
read_sector(s, buf);
for(int i=0; i<512; i++)
{
if(buf[i] == '\0')
{
finish(nw);
return;
}
bool inw1 = (buf[i]|('a'-'A'))-'a' < 26u;
// or: bool inw1 = ('a' ≤ buf[i] && buf[i] ≤ 'z' || 'Z');
nw += inw1 & inw;
inw = inw1;
}
}
}
void finish(int n)
{
short *p = (short *) 0xb8014;
do {
*p-- = '0' + n % 10;
n /= 10;
}while(n);
}