Operating system The operating system itself is a large program which often

Operating system the operating system itself is a

This preview shows page 12 - 17 out of 102 pages.

Operating system: The operating system itself is a large program which often takes up a large part of the available memory. Note that this figure is very simplified. It does not show, for instance, special memory which might be located inside the devices or CPU. Such memory is often used for caching. Also it does not show how the various components are connected together by means of a high speed data bus .
Image of page 12
Figure 2.1: A simple schematic memory map of a microcomputer. The order of the different segments of memory can vary depending on the system. 2.2 Stack
Image of page 13
A stack is a so-called last-in first-out (LIFO) data structure. That is to say - the last thing to be placed on top of a stack, when making it, is the first item which gets removed when un-making it. Stacks are used by the CPU to store the current position within a program before jumping to subroutines, so that they remember where to return to after the subroutine is finished. Because of the nature of the stack, the CPU can simply deposit the address of the next instruction to be executed (after the subroutine is finished) on top of the stack. When the subroutine is finished, the CPU pulls the first address it finds off the top of the stack and jumps to that location. Notice that the stack mechanism will continue to work even if the subroutine itself calls another subroutine, since the second subroutine causes another stack frame to be saved on the top of the stack. When that is finished, it returns to the first subroutine and then to the original program in the correct order. On many older microcomputers and in many operating systems the stack is allocated with a fixed size in advance. If too many levels of nested subroutines are called, the stack can overflow . Consider the following example code for a stack. // // A simple stack handler. // // Use the commands "push" and "pop" to push onto the stack and to pop // "out" of the stack. The allocated stacksize is very small so that // an overflow can occur if you push too far!! e.g. input // // push 23 // push 4 // pop // push 678 // quit // // In a real stack handler the numbers would be the address of the next // instruction to return to after completing a subroutine. // // The program is compiled with // // g++ stack.C // // MB 1994 // //********************************************************************* #include <iostream.h> #include <strstream.h> #include <string.h> //********************************************************************* * // Include file //********************************************************************* *
Image of page 14
const int forever = 1; const int stacksize = 10; const int bufsize = 20; //********************************************************************* * class Stack { public: int stack[stacksize]; Stack(); void ShowStack(); void Push(int); int Pop(); private: int stackpointer; }; //********************************************************************* * // Level 0 //********************************************************************* * main () { char input[bufsize]; char command[5]; int number, newnumber; Stack s; cout << "Stack demo\n\n"; s.ShowStack(); while (forever) { cout << "Enter command: "; // Extract command cin.getline(input,bufsize); istrstream(input,sizeof(input)) >> command >> number; // Interpret command if (strcmp(command,"push") == 0) { s.Push(number); } else if (strcmp(command,"pop")==0)
Image of page 15
{ newnumber = s.Pop(); } else if (strcmp(command,"quit")==0) { break; } else { number = 0; cout << "Bad command\n\n"; } s.ShowStack(); } s.ShowStack(); } //********************************************************************* * // Class Stack //********************************************************************* * Stack::Stack() { int i; stackpointer = 0; for (i = 0; i < stacksize; i++) { stack[i] = 0; } } //********************************************************************* * void Stack::Push (int n) { cout << "Pushing " << n << " on the stack\n"; if (stackpointer >= stacksize) { cerr << "Stack overflow!\n"; return; } stack[stackpointer] = n; stackpointer++; } //********************************************************************* *
Image of page 16
Image of page 17

You've reached the end of your free preview.

Want to read all 102 pages?

  • One '20

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors