init: init nachos hw01

This commit is contained in:
AFS_TA
2026-03-27 10:12:37 +08:00
commit 1e698d2426
240 changed files with 76969 additions and 0 deletions

16
c++example/Makefile Executable file
View File

@@ -0,0 +1,16 @@
PREFIX=decstation-ultrix- # add crosscompiler prefix here i.e. decstation-ultrix-
INCLUDEDIR= # add path to include directories for crosscompiler environment here:
# don't forget the -I tag before the directory
# i.e: -I/usr/lcoal/nachosxdev/include -I/usr/local/nachosxdev/include/g++-3
all: stack inheritstack templatestack
stack: stack.h stack.cc
$(PREFIX)g++ $(INCLUDEDIR) -o stack stack.cc
inheritstack: inheritstack.h inheritstack.cc list.h list.cc
$(PREFIX)g++ $(INCLUDEDIR) -o inheritstack inheritstack.cc list.cc
templatestack: templatestack.h templatestack.cc
$(PREFIX)g++ $(INCLUDEDIR) -o templatestack templatestack.cc

2760
c++example/c++.ps Executable file

File diff suppressed because it is too large Load Diff

1576
c++example/c++.tex Executable file

File diff suppressed because it is too large Load Diff

27
c++example/copyright.h Executable file
View File

@@ -0,0 +1,27 @@
#ifndef COPYRIGHT_H
#define COPYRIGHT_H
/*
Copyright (c) 1992,1993,1995 The Regents of the University of California.
All rights reserved.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose, without fee, and without written agreement is
hereby granted, provided that the above copyright notice and the following
two paragraphs appear in all copies of this software.
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
static char *copyright = "Copyright (c) 1992,1993,1995 The Regents of the University of California. All rights reserved.";
#endif /* COPYRIGHT_H */

230
c++example/inheritstack.cc Executable file
View File

@@ -0,0 +1,230 @@
// inheritstack.cc
// Routines for two implementions of a LIFO stack of integers,
// one as an array, the other as a list.
//
// Copyright (c) 1992,1993,1995 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
extern "C" {
#include <assert.h>
#define ASSERT(expression) assert(expression)
}
const bool FALSE = false;
const bool TRUE = true;
#include <iostream.h>
#include "copyright.h"
#include "list.h"
#include "inheritstack.h"
//----------------------------------------------------------------------
// Stack::Stack, Stack::~Stack
// constructor and destructor for the Stack class; no data
// to initialize!
//----------------------------------------------------------------------
Stack::Stack() {}
Stack::~Stack() {}
// IMPLEMENTATION #1: AS AN ARRAY
//----------------------------------------------------------------------
// ArrayStack::ArrayStack
// The constructor for the ArrayStack class.
//
// "sz" -- maximum number of elements on the ArrayStack at any time
//----------------------------------------------------------------------
ArrayStack::ArrayStack(int sz) : Stack() {
ASSERT(sz >= 1);
// Initialize the data members of the stack object.
size = sz;
top = 0;
stack = new int[size]; // allocate an array of integers.
}
//----------------------------------------------------------------------
// ArrayStack::~ArrayStack
// The destructor for the ArrayStack class. Just get rid of the array we
// allocated in the constructor.
//----------------------------------------------------------------------
ArrayStack::~ArrayStack() {
delete [] stack;
}
//----------------------------------------------------------------------
// ArrayStack::Push
// Put an integer on the top of the stack; error on overflow.
//
// "value" -- the value to put on the stack
//----------------------------------------------------------------------
void
ArrayStack::Push(int value) {
ASSERT(!Full());
stack[top++] = value;
}
//----------------------------------------------------------------------
// ArrayStack::Pop
// Remove an integer from the top of the stack, returning its value.
// Error if the stack is empty.
//----------------------------------------------------------------------
int
ArrayStack::Pop() {
ASSERT(!Empty());
return (stack[--top]);
}
//----------------------------------------------------------------------
// ArrayStack::Full
// Return TRUE if the stack has no more room.
//----------------------------------------------------------------------
bool
ArrayStack::Full() {
return (top == size);
}
//----------------------------------------------------------------------
// ArrayStack::Empty
// Return TRUE if the stack has nothing on it.
//----------------------------------------------------------------------
bool
ArrayStack::Empty() {
return (top == 0);
}
// IMPLEMENTATION #2: AS A LIST
//----------------------------------------------------------------------
// ListStack::ListStack
// The constructor for the ListStack class.
//----------------------------------------------------------------------
ListStack::ListStack() : Stack() {
stack = new List; // allocate an empty list of integers.
}
//----------------------------------------------------------------------
// ListStack::~ListStack
// The destructor for the ListStack class. Just get rid of the list we
// allocated in the constructor.
//----------------------------------------------------------------------
ListStack::~ListStack() {
delete stack;
}
//----------------------------------------------------------------------
// ListStack::Push
// Put an integer on the top of the stack.
//
// "value" -- the value to put on the stack
//----------------------------------------------------------------------
void
ListStack::Push(int value) {
stack->Prepend(value);
}
//----------------------------------------------------------------------
// ListStack::Pop
// Remove an integer from the top of the stack, returning its value.
// Error if the stack is empty.
//----------------------------------------------------------------------
int
ListStack::Pop() {
ASSERT(!Empty());
return stack->Remove();
}
//----------------------------------------------------------------------
// ListStack::Full
// Return FALSE, because a liststack can never overflow
//----------------------------------------------------------------------
bool
ListStack::Full() {
return FALSE;
}
//----------------------------------------------------------------------
// ListStack::Empty
// Return TRUE if the stack has nothing on it.
//----------------------------------------------------------------------
bool
ListStack::Empty() {
return stack->Empty();
}
//----------------------------------------------------------------------
// Stack::SelfTest
// Test our stack implementation by pushing 10 numbers onto the
// stack, and then print them as it pops them off.
//
// Note this code is generic between the two versions --
// it doesn't matter whether this is an ArrayStack or a ListStack!
//
// "numToPush" is the number of items to put on the stack in the
// selftest.
//----------------------------------------------------------------------
void
Stack::SelfTest(int numToPush) {
int count = 17;
// Put a bunch of stuff in the stack...
for (int i = 0; i < numToPush; i++) {
ASSERT(!Full());
cout << "pushing " << count << "\n";
Push(count++);
}
// ... and take it out again.
while (!Empty()) {
cout << "popping " << Pop() << "\n";
}
}
//----------------------------------------------------------------------
// main
// Run the test code for the stack implementation.
//----------------------------------------------------------------------
int
main() {
Stack *s1 = new ArrayStack(10); // Constructor with an argument.
Stack *s2 = new ListStack();
cout << "Testing ArrayStack\n";
s1->SelfTest(10);
cout << "Testing ListStack\n";
s2->SelfTest(10);
delete s1; // always delete what you allocate
delete s2; // always delete what you allocate
return 0;
}

86
c++example/inheritstack.h Executable file
View File

@@ -0,0 +1,86 @@
// inheritstack.h
// Data structures for a "stack" -- a Last-In-First-Out list of integers.
//
// We define two separate implementations of stacks, to
// illustrate C++ inheritance.
//
// Copyright (c) 1992,1993,1995 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#ifndef INHERITSTACK_H // to prevent recursive includes
#define INHERITSTACK_H
#include "copyright.h"
#include "list.h"
// The following defines an "abstract" stack of integers.
// This class is abstract because no one is allowed to create
// instances of it; instead, you make instances of the derived
// classes that inherit from it.
class Stack {
public:
virtual ~Stack(); // Destructor
virtual void Push(int value) = 0; // Push an integer on the stack
virtual int Pop() = 0; // Pop an integer off the stack
virtual bool Full() = 0; // Returns TRUE if the stack is full
virtual bool Empty() = 0; // Returns TRUE if the stack is empty
void SelfTest(int numToPush); // Test whether the implementation works.
// Note that the test routine is shared among
// all derived classes because it shouldn't
// matter to the test code which version we're using!
protected:
Stack(); // Constructor is protected to prevent anyone but
// derived classes from calling constructor.
};
// The following defines an implementation of Stack using arrays.
// This is the same as the original implementation in stack.h,
// except we don't need a SelfTest() because that's defined above by Stack!
class ArrayStack : public Stack {
public:
ArrayStack(int sz); // Constructor: initialize variables, allocate space.
~ArrayStack(); // Destructor: deallocate space allocated above.
void Push(int value); // Push an integer on the stack
int Pop(); // Pop an integer off the stack
bool Full(); // Returns TRUE if the stack is full
bool Empty(); // Returns TRUE if the stack is empty
private:
int size; // The maximum capacity of the stack.
int top; // Index of the next position to be used.
int *stack; // A pointer to an array that holds the contents.
};
// The following defines an implementation of Stack using lists.
//
// Note that a list implementation can't overflow, so we don't
// need to pass a maximum size into the constructor.
class ListStack : public Stack {
public:
ListStack(); // Constructor: initialize variables, allocate space.
~ListStack(); // Destructor: deallocate space allocated above.
void Push(int value); // Push an integer on the stack
int Pop(); // Pop an integer off the stack
bool Full(); // Always return FALSE, this implementation never overflows
bool Empty(); // Returns TRUE if the stack is empty
private:
List *stack;
};
#endif INHERITSTACK_H

133
c++example/list.cc Executable file
View File

@@ -0,0 +1,133 @@
// list.cc
// Routines to manage a singly-linked list of integers.
//
// A "ListElement" is allocated for each item to be put on the
// list; it is de-allocated when the item is removed. This means
// we don't need to keep a "next" pointer in every object we
// want to put on a list.
//
// Copyright (c) 1992,1993,1995 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
extern "C" {
#include <assert.h>
#define ASSERT(expression) assert(expression)
}
#include "copyright.h"
#include "list.h"
const int NULL = 0;
// The following class defines a "list element" -- which is
// used to keep track of one item on a list. It is equivalent to a
// LISP cell, with a "car" ("next") pointing to the next element on the list,
// and a "cdr" ("item") containing the item on the list.
//
// Class defined in list.cc, because only the List class can be allocating
// and accessing ListElements.
class ListElement {
public:
ListElement(int value) { item = value; next = NULL;};
// constructor for list element
ListElement *next; // next element on list,
// NULL if this is the last
int item; // value of this element
};
//----------------------------------------------------------------------
// List::List
// Initialize a list, empty to start with.
// Elements can now be added to the list.
//----------------------------------------------------------------------
List::List() {
first = last = NULL;
}
//----------------------------------------------------------------------
// List::~List
// Prepare a list for deallocation. If the list still contains any
// ListElements, de-allocate them.
//----------------------------------------------------------------------
List::~List() {
while (!Empty())
(void) Remove(); // delete all the list elements
}
//----------------------------------------------------------------------
// List::Prepend
// Put an integer on the front of the list.
//
// Allocate a ListElement to keep track of the integer.
// If the list is empty, then this will be the only element.
// Otherwise, put it at the beginning.
//
// "value" is the integer to be put on the list.
//----------------------------------------------------------------------
void
List::Prepend(int value) {
ListElement *element = new ListElement(value);
if (Empty()) { // list is empty
first = element;
last = element;
} else { // else put it before first
element->next = first;
first = element;
}
}
//----------------------------------------------------------------------
// List::Remove
// Remove the first integer from the front of the list.
// Error if nothing on the list.
//
// Returns:
// The removed integer.
//----------------------------------------------------------------------
int
List::Remove() {
ListElement *element = first;
int value;
ASSERT(!Empty());
element = first;
value = first->item;
if (first == last) { // list had one item, now has none
first = NULL;
last = NULL;
} else {
first = element->next;
}
delete element; // deallocate list element -- no longer needed
return value;
}
//----------------------------------------------------------------------
// List::Empty
// Returns TRUE if the list is empty (has no items).
//----------------------------------------------------------------------
bool
List::Empty() {
return (first == NULL);
}

35
c++example/list.h Executable file
View File

@@ -0,0 +1,35 @@
// list.h
// Data structures to manage LISP-like lists.
//
// Copyright (c) 1992,1993,1995 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#ifndef LIST_H
#define LIST_H
#include "copyright.h"
class ListElement;
// The following class defines a "list" -- a singly linked list of
// list elements, each of which contains an integer.
class List {
public:
List(); // initialize the list
~List(); // de-allocate the list
void Prepend(int value); // Put item at the beginning of the list
int Remove(); // Take item off the front of the list
bool Empty(); // is the list empty?
void SelfTest();
private:
ListElement *first; // Head of the list, NULL if list is empty
ListElement *last; // Last element of list
};
#endif // LIST_H

135
c++example/stack.cc Executable file
View File

@@ -0,0 +1,135 @@
// stack.cc
// Routines to implement a LIFO stack of integers.
//
// The stack is represented as an array; we return an error
// if the caller tries to push more things onto the stack than we have
// room for.
//
// Copyright (c) 1992,1993,1995 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
extern "C" {
#include <assert.h>
#define ASSERT(expression) assert(expression)
}
#include <iostream.h>
#include "copyright.h"
#include "stack.h"
//----------------------------------------------------------------------
// Stack::Stack
// The constructor for the Stack class. Note that it doesn't have a
// return type.
//
// "sz" -- maximum number of elements on the Stack at any time
//----------------------------------------------------------------------
Stack::Stack(int sz) {
ASSERT(sz >= 1);
// Initialize the data members of the stack object.
size = sz;
top = 0;
stack = new int[size]; // allocate an array of integers.
}
//----------------------------------------------------------------------
// Stack::~Stack
// The destructor for the Stack class. Just get rid of the array we
// allocated in the constructor.
//----------------------------------------------------------------------
Stack::~Stack() {
delete [] stack;
}
//----------------------------------------------------------------------
// Stack::Push
// Put an integer on the top of the stack; error on overflow.
//
// "value" -- the value to put on the stack
//----------------------------------------------------------------------
void
Stack::Push(int value) {
ASSERT(!Full());
stack[top++] = value;
}
//----------------------------------------------------------------------
// Stack::Pop
// Remove an integer from the top of the stack, returning its value.
// Error if the stack is empty.
//----------------------------------------------------------------------
int
Stack::Pop() {
ASSERT(!Empty());
return (stack[--top]);
}
//----------------------------------------------------------------------
// Stack::Full
// Return TRUE if the stack has no more room.
//----------------------------------------------------------------------
bool
Stack::Full() {
return (top == size);
}
//----------------------------------------------------------------------
// Stack::Empty
// Return TRUE if the stack has nothing on it.
//----------------------------------------------------------------------
bool
Stack::Empty() {
return (top == 0);
}
//----------------------------------------------------------------------
// Stack::SelfTest
// Test our stack implementation by pushing 10 numbers onto the
// stack, and then print them as it pops them off.
//----------------------------------------------------------------------
void
Stack::SelfTest() {
int count = 17;
// Put a bunch of stuff in the stack...
while (!Full()) {
cout << "pushing " << count << "\n";
Push(count++);
}
// ... and take it out again.
while (!Empty()) {
cout << "popping " << Pop() << "\n";
}
}
//----------------------------------------------------------------------
// main
// Run the test code for the stack implementation.
//----------------------------------------------------------------------
int
main() {
Stack *stack = new Stack(10); // Constructor with an argument.
stack->SelfTest();
delete stack; // always delete what you allocate
return 0;
}

38
c++example/stack.h Executable file
View File

@@ -0,0 +1,38 @@
// stack.h
// Data structures for a "stack" -- a Last-In-First-Out list of integers.
//
// Copyright (c) 1992,1993,1995 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#ifndef STACK_H // to prevent recursive includes
#define STACK_H
#include "copyright.h"
// The following defines the Stack class. The functions are
// implemented in the file stack.cc.
//
// The constructor (initializer) for the Stack is passed the number
// of elements (integers) in the stack.
class Stack {
public:
Stack(int sz); // Constructor: initialize variables, allocate space.
~Stack(); // Destructor: deallocate space allocated above.
void Push(int value); // Push an integer on the stack, checking for overflow
int Pop(); // Pop an integer off the stack, checking for underflow.
bool Full(); // Returns TRUE if the stack is full, FALSE otherwise.
bool Empty(); // Returns TRUE if the stack is empty, FALSE otherwise.
void SelfTest(); // Test whether the implementation works.
private:
int size; // The maximum capacity of the stack.
int top; // Index of the next position to be used.
int *stack; // A pointer to an array that holds the contents.
};
#endif // STACK_H

149
c++example/templatestack.cc Executable file
View File

@@ -0,0 +1,149 @@
// templatestack.cc
// Routines to implement a LIFO stack of arbitrary things.
//
// The stack is represented as an array; we return an error
// if the caller tries to push more things onto the stack than we have
// room for.
//
// Copyright (c) 1992,1993,1995 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
extern "C" {
#include <assert.h>
#define ASSERT(expression) assert(expression)
}
#include <iostream.h>
#include "copyright.h"
#include "templatestack.h"
//----------------------------------------------------------------------
// Stack<T>::Stack
// The constructor for the Stack class. Note that it doesn't have a
// return type.
//
// "sz" -- maximum number of elements on the Stack at any time
//----------------------------------------------------------------------
template <class T>
Stack<T>::Stack(int sz) {
ASSERT(sz >= 1);
// Initialize the data members of the stack object.
size = sz;
top = 0;
stack = new T[size]; // allocate an array of integers.
}
//----------------------------------------------------------------------
// Stack<T>::~Stack
// The destructor for the Stack class. Just get rid of the array we
// allocated in the constructor.
//----------------------------------------------------------------------
template <class T>
Stack<T>::~Stack() {
delete [] stack;
}
//----------------------------------------------------------------------
// Stack<T>::Push
// Put a T on the top of the stack; error on overflow.
//
// "value" -- the value to put on the stack
//----------------------------------------------------------------------
template <class T>
void
Stack<T>::Push(T value) {
ASSERT(!Full());
stack[top++] = value;
}
//----------------------------------------------------------------------
// Stack<T>::Pop
// Remove a T from the top of the stack, returning its value.
// Error if the stack is empty.
//----------------------------------------------------------------------
template <class T>
T
Stack<T>::Pop() {
ASSERT(!Empty());
return (stack[--top]);
}
//----------------------------------------------------------------------
// Stack<T>::Full
// Return TRUE if the stack has no more room.
//----------------------------------------------------------------------
template <class T>
bool
Stack<T>::Full() {
return (top == size);
}
//----------------------------------------------------------------------
// Stack<T>::Empty
// Return TRUE if the stack has nothing on it.
//----------------------------------------------------------------------
template <class T>
bool
Stack<T>::Empty() {
return (top == 0);
}
//----------------------------------------------------------------------
// Stack<T>::SelfTest
// Test our stack implementation by pushing 10 T's onto the
// stack, and then print them as it pops them off.
//----------------------------------------------------------------------
template <class T>
void
Stack<T>::SelfTest(T start) {
T count = start;
// Put a bunch of stuff in the stack...
while (!Full()) {
cout << "pushing " << count << "\n";
Push(count++);
}
// ... and take it out again.
while (!Empty()) {
cout << "popping " << Pop() << "\n";
}
}
//----------------------------------------------------------------------
// main
// Run the test code for the stack implementation.
//----------------------------------------------------------------------
int
main() {
Stack<int> *s1 = new Stack<int>(10);
Stack<char> *s2 = new Stack<char>(10);
cout << "Testing Stack<int>\n";
s1->SelfTest(17);
cout << "Testing Stack<char>\n";
s2->SelfTest('a');
delete s1; // always delete what you allocate
delete s2; // always delete what you allocate
return 0;
}

39
c++example/templatestack.h Executable file
View File

@@ -0,0 +1,39 @@
// templatestack.h
// Data structures for a stack" -- a Last-In-First-Out list --
// of arbitrary things.
//
// Copyright (c) 1992,1993,1995 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#ifndef TEMPLATESTACK_H // to prevent recursive includes
#define TEMPLATESTACK_H
#include "copyright.h"
// The following defines the Stack class. The functions are
// implemented in the file templatestack.cc.
//
// T is the type of the thing we want to put on the stack.
template <class T>
class Stack {
public:
Stack(int sz); // Constructor
~Stack(); // Destructor
void Push(T value); // Push a T on the stack
T Pop(); // Pop a T off the stack
bool Full(); // Returns TRUE if the stack is full
bool Empty(); // Returns TRUE if the stack is empty
void SelfTest(T start); // Test whether the implementation works.
private:
int size; // The maximum capacity of the stack.
int top; // Index of the next position to be used.
T *stack; // A pointer to an array that holds the contents.
};
#endif // TEMPLATESTACK_H