0% found this document useful (0 votes)
107 views42 pages

Operating Systems: Elements of Computing Systems, Nisan & Schocken, MIT Press

Uploaded by

deep patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views42 pages

Operating Systems: Elements of Computing Systems, Nisan & Schocken, MIT Press

Uploaded by

deep patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Elements of Computing Systems, Nisan & Schocken, MIT Press www.idc.ac.

il/tecs

Operating Systems

Building a Modern Computer From First Principles

www.nand2tetris.org

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 1
Where we are at:

Human Abstract design Software


abstract interface
Thought hierarchy
Chapters 9, 12
H.L. Language Compiler
& abstract interface
Chapters 10 - 11
Operating Sys.
Virtual VM Translator
abstract interface
Machine Chapters 7 - 8

Assembly
Language

Assembler

Chapter 6

abstract interface
Computer
Machine Architecture
abstract interface
Language
Chapters 4 - 5
Hardware Gate Logic
abstract interface
Platform Chapters 1 - 3 Electrical
Chips & Engineering
Hardware Physics
Logic Gates
hierarchy

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 2
Jack revisited

/**
/** Computes
Computes the
the average
average of
of aa sequence
sequence of
of integers.
integers. */
*/
class Main
class Main {{
function
function void
void main()
main() {{
var
var Array
Array a;
a;
var int length;
var int length;
var
var int
int i,
i, sum;
sum;

let
let length
length == Keyboard.readInt(”How
Keyboard.readInt(”How many
many numbers?
numbers? ”);
”);
let
let aa == Array.new(length);
Array.new(length); //
// Constructs
Constructs the
the array
array
let i =
let i = 0;0;

while
while (i(i << length)
length) {{
let
let a[i]
a[i] == Keyboard.readInt(”Enter
Keyboard.readInt(”Enter the
the next
next number:
number: ”);
”);
let
let sum
sum == sum
sum ++ a[i];
a[i];
let i = i +
let i = i + 1;1;
}}

do
do Output.printString(”The
Output.printString(”The average
average is:
is: ”);
”);
do Output.printInt(sum / length);
do Output.printInt(sum / length);
do
do Output.println();
Output.println();
return;
return;
}}
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 3
Jack revisited

/**
/** Computes
Computes the
the average
average of
of aa sequence
sequence of
of integers.
integers. */
*/
class Main
class Main {{
function
function void
void main()
main() {{
var
var Array
Array a;
a;
var int length;
var int length;
var
var int
int i,
i, sum;
sum;

let length == Keyboard.readInt(”How


let length Keyboard.readInt(”How many
many numbers?
numbers? ”);
”);
let
let aa == Array.new(length);
Array.new(length); //
// Constructs
Constructs the
the array
array
let i =
let i = 0;0;

while
while (i(i << length)
length) {{
let
let a[i]
a[i] == Keyboard.readInt(”Enter
Keyboard.readInt(”Enter the
the next
next number:
number: ”);
”);
let
let sum
sum == sum
sum ++ a[i];
a[i];
let i = i +
let i = i + 1;1;
}}

do Output.printString(”The
do Output.printString(”The average
average is:
is: ”);
”);
do Output.printInt(sum / length);
do Output.printInt(sum / length);
do
do Output.println();
Output.println();
return;
return;
}}
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 4
Typical OS functions

Language extensions / standard library System-oriented services

 Mathematical operations  Memory management


(abs, sqrt, ...) (objects, arrays, ...)

 Abstract data types  I/O device drivers


(String, Date, ...)
 Mass storage
 Output functions
 File system
(printChar, printString ...)
 Multi-tasking
 Input functions
(readChar, readLine ...)  UI management (shell / windows)

 Graphics functions  Security


(drawPixel, drawCircle, ...)
 Communications
 And more ...
 And more ...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 5
The Jack OS

 Math: Provides basic mathematical operations;

 String: Implements the String type and string-related operations;

 Array: Implements the Array type and array-related operations;

 Output: Handles text output to the screen;

 Screen: Handles graphic output to the screen;

 Keyboard: Handles user input from the keyboard;

 Memory: Handles memory operations;

 Sys: Provides some execution-related services.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 6
Jack OS API
class
class Math
Math {{
function
function voidvoid init()
init()
Class String
Class String
function int {{
function int abs(int
abs(int x)
x)
constructor
constructor
function int String
String new(int
multiply(int new(int
x, maxLength)
maxLength)
function
Class int
Arraymultiply(int
{ x, int
int y)
y)
Class
method
method
function intArray
void
void {dispose()
dispose()
divide(int x,
function int divide(int x, int
int y) y)
method
method
function int
function
intint length()
Array new(int
length() size)
function int min(int
function
class Array
min(int
Output
x, intint y)
{{x,new(int y) size)
method
method
function class
char
intchar Output
max(int charAt(int
charAt(int
x, j)
j)
function int max(int
function x, int
void int y)
y)
moveCursor(int i,
method
methodmethod
method
function void
intvoid
void
function
void
sqrt(int
dispose()
void
dispose()
setCharAt(intmoveCursor(int
setCharAt(int
x) j,
j, char
char i, int
c)
c) int j)j)
function int Class
sqrt(int
function Screen
voidx)
ClassappendChar(char
Screen {{
}} method
method
} function
String
String void printChar(char
printChar(char
appendChar(char c)
c)
c)
c)
} function
functionfunction
void void
void clearScreen()
clearScreen()
printString(String s)
method
method voidfunction
void void printString(String
eraseLastChar()
class Memory
eraseLastChar() {{ s)
function class
function
function
void Memory
void
void setColor(boolean
setColor(boolean
printInt(int i) b)
b)
method function
int
method int function void
intValue()
intValue() printInt(int i)
function function
function
void void
void drawPixel(int
int peek(int
drawPixel(int
println() x,
x, int
address)int y)
y)
method function
void function
void
setInt(int
Class int
println()
j)
Keyboard peek(int
{ address)
method voidfunction setInt(int
Class void j)
Keyboard {
drawLine(int x1,
function functionfunction
function
char void
void
backSpace() void drawLine(int
backSpace()
backSpace() x1, int
int y1,
y1,
function
function char backSpace()function void
void poke(intint
poke(int x2,
int x2, int
address, y2)
int
int y2)
address, int value)
value)
}} function
function char
char keyPressed()
keyPressed()
function char doubleQuote()
Class Sys
function char function
function
function
void
doubleQuote()
Class
void Sys {
drawRectangle(int
Array {
drawRectangle(int
alloc(int
x1,
x1, int
size) int y1,
y1,
function char function
newLine()function Array
char alloc(int int
readChar() x2,
size) int
int x2, int y2) y2)
function char newLine() function charvoidreadChar()
function void function
drawCircle(int halt():
x,
}} function
function voidfunction
drawCircle(int
void deAlloc(Array x, int
void halt(): int y,
o) y, int
int r)
r)
function
function void deAlloc(Array
String o)
readLine(String message)
}} function
functionStringvoid readLine(String
error(int message)
errorCode)
}} function void error(int errorCode)
function
function int
int readInt(String
readInt(String message)
message)
function void wait(int
function void wait(int duration) duration)
}}
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 7
A typical OS:

 Is modular and scalable

 Empowers programmers (language extensions)

 Empowers users (file system, GUI, ...)

 Closes gaps between software and hardware

 Runs in “protected mode”

 Typically written in some high level language

 Typically grows gradually, assuming more and more functions

 Must be efficient.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 8
Efficiency

We have to implement various operations on n-bit binary numbers


(n = 16, 32, 64, ...).

For example, consider multiplication

Naïve algorithm: to multiply x*y: { for i = 1 ... y do sum = sum + x }

Run-time is proportional to y

In a 64-bit system, y can be as large as 264.

Multiplications can take years to complete

Algorithms that operate on n-bit inputs can be either:

 Naïve: run-time is proportional to the value of the n-bit inputs

 Good: run-time is proportional to n, the input’s size.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 9
Example I: multiplication

 Run-time: proportional to n

 Can be implemented in SW or HW

 Division: similar idea.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 10
Example II: square root
The square root function has two convenient properties:
 It’s inverse function is computed easily
 Monotonically increasing
Functions that have these two properties can be computed by binary search:

Number of loop iterations is bounded by n/2, thus the run-time is O(n).

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 11
Math operations (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
class Output {
class Screen {
class Screen {
class Memory {
class
class Math
Math {{
class Memory {
class Keyboard {
class Keyboard {
class Sys {
function
function void
void init()
init() class Sys {
function (…)
function (…)


}
}
function int abs(int
function int abs(int x)
x)

 function
function int
int multiply(int
multiply(int x,
x, int
int y)
y)

 function
function int
int divide(int
divide(int x,
x, int
int y)
y)

function
function int
int min(int
min(int x,
x, int
int y)
y)

function int max(int


function int max(int x,
x, int
int y)
y)

 function
function int
int sqrt(int
sqrt(int x)
x)

}}

The remaining functions are simple to implement.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 12
String processing (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
Class
Class String
String {{ class Output {
class Screen {
class Screen {
constructor
constructor String
String new(int
new(int maxLength)
maxLength)
class Memory {
class Memory {
class Keyboard {
class Keyboard {
method
method void
void dispose()
dispose() class Sys {
class Sys {
function (…)
function (…)


}
method
method int
int length()
length() }

method
method char
char charAt(int
charAt(int j)
j)
method
method void
void setCharAt(int
setCharAt(int j,
j, char
char c)
c)
method
method String
String appendChar(char
appendChar(char c)
c)
method
method void
void eraseLastChar()
eraseLastChar()
method
method int
int intValue()
intValue()
method
method void
void setInt(int
setInt(int j)
j)
function
function char
char backSpace()
backSpace()
function char doubleQuote()
function char doubleQuote()
function
function char
char newLine()
newLine()
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 13
Single digit ASCII conversions

 asciiCode(digit) == digit + 48

 digit(asciiCode) == asciiCode - 48

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 14
Converting a number to a string

 SingleDigit–to-character conversions: done


 Number–to-string conversions:

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 15
Memory management (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
class Output {
class Screen {
class Screen {
class Memory {
class Memory {
class Keyboard {
class Keyboard {
class Sys {
class Sys {
function (…)
function (…)


}
}

class
class Memory
Memory {{
function
function int
int peek(int
peek(int address)
address)
function
function void
void poke(int
poke(int address,
address, int
int value)
value)
function Array alloc(int
function Array alloc(int size)
size)
function
function void
void deAlloc(Array
deAlloc(Array o)
o)
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 16
Memory management (naive)
 When a program constructs (destructs) an object, the OS has to allocate
(de-allocate) a RAM block on the heap:
 alloc(size): returns a reference to a free RAM block of size size
 deAlloc(object): recycles the RAM block that object refers to

 The data structure that


this algorithm manages
is a single pointer: free.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 17
Memory management (improved)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 18
Peek and poke

class
class Memory
Memory {{
function
function int
int peek(int
peek(int address)
address)
function
function void
void poke(int
poke(int address,
address, int
int value)
value)
function
function Array
Array alloc(int
alloc(int size)
size)
function
function void
void deAlloc(Array
deAlloc(Array o)
o)
}}

 Implementation: based on our ability to exploit exotic casting in Jack:

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 19
Graphics primitives (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
class Output {
class Screen {
class Screen {
class Memory {
class Memory {
class Keyboard {
class Keyboard {
class Sys {
class Sys {
function (…)
function (…)


}
}

Class
Class Screen
Screen {{

function
function void
void clearScreen()
clearScreen()

function
function void
void setColor(boolean
setColor(boolean b)
b)

function
function void
void drawPixel(int
drawPixel(int x,
x, int
int y)
y)

function
function void
void drawLine(int
drawLine(int x1,
x1, int
int y1,
y1, int
int x2,
x2, int
int y2)
y2)

function
function void
void drawRectangle(int
drawRectangle(int x1,
x1, int
int y1,int
y1,int x2,
x2, int
int y2)
y2)

function
function void
void drawCircle(int
drawCircle(int x,
x, int
int y,
y, int
int r)
r)

}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 20
Memory-mapped screen

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 21
Pixel drawing

 Implementation: using poke(address,value)

screen refresh
program
driver mechanism

part of the screen part of the physical


application
operating system memory map hardware screen

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 22
Image representation: bitmap versus vector graphics

pixel

(0,0)

bitmap vector

 Bitmap file: 00100, 01010,01010,10001,11111,10001,00000, . . .

 Vector graphics file: drawLine(2,0,0,5), drawLine(2,0,4,5), drawLine(1,4,3,4)

 Pros and cons of each method.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 23
Vector graphics: basic operations

0 1 2 3 . . . drawPixel(x,y) (Primitive operation)


0
1 drawLine(x1,y1,x2,y2)
2
3
.
Screen = drawCircle(x,y,r)
.
grid of pixels drawRectangle(x1,y1,x2,y2)
.

drawTriangle(x1,y1,x2,y2,x3,y3)

etc. (a few more similar operations)


0 1 2 3 4 5 6 7 8 9 10 11 12 13
0
1 drawLine(0,3,0,11)
2
drawRectangle(1,3,5,9)
3
4
drawLine(1,12,2,12)
5 drawLine(3,10,3,11)
6 drawLine(6,4,6,9)
7 drawLine(7,0,7,12)
8 drawLine(8,1,8,12)
9

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 24
How to draw a line?

drawLine(x1,y1,x2,y2)

 Basic idea: drawLine is implemented through a sequence of drawPixel operations


 Challenge 1: which pixels should be drawn ?
 Challenge 2: how to draw the line fast ?
 Simplifying assumption: the line that we are asked to draw goes north-east.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 25
Line Drawing

 Given: drawLine(x1,y1,x2,y2)

 Notation: x=x1, y=y1, dx=x2-x1, dy=y2-y1


dy
 Using the new notation:
We are asked to draw a line
between (x,y) and (x+dx,y+dy)
dx

set (a,b) = (0,0) set (a,b) = (0,0)

while there is more work to do while (a ≤ dx) and (b ≤ dy)

drawPixel(x+a,y+b) drawPixel(x+a,y+b)

decide if you want to go right, or up decide if you want to go right, or up

if you decide to go right, set a=a+1; if you decide to go right, set a=a+1;
if you decide to go up, set b=b+1 if you decide to go up, set b=b+1

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 26
Line Drawing algorithm

drawLine(x,y,x+dx,y+dy) drawLine(x,y,x+dx,y+dy)
set (a,b) = (0,0) set (a,b) = (0,0)
while (a ≤ dx) and (b ≤ dy) while (a ≤ dx) and (b ≤ dy)
drawPixel(x+a,y+b) drawPixel(x+a,y+b)
decide if you want to go right, or up Oy if b/a > dy/dx set a=a+1
if you decide to go right, set a=a+1;
Vey else set b=b+1
if you decide to go up, set b=b+1

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 27
Line Drawing algorithm, optimized

Motivation
drawLine(x,y,x+dx,y+dy)
 When you draw polygons, e.g. in animation
set (a,b) = (0,0)
or video, you need to draw millions of lines
while (a ≤ dx) and (b ≤ dy)
 Therefore, drawLine must be ultra fast
drawPixel(x+a,y+b)
 Division is a very slow operation
if b/a > dy/dx set a=a+1
else set b=b+1  Addition is ultra fast (hardware based)

b/a > dy/dx is the same as a*dy < b*dx


drawLine(x,y,x+dx,y+dy)
Define diff = a*dy – b*dx
set (a,b) = (0,0), diff = 0
Let’s take a close look at this diff:
while (a ≤ dx) and (b ≤ dy)
1. b/a > dy/dx is the same as diff < 0
drawPixel(x+a,y+b)
2. When we set (a,b)=(0,0), diff = 0
if diff < 0 set a=a+1, diff = diff + dx
else set b=b+1, diff = diff - dy 3. When we set a=a+1, diff goes up by dy
4. When we set b=b+1, diff goes down by dx

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 28
Circle drawing

The screen
origin (0,0)
is at the top
left.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 29
An anecdote about efficiency and design

……Jobs
Jobsobsessed
obsessedabout
aboutthe
thelook
lookof
ofwhat
whatwould
wouldappear
appearonon
the
thescreen.
screen.One
Oneday
dayBill
BillAtkinson
Atkinsonburst
burstinto
intohis
hisoffice
officeall
all
excited.
excited.He
Hehad
hadjust
justcome
comeupupwith
withaabrilliant
brilliantalgorithm
algorithm
that
thatcould
coulddraw
drawcircles
circlesonscreen
onscreenquickly.
quickly.The
Themath
mathfor
for
making
makingcircles
circlesusually
usuallyrequired
requiredcalculating
calculatingsquare
squareroots,
roots,
which
whichthe
theMotorola
Motorola68000
68000microprocessor
microprocessordidn’t
didn’tsupport.
support.
But
ButAtkinson
Atkinsondid
didaaworkaround
workaroundbased
basedononthe
thefact
factthat
thatthe
the
sum
sumof
ofaasequence
sequenceof
ofodd
oddnumbers
numbersproduces
producesaasequence
sequence
of
ofperfect
perfectsquares
squares(e.g.
(e.g.11++33==4,4,11++33++55==9,9,etc.)
etc.)
When
WhenAtkinson
Atkinsonfired
firedup
uphis
hisdemo,
demo,everyone
everyonewas
was
impressed
impressedexcept
exceptJobs.
Jobs.“Well,
“Well,circles
circlesare
arenice,”
nice,”he
hesaid,
said,
“but
“buthow
howabout
aboutdrawing
drawingrectangles
rectangleswith
withrounded
rounded
corners?”
corners?”
(Steve
(SteveJobs,
Jobs,by
byWalter
WalterIsaacson,
Isaacson,2012)
2012)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 30
To sum up (vector graphics)…

 To do vector graphics (e.g. display a PPT file), you have to draw polygons

 To draw polygons, you need to draw lines

 To draw lines, you need to divide

 Division can be
re-expressed as multiplication

 Multiplication can be
reduced to addition

 Addition is easy.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 31
Character output primitives (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
class Output {
class Screen {
class Screen {
class Memory {
class Memory {
class Keyboard {
class Keyboard {
class Sys {
class Sys {
function (…)
function (…)

class
class Output
Output {{ }

}

function
function void
void moveCursor(int
moveCursor(int i,
i, int
int j)
j)

function
function void
void printChar(char
printChar(char c)
c)

function
function void
void printString(String
printString(String s)
s)

function
function void
void printInt(int
printInt(int i)
i)

function
function void
void println()
println()

function void backSpace()


function void backSpace()

}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 32
Character output

 Given display: a physical screen, say 256 rows by 512 columns


 We can allocate an 11 by 8 grid for each character
 Hence, our output package should manage a 23 lines by 64 characters screen
 Font: each displayable character must have an agreed-upon bitmap
 In addition, we have to manage a “cursor”.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 33
Font implementation (in the Jack OS)

class Output {
class Output {
static Array charMaps;
static Array charMaps;
function void initMap() {
function void initMap() {
let charMaps = Array.new(127);
let charMaps = Array.new(127);
// Assign a bitmap for each character
// Assign a bitmap for each character
do Output.create(32,0,0,0,0,0,0,0,0,0,0,0); // space
do Output.create(32,0,0,0,0,0,0,0,0,0,0,0); // space
do Output.create(33,12,30,30,30,12,12,0,12,12,0,0); // !
do Output.create(33,12,30,30,30,12,12,0,12,12,0,0); // !
do Output.create(34,54,54,20,0,0,0,0,0,0,0,0); // “
do Output.create(34,54,54,20,0,0,0,0,0,0,0,0); // “
do Output.create(35,0,18,18,63,18,18,63,18,18,0,0); // #
do Output.create(35,0,18,18,63,18,18,63,18,18,0,0); // #
...
...
do Output.create(48,12,30,51,51,51,51,51,30,12,0,0); // 0
do Output.create(48,12,30,51,51,51,51,51,30,12,0,0); // 0
do Output.create(49,12,14,15,12,12,12,12,12,63,0,0); // 1
do Output.create(49,12,14,15,12,12,12,12,12,63,0,0); // 1
do Output.create(50,30,51,48,24,12,6,3,51,63,0,0); // 2
do Output.create(50,30,51,48,24,12,6,3,51,63,0,0); // 2
. . .
. . .
do Output.create(65,0,0,0,0,0,0,0,0,0,0,0); // A ** TO BE FILLED **
do Output.create(65,0,0,0,0,0,0,0,0,0,0,0); // A ** TO BE FILLED **
do Output.create(66,31,51,51,51,31,51,51,51,31,0,0); // B
do Output.create(66,31,51,51,51,31,51,51,51,31,0,0); // B
do Output.create(67,28,54,35,3,3,3,35,54,28,0,0); // C
do Output.create(67,28,54,35,3,3,3,35,54,28,0,0); // C
. . .
. . .
return;
return;
} // Creates a character map array
} // Creates a character map array
function void create(int index, int a, int b, int c, int d, int e,
function void create(int index, int a, int b, int c, int d, int e,
int f, int g, int h, int i, int j, int k) {
int f, int g, int h, int i, int j, int k) {
var Array map;
var Array map;
let map = Array.new(11);
let map = Array.new(11);
let charMaps[index] = map;
let charMaps[index] = map;
let map[0] = a;
let map[0] = a;
let map[1] = b;
let map[1] = b;
let map[2] = c;
let map[2] = c;
...
...
let map[10] = k;
let map[10] = k;
return; }
return; }

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 34
Keyboard primitives (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
class Output {
class Screen {
class Screen {
class Memory {
class Memory {
class Keyboard {
class Keyboard {
class Sys {
class Sys {
function (…)
function (…)


}
}

Class
Class Keyboard
Keyboard {{

function
function char
char keyPressed()
keyPressed()

function
function char
char readChar()
readChar()

function
function String
String readLine(String
readLine(String message)
message)

function
function int
int readInt(String
readInt(String message)
message)

}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 35
Keyboard input

 If the RAM address of the keyboard’s memory map is known,


the above logic can be implemented using a peek function
 Problem I: the elapsed time between a “key press” and key release”
events is unpredictable
 Problem II: when pressing a key, the user should get some visible
feedback (cursor, echo, ...).

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 36
A historic moment remembered


…Wozniak
Wozniakbegan
beganwriting
writingthe
thesoftware
softwarethat
that
would
wouldget
getthe
themicroprocessor
microprocessortotodisplay
displayimages
images
on
onthe
thescreen.
screen. After
Afteraacouple
coupleof
ofmonth
monthhehewas
was
ready
readytototest
testit.it.“I“Ityped
typedaafew
fewkeys
keyson
onthe
the
keyboard
keyboardand
andIIwas
wasshocked!
shocked!The
Theletters
letterswere
were
displayed
displayedon
onthe
thescreen.”
screen.”
ItItwas
wasSunday,
Sunday,June
June29,
29,1975,
1975,aamilestone
milestonefor forthe
the
personal
personalcomputer.
computer.“It
“Itwas
wasthe
thefirst
firsttime
timeinin
history,”
history,”Wozniak
Wozniaklater
latersaid,
said,“anyone
“anyonehadhadtyped
typedaa
character
characteron
onaakeyboard
keyboardandandseen
seenititshow
showup
upon
on
their
theirown
owncomputer’s
computer’sscreen
screenright
rightininfront
frontof
of
them”
them”
(Steve
(SteveJobs,
Jobs,by
byWalter
WalterIsaacson,
Isaacson,2012)
2012)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 37
Keyboard input (cont.)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 38
Jack OS recap
Project 12:
class
class Math
Math {{
function
function
Class
void
void init()
String {init() Build it.
Class
function String {
function int abs(int x)
int abs(int x)
Class
Class Array
Array {{
function
function Array
Array new(int
new(int size)
size)
class
class Output
Output {{
method
method void
void dispose()
dispose()
Class
Class Screen
Screen {{
}}
class
class Memory
Memory {{
function
Class int peek(int
int
Class Keyboard
function {{
peek(int
Keyboard address)
address)

Class
Class Sys
Sys {{
function
function void
void halt():
halt():
function
function void error(int errorCode)
void error(int errorCode)
function void wait(int duration)
function void wait(int duration)
}}

 Implementation: just like GNU Unix and Linux were built:


 Start with an existing system,
and gradually replace it with a new system,
one library at a time.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 39
Perspective
 What we presented can be described as a:

 mini OS

 Standard library

 Many classical OS functions are missing

 No separation between user mode and OS mode

 Some algorithms (e.g. multiplication and division) are standard

 Other algorithms (e.g. line- and circle-drawing) can be accelerated


with special hardware

 And, by the way, we’ve just finished building the computer.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 40
Here’s Nand;
Go build
a computer

Nand

In CS, God gave us Nand

Everything else was done by humans.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 41
Some Final notes

 CS is a science
 What is science?
 Reductionism
 Life science: From Aristo (millions of rules) to Darwin (3 rules) to Watson
and Crick (1 rule)
 Computer science: We knew in advance that we could build a computer
from almost nothing. In this course we actually did it.
 Key lessons:
 Elegance
 Clarity
 Simplicity
 Playfulness.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.nand2tetris.org , Chapter 12: Operating System slide 42

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy